src/Controller/V1/EventsController.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\V1;
  4. use App\Entity\Getmesh\Event;
  5. use App\Entity\Vista\DTO\SessionsByDateDTO;
  6. use App\Helper\SessionHelper;
  7. use App\Repository\EventRepository;
  8. use App\Repository\LocationRepository;
  9. use FOS\RestBundle\Controller\Annotations as Rest;
  10. use FOS\RestBundle\Controller\Annotations\View;
  11. use Nelmio\ApiDocBundle\Annotation\Model;
  12. use Swagger\Annotations as SWG;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @Route("/events")
  17.  * @SWG\Tag(name="Event_v1")
  18.  */
  19. class EventsController
  20. {
  21.     use ResponseTrait;
  22.     /** @var EventRepository */
  23.     protected $eventRepository;
  24.     /** @var SessionHelper */
  25.     protected $sessionHelper;
  26.     /** @var LocationRepository */
  27.     protected $locationRepository;
  28.     public function __construct(
  29.         EventRepository $eventRepository,
  30.         SessionHelper $sessionHelper,
  31.         LocationRepository $locationRepository
  32.     ) {
  33.         $this->eventRepository $eventRepository;
  34.         $this->sessionHelper $sessionHelper;
  35.         $this->locationRepository $locationRepository;
  36.     }
  37.     /**
  38.      * @Route("", methods={"GET"})
  39.      * @Rest\View(serializerGroups={"Default", "event_list"})
  40.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  41.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  42.      * @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true)
  43.      * @Rest\QueryParam(name="date", allowBlank=true)
  44.      *
  45.      * @SWG\Response(
  46.      *     response="200",
  47.      *     description="Success",
  48.      *     @SWG\Schema(items=@SWG\Items(ref=@Model(type=\App\Entity\Getmesh\Event::class))))
  49.      *
  50.      * @param int                     $limit
  51.      * @param int                     $offset
  52.      * @param int                     $location
  53.      * @param \DateTimeImmutable|null $date
  54.      *
  55.      * @return array
  56.      * @throws \Exception
  57.      */
  58.     public function indexAction(
  59.         $limit 0,
  60.         $offset 0,
  61.         $location null,
  62.         ?\DateTimeImmutable $date null
  63.     ) {
  64.         if (is_numeric($location)) {
  65.             $cinemaIds $this->locationRepository->findById($location)->getItems();
  66.         } else {
  67.             $cinemaIds $this->locationRepository->findLocationIds();
  68.         }
  69.         return $this->eventRepository->search(
  70.             $date,
  71.             $cinemaIds,
  72.             $limit ?: null,
  73.             $offset ?: null
  74.         );
  75.     }
  76.     /**
  77.      * @Route("/{id}", methods={"GET"})
  78.      * @Rest\View()
  79.      *
  80.      * @SWG\Response(
  81.      *     response="200",
  82.      *     description="Success",
  83.      *     @Model(type=\App\Entity\Getmesh\Event::class))
  84.      *
  85.      * @param $id
  86.      *
  87.      * @return Event|object
  88.      */
  89.     public function getAction($id)
  90.     {
  91.         if (null === ($item $this->eventRepository->findByIdOrEventUrlName($id))) {
  92.             throw new NotFoundHttpException(sprintf('Event %s doesn\'t exists'$id));
  93.         }
  94.         return $item;
  95.     }
  96.     /**
  97.      * @param SessionsByDateDTO[] $sessions
  98.      *
  99.      * @return array
  100.      */
  101.     private function prepareSessionsResponse(array $sessions): array
  102.     {
  103.         $response = [];
  104.         foreach ($sessions as $date) {
  105.             $sessions = [];
  106.             foreach ($date->getSessions() as $session) {
  107.                 $sessionResponse $this->prepareSession($session);
  108.                 $sessionResponse['scheduledFilm'] = [
  109.                     'id' => $session->getScheduledFilm()->getId(),
  110.                     'cinemaId' => $session->getScheduledFilm()->getCinemaId(),
  111.                     'corporateId' => $session->getScheduledFilm()->getCorporateId(),
  112.                     'title' => $session->getScheduledFilm()->getTitle(),
  113.                     'film' => [
  114.                         'id' => $session->getScheduledFilm()->getFilm()->getId(),
  115.                         'HOFilmCode' => $session->getScheduledFilm()->getFilm()->getHOFilmCode(),
  116.                         'corporateFilmId' => $session->getScheduledFilm()->getFilm()->getCorporateFilmId(),
  117.                         'titleCalculated' => $session->getScheduledFilm()->getFilm()->getTitleCalculated(),
  118.                         'titleOriginalCalculated' => $session->getScheduledFilm()->getFilm()->getTitleOriginalCalculated(),
  119.                         'posterImage' => $session->getScheduledFilm()->getFilm()->getPosterImageForResponse(),
  120.                     ]
  121.                 ];
  122.                 $sessions[] = $sessionResponse;
  123.             }
  124.             $response[] = [
  125.                 'date' => $date->getDate(),
  126.                 'sessions' => $sessions
  127.             ];
  128.         }
  129.         return $response;
  130.     }
  131.     /**
  132.      * @Route("/{id}/sessions", methods={"GET"})
  133.      * @Rest\QueryParam(name="filter", allowBlank=true)
  134.      * @Rest\QueryParam(name="order", allowBlank=true)
  135.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  136.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  137.      * @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true)
  138.      * @Rest\View(serializerGroups={"Default", "event_details"})
  139.      *
  140.      * @SWG\Parameter(name="filter", type="string", in="query")
  141.      * @SWG\Parameter(name="order", type="string", in="query")
  142.      *
  143.      * @SWG\Response(
  144.      *     response="200",
  145.      *     description="Success",
  146.      *     @Model(type=\App\Entity\Vista\Session::class))
  147.      *
  148.      * @param int|string   $id
  149.      * @param array $filter
  150.      * @param array $order
  151.      * @param int   $limit
  152.      * @param int   $offset
  153.      * @param int   $location
  154.      *
  155.      * @return array
  156.      */
  157.     public function sessionsAction($id$filter = [], $order = [], $limit 0$offset 0$location null)
  158.     {
  159.         $filter or $filter = [];
  160.         $event $this->getAction($id);
  161.         $filter['cinemaId'] = is_numeric($location)
  162.             ? $this->locationRepository->findById($location)->getItems()
  163.             : $this->locationRepository->findLocationIds();
  164.         $filter['event'] = $event;
  165.         return $this->prepareSessionsResponse($this->sessionHelper->findBy(
  166.             $filter,
  167.             $order ?: null,
  168.             $limit ?: null,
  169.             $offset ?: null,
  170.             true
  171.         ));
  172.     }
  173.     /**
  174.      * @Route("/filters/months/list", methods={"GET"})
  175.      * @View()
  176.      *
  177.      * @SWG\Response(
  178.      *     response="200",
  179.      *     description="Success",
  180.      *     @SWG\Schema(type="array", items=@SWG\Items(type="string")))
  181.      */
  182.     public function filtersMonthsListAction()
  183.     {
  184.         $thisMonth = (new \DateTime())->modify('first day of this month')->setTime(000);
  185.         $dates = [];
  186.         $events $this->eventRepository->search();
  187.         /** @var Event $event */
  188.         foreach ($events as $event) {
  189.             if (!$event->getShowEndDate() || !$event->getShowStartDate()) {
  190.                 continue;
  191.             }
  192.             $range = new \DatePeriod(
  193.                 $event->getShowStartDate()->modify('first day of this month')->setTime(000),
  194.                 new \DateInterval('P1M'),
  195.                 $event->getShowEndDate()->modify('last day of this month')->setTime(000)
  196.             );
  197.             /** @var \DateTime $month */
  198.             foreach ($range as $month) {
  199.                 ($month >= $thisMonth) and $dates[] = $month->format('Y-m-d\TH:i:sP');
  200.             }
  201.         }
  202.         return array_values(array_unique($dates));
  203.     }
  204. }