<?php
namespace App\Controller\ApiV2\Residents;
use App\Controller\ApiV2\AbstractController;
use App\Entity\Building;
use App\Entity\Resident;
use App\Exception\FormException;
use App\Form\ApiV2\ChangeApiV2PasswordFormType;
use App\Repository\BuildingRepository;
use App\Repository\FlatRepository;
use App\Services\HashIdService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
class ProfileController extends AbstractController
{
private EntityManagerInterface $entityManager;
private UserPasswordHasherInterface $passwordHasher;
private HashIdService $hashIdService;
private FlatRepository $flatRepository;
private BuildingRepository $buildingRepository;
/**
* @param BuildingRepository $buildingRepository
* @param EntityManagerInterface $entityManager
* @param UserPasswordHasherInterface $passwordHasher
* @param HashIdService $hashIdService
* @param FlatRepository $flatRepository
*/
public function __construct(
BuildingRepository $buildingRepository,
EntityManagerInterface $entityManager,
UserPasswordHasherInterface $passwordHasher,
HashIdService $hashIdService,
FlatRepository $flatRepository
) {
parent::__construct($buildingRepository);
$this->entityManager = $entityManager;
$this->passwordHasher = $passwordHasher;
$this->hashIdService = $hashIdService;
$this->flatRepository = $flatRepository;
$this->buildingRepository = $buildingRepository;
}
/**
* @Rest\Post ("/change-password", name="residents_change_password")
* @param Request $request
* @return array<string, string>
*/
public function changePassword(Request $request): array
{
/** @var Resident $resident */
$resident = $this->getUser();
try {
$data = $this->validateForm($request, ChangeApiV2PasswordFormType::class);
if (!$this->passwordHasher->isPasswordValid($resident, $data['currentPassword'])) {
throw new BadRequestException("Neispravna lozinka!");
}
$resident->setPassword(
$this->passwordHasher->hashPassword(
$resident,
$data['password']['first']
)
);
$this->entityManager->persist($resident);
$this->entityManager->flush();
return [
"message" => "Uspesno ste promenili lozinku."
];
} catch (FormException $exception) {
return $exception->getFormError();
}
}
/**
* @Rest\Post ("/disable", name="residents_disable")
* @return array<string, string>
*/
public function disable(): array
{
/** @var Resident $resident */
$resident = $this->getUser();
$resident->setDisabled(true);
$this->entityManager->persist($resident);
$this->entityManager->flush();
return [
"message" => "Uspesno ste deaktivirali nalog."
];
}
/**
* @Rest\Post ("/add-new-flat", name="residents_add_new_flat")
* @param Request $request
* @return array<string, string>
*/
public function addNewFlat(Request $request): array
{
/** @var Resident $resident */
$resident = $this->getUser();
$code = $request->request->get('code');
$flat = $this->flatRepository->findOneBy(['id' => $this->hashIdService->decodeHashedId($code)]);
if (!$flat) {
throw $this->createNotFoundException('Ovaj stan ne postoji.');
}
if ($flat->getResident() !== null) {
throw $this->createNotFoundException('Kod stana je već u upotrebi.');
}
$flat->setResident($resident);
$this->entityManager->persist($flat);
$this->entityManager->flush();
return [
"message" => "Uspesno ste dodali poseban deo."
];
}
/**
* @Rest\Get ("/list", name="residents_list_buildings_and_flats")
* @return array<mixed>
*/
public function listBuildingsAndFlat(): array
{
/** @var Resident $resident */
$resident = $this->getUser();
$flats = $this->flatRepository->getAllFlatsByResident($resident);
$list = [];
foreach ($flats as $flat) {
/** @var Building $building */
$building = $this->buildingRepository->findOneBy(['id' => $flat->getBuilding()->getId()]);
if (!isset($list[$building->getAddress()])) {
$list[$building->getAddress()] = [];
}
$list[$building->getAddress()][] = $flat;
}
return [
'data' => $list
];
}
}