LaLiga-BackEnd/src/Service/Season/getAllSeasons/HandleGetAllSeason.php

47 lines
1.3 KiB
PHP

<?php
namespace DMD\LaLigaApi\Service\Season\getAllSeasons;
use DMD\LaLigaApi\Dto\SeasonDto;
use DMD\LaLigaApi\Repository\SeasonRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class HandleGetAllSeason
{
public const PAGE_SIZE = 10;
public function __construct(
public EntityManagerInterface $entityManager,
public SeasonRepository $seasonRepository,
){}
public function __invoke(Request $request, int $page): JsonResponse
{
$seasonCollection = $this->seasonRepository->findBy([
'active' => true
],
limit: 10,
offset: ($page * self::PAGE_SIZE) - self::PAGE_SIZE
);
$seasonArray = [];
if (!is_null($seasonCollection))
{
foreach ($seasonCollection as $seasonObj)
{
$seasonDto = new SeasonDto();
$seasonDto->fillFromEntity($seasonObj);
$seasonArray[] = $seasonDto->createSeasonArray();
}
}
return new JsonResponse(
data: [
'success' => true,
'seasons' => $seasonArray
],
status: Response::HTTP_OK
);
}
}