src/Controller/ApiV2/Residents/ProfileController.php line 143

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ApiV2\Residents;
  3. use App\Controller\ApiV2\AbstractController;
  4. use App\Entity\Building;
  5. use App\Entity\Resident;
  6. use App\Exception\FormException;
  7. use App\Form\ApiV2\ChangeApiV2PasswordFormType;
  8. use App\Repository\BuildingRepository;
  9. use App\Repository\FlatRepository;
  10. use App\Services\HashIdService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use FOS\RestBundle\Controller\Annotations as Rest;
  16. class ProfileController extends AbstractController
  17. {
  18.     private EntityManagerInterface $entityManager;
  19.     private UserPasswordHasherInterface $passwordHasher;
  20.     private HashIdService $hashIdService;
  21.     private FlatRepository $flatRepository;
  22.     private BuildingRepository $buildingRepository;
  23.     /**
  24.      * @param BuildingRepository $buildingRepository
  25.      * @param EntityManagerInterface $entityManager
  26.      * @param UserPasswordHasherInterface $passwordHasher
  27.      * @param HashIdService $hashIdService
  28.      * @param FlatRepository $flatRepository
  29.      */
  30.     public function __construct(
  31.         BuildingRepository $buildingRepository,
  32.         EntityManagerInterface $entityManager,
  33.         UserPasswordHasherInterface $passwordHasher,
  34.         HashIdService $hashIdService,
  35.         FlatRepository $flatRepository
  36.     ) {
  37.         parent::__construct($buildingRepository);
  38.         $this->entityManager $entityManager;
  39.         $this->passwordHasher $passwordHasher;
  40.         $this->hashIdService $hashIdService;
  41.         $this->flatRepository $flatRepository;
  42.         $this->buildingRepository $buildingRepository;
  43.     }
  44.     /**
  45.      * @Rest\Post ("/change-password", name="residents_change_password")
  46.      * @param Request $request
  47.      * @return array<string, string>
  48.      */
  49.     public function changePassword(Request $request): array
  50.     {
  51.         /** @var Resident $resident */
  52.         $resident $this->getUser();
  53.         try {
  54.             $data $this->validateForm($requestChangeApiV2PasswordFormType::class);
  55.             if (!$this->passwordHasher->isPasswordValid($resident$data['currentPassword'])) {
  56.                 throw new BadRequestException("Neispravna lozinka!");
  57.             }
  58.             $resident->setPassword(
  59.                 $this->passwordHasher->hashPassword(
  60.                     $resident,
  61.                     $data['password']['first']
  62.                 )
  63.             );
  64.             $this->entityManager->persist($resident);
  65.             $this->entityManager->flush();
  66.             return [
  67.                 "message" => "Uspesno ste promenili lozinku."
  68.             ];
  69.         } catch (FormException $exception) {
  70.             return $exception->getFormError();
  71.         }
  72.     }
  73.     /**
  74.      * @Rest\Post ("/disable", name="residents_disable")
  75.      * @return array<string, string>
  76.      */
  77.     public function disable(): array
  78.     {
  79.         /** @var Resident $resident */
  80.         $resident $this->getUser();
  81.         $resident->setDisabled(true);
  82.         $this->entityManager->persist($resident);
  83.         $this->entityManager->flush();
  84.         return [
  85.             "message" => "Uspesno ste deaktivirali nalog."
  86.         ];
  87.     }
  88.     /**
  89.      * @Rest\Post ("/add-new-flat", name="residents_add_new_flat")
  90.      * @param Request $request
  91.      * @return array<string, string>
  92.      */
  93.     public function addNewFlat(Request $request): array
  94.     {
  95.         /** @var Resident $resident */
  96.         $resident $this->getUser();
  97.         $code $request->request->get('code');
  98.         $flat $this->flatRepository->findOneBy(['id' => $this->hashIdService->decodeHashedId($code)]);
  99.         if (!$flat) {
  100.             throw $this->createNotFoundException('Ovaj stan ne postoji.');
  101.         }
  102.         if ($flat->getResident() !== null) {
  103.             throw $this->createNotFoundException('Kod stana je već u upotrebi.');
  104.         }
  105.         $flat->setResident($resident);
  106.         $this->entityManager->persist($flat);
  107.         $this->entityManager->flush();
  108.         return [
  109.             "message" => "Uspesno ste dodali poseban deo."
  110.         ];
  111.     }
  112.     /**
  113.      * @Rest\Get ("/list", name="residents_list_buildings_and_flats")
  114.      * @return array<mixed>
  115.      */
  116.     public function listBuildingsAndFlat(): array
  117.     {
  118.         /** @var Resident $resident */
  119.         $resident $this->getUser();
  120.         $flats $this->flatRepository->getAllFlatsByResident($resident);
  121.         $list = [];
  122.         foreach ($flats as $flat) {
  123.             /** @var Building $building */
  124.             $building $this->buildingRepository->findOneBy(['id' => $flat->getBuilding()->getId()]);
  125.             if (!isset($list[$building->getAddress()])) {
  126.                 $list[$building->getAddress()] = [];
  127.             }
  128.             $list[$building->getAddress()][] = $flat;
  129.         }
  130.         return [
  131.             'data' => $list
  132.         ];
  133.     }
  134. }