Refactor
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good
Details
dyb-tech.com/LaLiga-BackEnd/pipeline/head This commit looks good
Details
This commit is contained in:
parent
85e8efbed9
commit
e2a4c6f3bd
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue