LaLiga-BackEnd/src/Service/League/createFacilities/HandleCreateFacilities.php

59 lines
2.0 KiB
PHP

<?php
namespace DMD\LaLigaApi\Service\League\createFacilities;
use DMD\LaLigaApi\Dto\FacilityDto;
use DMD\LaLigaApi\Dto\LeagueDto;
use DMD\LaLigaApi\Entity\CustomRole;
use DMD\LaLigaApi\Entity\League;
use DMD\LaLigaApi\Entity\User;
use DMD\LaLigaApi\Enum\Role;
use DMD\LaLigaApi\Exception\ValidationException;
use DMD\LaLigaApi\Repository\UserRepository;
use DMD\LaLigaApi\Service\League\FacilityFactory;
use DMD\LaLigaApi\Service\League\LeagueFactory;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HandleCreateFacilities
{
public function __construct(
public FacilityFactory $facilityFactory,
public EntityManagerInterface $entityManager,
public Security $security,
public UserRepository $userRepository
){}
/**
* @throws ValidationException
*/
public function __invoke(Request $request): JsonResponse
{
$userEntity = $this->userRepository->findOneBy([
'email' => $this->security->getUser()?->getUserIdentifier()
]);
if (is_null($userEntity)) {
throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized.");
}
$facilityDto = new FacilityDto();
$facilityDto->fillFromArray($request->toArray());
$facilityDto->validate();
$facilityEntity = $this->facilityFactory::create($facilityDto);
$this->entityManager->persist($facilityEntity);
$this->entityManager->flush();
$facilityDto->id = $facilityEntity->getId();
$facilityDto->createdAt = $facilityEntity->getCreatedAt();
return new JsonResponse(
data: [
'success' => true,
'league' => $facilityDto->toArray()
],
status: Response::HTTP_OK
);
}
}