From e2a4c6f3bd80ddc6546175d0cffacdee67ef775e Mon Sep 17 00:00:00 2001 From: Daniel Guzman Date: Sun, 11 Aug 2024 02:21:42 +0200 Subject: [PATCH] Refactor --- src/Service/League/LeagueFactory.php | 27 ++++++---- .../updateLeague/HandleUpdateLeague.php | 54 ++++--------------- 2 files changed, 27 insertions(+), 54 deletions(-) diff --git a/src/Service/League/LeagueFactory.php b/src/Service/League/LeagueFactory.php index 72436e87..5a002d6f 100644 --- a/src/Service/League/LeagueFactory.php +++ b/src/Service/League/LeagueFactory.php @@ -9,7 +9,24 @@ class LeagueFactory { public static function create(LeagueDto $leagueDto): League { - $leagueEntity = new League(); + $leagueEntity = self::assignProperties(new League(), $leagueDto); + if (isset($leagueDto->isPublic)) + { + $leagueEntity->setIsPublic($leagueDto->isPublic); + } + else + { + $leagueEntity->setIsPublic(true); + } + return $leagueEntity; + } + public static function update(League $leagueEntity,LeagueDto $leagueDto): League + { + return self::assignProperties($leagueEntity, $leagueDto); + } + + private static function assignProperties(League $leagueEntity, LeagueDto $leagueDto): League + { if (!empty($leagueDto->name)) { $leagueEntity->setName($leagueDto->name); @@ -34,14 +51,6 @@ class LeagueFactory { $leagueEntity->setCity($leagueDto->city); } - if (isset($leagueDto->isPublic)) - { - $leagueEntity->setIsPublic($leagueDto->isPublic); - } - else - { - $leagueEntity->setIsPublic(true); - } return $leagueEntity; } } \ No newline at end of file diff --git a/src/Service/League/updateLeague/HandleUpdateLeague.php b/src/Service/League/updateLeague/HandleUpdateLeague.php index 57887e82..26e7ffcf 100644 --- a/src/Service/League/updateLeague/HandleUpdateLeague.php +++ b/src/Service/League/updateLeague/HandleUpdateLeague.php @@ -7,6 +7,7 @@ use DMD\LaLigaApi\Entity\League; use DMD\LaLigaApi\Repository\CustomRoleRepository; use DMD\LaLigaApi\Repository\LeagueRepository; use DMD\LaLigaApi\Repository\UserRepository; +use DMD\LaLigaApi\Service\Common\AuthorizeRequest; use DMD\LaLigaApi\Service\League\LeagueFactory; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\SecurityBundle\Security; @@ -18,8 +19,9 @@ use Symfony\Component\HttpKernel\Exception\HttpException; class HandleUpdateLeague { public function __construct( - public LeagueFactory $leagueSaver, + public LeagueFactory $leagueFactory, public EntityManagerInterface $entityManager, + public AuthorizeRequest $authorizeRequest, public Security $security, public UserRepository $userRepository, public LeagueRepository $leagueRepository, @@ -28,16 +30,15 @@ class HandleUpdateLeague public function __invoke(Request $request, int $leagueId): JsonResponse { - $authorizationResult = $this->checkUserAuthorization($leagueId); + $this->authorizeRequest->authorizeLeaguePresident($leagueId); + $user = $this->security->getUser(); + $leagueEntity = $this->leagueRepository->find($leagueId); $leagueDto = new LeagueDto(); - $leagueDto->fillFromObject($authorizationResult['leagueObj']); + $leagueDto->fillFromObject($leagueEntity); $leagueDto->fillFromArray($request->toArray()); $leagueDto->validate(); - if (!empty($leagueDto->validationErrors)) - { - return $this->generateErrorResponse($leagueDto->validationErrors); - } - $this->leagueSaver->save($authorizationResult['leagueObj'], $leagueDto); + $leagueEntity = $this->leagueFactory::update($leagueEntity, $leagueDto); + $this->entityManager->persist($leagueEntity); $this->entityManager->flush(); return new JsonResponse( data: [ @@ -47,41 +48,4 @@ class HandleUpdateLeague status: Response::HTTP_OK ); } - - public function generateErrorResponse(array $validationErrors): JsonResponse - { - return new JsonResponse( - data: [ - 'success' => false, - 'error' => $validationErrors - ], - status: Response::HTTP_OK - ); - } - - public function checkUserAuthorization(int $leagueId): array - { - $user = $this->security->getUser(); - if (is_null($user)) - { - throw new HttpException(Response::HTTP_FORBIDDEN, "Unauthorized."); - } - $customRole = $this->customRoleRepository->findBy([ - 'name' => $leagueId.'_LEAGUE_PRESIDENT', - 'user' => $user - ]); - if (is_null($customRole)) - { - throw new HttpException(Response::HTTP_FORBIDDEN, "Usuario no tiene permiso para editar la liga."); - } - $leagueObj = $this->leagueRepository->find($leagueId); - if (is_null($leagueObj)) - { - throw new HttpException(Response::HTTP_NOT_FOUND, "Liga con id: ". $leagueId .' no ha sido encontrada.'); - } - return [ - 'leagueObj' => $leagueObj, - 'userObj' => $user - ]; - } } \ No newline at end of file