<?php
declare(strict_types=1);
namespace App\Controller\V1;
use App\Entity\Getmesh\Event;
use App\Entity\Vista\DTO\SessionsByDateDTO;
use App\Helper\SessionHelper;
use App\Repository\EventRepository;
use App\Repository\LocationRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\View;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/events")
* @SWG\Tag(name="Event_v1")
*/
class EventsController
{
use ResponseTrait;
/** @var EventRepository */
protected $eventRepository;
/** @var SessionHelper */
protected $sessionHelper;
/** @var LocationRepository */
protected $locationRepository;
public function __construct(
EventRepository $eventRepository,
SessionHelper $sessionHelper,
LocationRepository $locationRepository
) {
$this->eventRepository = $eventRepository;
$this->sessionHelper = $sessionHelper;
$this->locationRepository = $locationRepository;
}
/**
* @Route("", methods={"GET"})
* @Rest\View(serializerGroups={"Default", "event_list"})
* @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
* @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
* @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true)
* @Rest\QueryParam(name="date", allowBlank=true)
*
* @SWG\Response(
* response="200",
* description="Success",
* @SWG\Schema(items=@SWG\Items(ref=@Model(type=\App\Entity\Getmesh\Event::class))))
*
* @param int $limit
* @param int $offset
* @param int $location
* @param \DateTimeImmutable|null $date
*
* @return array
* @throws \Exception
*/
public function indexAction(
$limit = 0,
$offset = 0,
$location = null,
?\DateTimeImmutable $date = null
) {
if (is_numeric($location)) {
$cinemaIds = $this->locationRepository->findById($location)->getItems();
} else {
$cinemaIds = $this->locationRepository->findLocationIds();
}
return $this->eventRepository->search(
$date,
$cinemaIds,
$limit ?: null,
$offset ?: null
);
}
/**
* @Route("/{id}", methods={"GET"})
* @Rest\View()
*
* @SWG\Response(
* response="200",
* description="Success",
* @Model(type=\App\Entity\Getmesh\Event::class))
*
* @param $id
*
* @return Event|object
*/
public function getAction($id)
{
if (null === ($item = $this->eventRepository->findByIdOrEventUrlName($id))) {
throw new NotFoundHttpException(sprintf('Event %s doesn\'t exists', $id));
}
return $item;
}
/**
* @param SessionsByDateDTO[] $sessions
*
* @return array
*/
private function prepareSessionsResponse(array $sessions): array
{
$response = [];
foreach ($sessions as $date) {
$sessions = [];
foreach ($date->getSessions() as $session) {
$sessionResponse = $this->prepareSession($session);
$sessionResponse['scheduledFilm'] = [
'id' => $session->getScheduledFilm()->getId(),
'cinemaId' => $session->getScheduledFilm()->getCinemaId(),
'corporateId' => $session->getScheduledFilm()->getCorporateId(),
'title' => $session->getScheduledFilm()->getTitle(),
'film' => [
'id' => $session->getScheduledFilm()->getFilm()->getId(),
'HOFilmCode' => $session->getScheduledFilm()->getFilm()->getHOFilmCode(),
'corporateFilmId' => $session->getScheduledFilm()->getFilm()->getCorporateFilmId(),
'titleCalculated' => $session->getScheduledFilm()->getFilm()->getTitleCalculated(),
'titleOriginalCalculated' => $session->getScheduledFilm()->getFilm()->getTitleOriginalCalculated(),
'posterImage' => $session->getScheduledFilm()->getFilm()->getPosterImageForResponse(),
]
];
$sessions[] = $sessionResponse;
}
$response[] = [
'date' => $date->getDate(),
'sessions' => $sessions
];
}
return $response;
}
/**
* @Route("/{id}/sessions", methods={"GET"})
* @Rest\QueryParam(name="filter", allowBlank=true)
* @Rest\QueryParam(name="order", allowBlank=true)
* @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
* @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
* @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true)
* @Rest\View(serializerGroups={"Default", "event_details"})
*
* @SWG\Parameter(name="filter", type="string", in="query")
* @SWG\Parameter(name="order", type="string", in="query")
*
* @SWG\Response(
* response="200",
* description="Success",
* @Model(type=\App\Entity\Vista\Session::class))
*
* @param int|string $id
* @param array $filter
* @param array $order
* @param int $limit
* @param int $offset
* @param int $location
*
* @return array
*/
public function sessionsAction($id, $filter = [], $order = [], $limit = 0, $offset = 0, $location = null)
{
$filter or $filter = [];
$event = $this->getAction($id);
$filter['cinemaId'] = is_numeric($location)
? $this->locationRepository->findById($location)->getItems()
: $this->locationRepository->findLocationIds();
$filter['event'] = $event;
return $this->prepareSessionsResponse($this->sessionHelper->findBy(
$filter,
$order ?: null,
$limit ?: null,
$offset ?: null,
true
));
}
/**
* @Route("/filters/months/list", methods={"GET"})
* @View()
*
* @SWG\Response(
* response="200",
* description="Success",
* @SWG\Schema(type="array", items=@SWG\Items(type="string")))
*/
public function filtersMonthsListAction()
{
$thisMonth = (new \DateTime())->modify('first day of this month')->setTime(0, 0, 0);
$dates = [];
$events = $this->eventRepository->search();
/** @var Event $event */
foreach ($events as $event) {
if (!$event->getShowEndDate() || !$event->getShowStartDate()) {
continue;
}
$range = new \DatePeriod(
$event->getShowStartDate()->modify('first day of this month')->setTime(0, 0, 0),
new \DateInterval('P1M'),
$event->getShowEndDate()->modify('last day of this month')->setTime(0, 0, 0)
);
/** @var \DateTime $month */
foreach ($range as $month) {
($month >= $thisMonth) and $dates[] = $month->format('Y-m-d\TH:i:sP');
}
}
return array_values(array_unique($dates));
}
}