src/Controller/V1/CinemaController.php line 104

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\V1;
  4. use App\Entity\Local\CinemaAreaCategory;
  5. use App\Entity\Local\Response\CinemasWithMoviesItem;
  6. use App\Entity\Vista\Cinema;
  7. use App\Entity\Vista\Session;
  8. use App\Helper\SessionHelper;
  9. use App\Helper\UserHelper;
  10. use App\Repository\CinemaAreaCategoryRepository;
  11. use App\Repository\CinemaRepository;
  12. use App\Repository\LocationRepository;
  13. use App\Repository\MovieRepositoryInterface as MovieRepository;
  14. use App\Repository\RestrictionsRepository;
  15. use App\Repository\SeatPlanRepository;
  16. use App\Repository\TicketRepository;
  17. use FOS\RestBundle\Controller\Annotations as Rest;
  18. use FOS\RestBundle\Controller\Annotations\View;
  19. use Nelmio\ApiDocBundle\Annotation\Model;
  20. use Swagger\Annotations as SWG;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use App\Repository\SessionRepositoryInterface as SessionRepository;
  24. use App\Repository\SeatPlanLocalRepository;
  25. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  26. /**
  27.  * @Route("/cinemas")
  28.  * @SWG\Tag(name="Cinema_v1")
  29.  */
  30. class CinemaController extends BaseCinemaController
  31. {
  32.     /** @var TicketRepository */
  33.     protected $ticketRepository;
  34.     /** @var UserHelper */
  35.     protected $userHelper;
  36.     /** @var CinemaAreaCategoryRepository */
  37.     protected $areaCategoryRepository;
  38.     /** @var SeatPlanRepository */
  39.     protected $seatPlanRepository;
  40.     /** @var RestrictionsRepository */
  41.     protected $restrictionsRepository;
  42.     /** @var SessionRepository */
  43.     private $sessionRepository;
  44.     /** @var SeatPlanLocalRepository */
  45.     protected $seatPlanLocalRepository;
  46.     public function __construct(
  47.         SessionRepository $sessionRepository,
  48.         CinemaRepository $cinemaRepository,
  49.         MovieRepository $movieRepository,
  50.         SessionHelper $sessionHelper,
  51.         LocationRepository $locationRepository,
  52.         CinemaAreaCategoryRepository $areaCategoryRepository,
  53.         SeatPlanRepository $seatPlanRepository,
  54.         UserHelper $userHelper,
  55.         RestrictionsRepository $restrictionsRepository,
  56.         SeatPlanLocalRepository $seatPlanLocalRepository,
  57.         TicketRepository $ticketRepository
  58.     ) {
  59.         parent::__construct($locationRepository$cinemaRepository$movieRepository$sessionHelper);
  60.         $this->sessionRepository $sessionRepository;
  61.         $this->userHelper $userHelper;
  62.         $this->areaCategoryRepository $areaCategoryRepository;
  63.         $this->seatPlanRepository $seatPlanRepository;
  64.         $this->restrictionsRepository $restrictionsRepository;
  65.         $this->seatPlanLocalRepository $seatPlanLocalRepository;
  66.         $this->ticketRepository $ticketRepository;
  67.     }
  68.     /**
  69.      * @Route("", methods="GET")
  70.      * @Rest\QueryParam(name="filter", allowBlank=true)
  71.      * @Rest\QueryParam(name="order", allowBlank=true)
  72.      * @Rest\QueryParam(name="limit", default="0", requirements="\d+", allowBlank=true)
  73.      * @Rest\QueryParam(name="offset", default="0", requirements="\d+", allowBlank=true)
  74.      * @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true)
  75.      * @View(serializerGroups={"Default", "cinema_list"})
  76.      *
  77.      * @SWG\Parameter(name="filter", type="string", in="query")
  78.      * @SWG\Parameter(name="order", type="string", in="query")
  79.      *
  80.      * @SWG\Response(
  81.      *     response="200",
  82.      *     description="Returns a list of cinemas",
  83.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Cinema::class))))
  84.      *
  85.      * @param array $filter
  86.      * @param array $order
  87.      * @param int $limit
  88.      * @param int $offset
  89.      * @param int $location
  90.      *
  91.      * @return Cinema[]
  92.      */
  93.     public function indexAction($filter = [], $order = [], $limit null$offset null$location null)
  94.     {
  95.         $filter or $filter = [];
  96.         if (is_numeric($location)) {
  97.             $filter['id'] = $this->locationRepository->findById($location)->getItems();
  98.         } else {
  99.             $filter['id'] = $this->locationRepository->findLocationIds();
  100.         }
  101.         $cinemas $this->cinemaRepository->findBy($filter$order ?: [], $limit ?: null$offset ?: null);
  102.         $response = [];
  103.         foreach ($cinemas as $cinema) {
  104.             $response[] = $this->prepareCinema($cinema);
  105.         }
  106.         return $response;
  107.     }
  108.     /**
  109.      * @Route("/with-movies", methods={"GET"})
  110.      * @Rest\QueryParam(name="locationId", allowBlank=true, nullable=true)
  111.      * @Rest\QueryParam(name="date", allowBlank=true)
  112.      * @Rest\QueryParam(name="order", allowBlank=true)
  113.      * @View(serializerGroups={"Default", "cinema_list"})
  114.      *
  115.      * @SWG\Response(
  116.      *     response="200",
  117.      *     description="Returns a list of cinemas",
  118.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Local\Response\CinemasWithMoviesItem::class))))
  119.      *
  120.      * @param int|null       $locationId
  121.      * @param \DateTime|null $date
  122.      * @return array
  123.      * @throws \Exception
  124.      */
  125.     public function cinemasWithMoviesAction($locationId null, ?\DateTime $date null)
  126.     {
  127.         if (!is_numeric($locationId)) {
  128.             $locationId null;
  129.         } else {
  130.             $locationId = (int) $locationId;
  131.         }
  132.         $filter = [];
  133.         if (null !== $locationId) {
  134.             $filter['id'] = $this->locationRepository->findById($locationId)->getItems();
  135.         } else {
  136.             $filter['id'] = $this->locationRepository->findLocationIds();
  137.         }
  138.         $defaultDate $this->sessionHelper->getDefaultDate(null$locationId);
  139.         if (!$date) {
  140.             $date $defaultDate;
  141.         } elseif ($date $defaultDate) {
  142.             $date $defaultDate;
  143.         }
  144.         return $this->prepareResponse($this->getCinemasWithMovies('CELL'$filter$date));
  145.     }
  146.     /**
  147.      * @Route("/{id}", methods={"GET"})
  148.      * @View(serializerGroups={"Default", "cinema_details"})
  149.      *
  150.      * @SWG\Response(
  151.      *     response="200",
  152.      *     description="Get cinema by id",
  153.      *     @Model(type=\App\Entity\Vista\Cinema::class))
  154.      * @SWG\Response(
  155.      *     response="404",
  156.      *     description="Cinema not found")
  157.      *
  158.      * @param $id
  159.      * @return array
  160.      */
  161.     public function showAction($id): array
  162.     {
  163.         return $this->prepareCinema($this->getCinema($id));
  164.     }
  165.     /**
  166.      * @Route("/{id}/price-list", methods={"GET"})
  167.      * @View()
  168.      *
  169.      * @SWG\Response(
  170.      *     response="200",
  171.      *     description="Get cinema price list by id",
  172.      *     @Model(type=\App\Entity\Vista\Cinema::class))
  173.      * @SWG\Response(
  174.      *     response="404",
  175.      *     description="Cinema not found")
  176.      *
  177.      * @param string $id
  178.      *
  179.      * @return Response
  180.      */
  181.     public function priceListAction(string $id): Response
  182.     {
  183.         /** @var Cinema $cinema */
  184.         if (!$cinema $this->cinemaRepository->find($id)) {
  185.             throw new NotFoundHttpException();
  186.         }
  187.         $response = new Response($cinema->getInfo() ?? '');
  188.         $response->headers->set('Content-type''text/html');
  189.         return $response;
  190.     }
  191.     /**
  192.      * @Route("/{id}/movies", methods={"GET"})
  193.      * @Rest\QueryParam(name="date", allowBlank=true)
  194.      * @View(serializerGroups={"Default", "movie_details"})
  195.      *
  196.      * @SWG\Response(
  197.      *     response="200",
  198.      *     description="Get upcoming movies list by cinema id",
  199.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\ScheduledFilm::class))))
  200.      * @SWG\Response(
  201.      *     response="404",
  202.      *     description="Cinema not found")
  203.      *
  204.      * @param $id
  205.      * @param \DateTime|null $date
  206.      * @return array
  207.      * @throws \Exception
  208.      */
  209.     public function moviesAction($id, ?\DateTime $date null)
  210.     {
  211.         $cinema $this->getCinema($id);
  212.         $defaultDate $this->sessionHelper->getDefaultDate([$cinema->getId()]);
  213.         if (!$date || ($date $defaultDate)) {
  214.             $date $defaultDate;
  215.         }
  216.         $response $this->prepareMoviesResponse(
  217.             $this->movieRepository->findByCinemaIdAndDate($cinema->getId(), $date)
  218.         );
  219.         return $this->filterSessionByType($response'CELL');
  220.     }
  221.     /**
  222.      * @Route("/{id}/sessions", methods={"GET"})
  223.      * @Rest\QueryParam(name="filter", allowBlank=true)
  224.      * @Rest\QueryParam(name="order", allowBlank=true)
  225.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true, strict=true, nullable=true)
  226.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true, strict=true, nullable=true)
  227.      * @Rest\QueryParam(name="location", requirements="\d+", allowBlank=true, strict=true, nullable=true)
  228.      * @Rest\QueryParam(name="date", allowBlank=true)
  229.      * @Rest\View(serializerGroups={"Default", "session_details"})
  230.      *
  231.      * @SWG\Parameter(name="filter", type="string", in="query")
  232.      * @SWG\Parameter(name="order", type="string", in="query")
  233.      *
  234.      * @SWG\Response(
  235.      *     response="200",
  236.      *     description="Get list of sessions by movie id",
  237.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Session::class))))
  238.      * @SWG\Response(
  239.      *     response="404",
  240.      *     description="Cinema not found")
  241.      *
  242.      * @param int|string            $id
  243.      * @param array          $filter
  244.      * @param array          $order
  245.      * @param int|null       $limit
  246.      * @param int|null       $offset
  247.      * @param \DateTime|null $date
  248.      *
  249.      * @return Session[]|array
  250.      * @throws \Exception
  251.      */
  252.     public function sessionsAction(
  253.         $id,
  254.         $filter = [],
  255.         $order = [],
  256.         ?int $limit null,
  257.         ?int $offset null,
  258.         ?\DateTime $date null
  259.     ) {
  260.         $filter or $filter = [];
  261.         $cinema $this->getCinema($id);
  262.         $filter['cinemaId'] = $cinema->getId();
  263.         if ($date) {
  264.             $filter['>=showtime'] = $date->setTime(000);
  265.             $filter['<=showtime'] = (clone $date)->add(new \DateInterval('P1D'));
  266.         }
  267.         return $this->prepareSessionsResponse($this->sessionHelper->findBy(
  268.             $filter ?: [],
  269.             $order ?: null,
  270.             $limit,
  271.             $offset
  272.         ));
  273.     }
  274.     /**
  275.      * @Route("/{cinemaId}/sessions/{sessionId}/tickets", methods={"GET"})
  276.      * @View()
  277.      *
  278.      * @SWG\Response(
  279.      *     response="200",
  280.      *     description="Get available tickets by cinema id and session id",
  281.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\SessionTicketType::class))))
  282.      *
  283.      * @param $cinemaId
  284.      * @param $sessionId
  285.      * @return \App\Entity\Vista\SessionTicketType[]
  286.      */
  287.     public function sessionsTicketsAction($cinemaId$sessionId)
  288.     {
  289.         return $this->ticketRepository->findByCinemaIdSessionId($cinemaId$sessionId);
  290.     }
  291.     /**
  292.      * @Route("/{cinemaId}/sessions/{sessionId}/area-categories", methods={"GET"})
  293.      * @View()
  294.      *
  295.      * @SWG\Response(
  296.      *     response="200",
  297.      *     description="Success",
  298.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Local\CinemaAreaCategory::class))))
  299.      *
  300.      * @param string $cinemaId
  301.      * @param string $sessionId
  302.      * @return CinemaAreaCategory[]
  303.      */
  304.     public function areaCategoriesAction(string $cinemaIdstring $sessionId)
  305.     {
  306.         $categories array_values($this->areaCategoryRepository->find($cinemaId) ?: []);
  307.         [, $seatPlanRows] = $this->seatPlanRepository->getSeatPlan($cinemaId$sessionId$categories);
  308.         return $this->areaCategoryRepository->filterBySeatPlan($categories$seatPlanRows);
  309.     }
  310.     /**
  311.      * @Route("/list/short", methods="GET")
  312.      * @View(serializerGroups={"Default", "cinema_list_short"})
  313.      *
  314.      * @SWG\Response(response="200",
  315.      *     description="Success")
  316.      * @SWG\Response(
  317.      *     response="404",
  318.      *     description="Cinema not found")
  319.      *
  320.      * @return Response
  321.      */
  322.     public function allCinemaList() {
  323.         $filter = ['id' => $this->locationRepository->findLocationIds()];
  324.         $cinemas $this->cinemaRepository->findBy($filter, ['name' => 'ASC']);
  325.         $json '';
  326.         foreach ($cinemas as $cinema) {
  327.             if ($json) { $json .= ','; }
  328.             $json .= "{\"id\":\"{$cinema->getId()}\",\"name\":\"{$cinema->getName()}\"}";
  329.         }
  330.         $response = new Response("[$json]");
  331.         $response->headers->set('Content-Type''application/json; charset=UTF-8');
  332.         return $response;
  333.     }
  334.     /**
  335.      * @Route("/halls/{cinemaId}", methods="GET")
  336.      * @View()
  337.      *
  338.      * @SWG\Response(response=200,
  339.      *     description="Success")
  340.      * @SWG\Response(
  341.      *     response="404",
  342.      *     description="Cinema not found")
  343.      *
  344.      * @param string      $cinemaId
  345.      * @return Response
  346.      */
  347.     public function getSavedCinemaHalls(string $cinemaId) {
  348.         $sessions $this->sessionRepository->findScreens($cinemaId);
  349.         //error_log(sprintf("%s %s(%s) ", date('Y-m-d H:i:s'), __METHOD__, __LINE__) . ' sessions ' . var_export($sessions, true));
  350.         $json '';
  351.         if ($sessions) {
  352.             $seatPlanCinemaHalls $this->seatPlanLocalRepository->findBy(['cinemaId' => $cinemaId], ['screenNumber' => 'ASC']);
  353.             $halls = [];
  354.             foreach($seatPlanCinemaHalls as $hall) {
  355.                 $halls[$hall->getScreenNumber()] = null;
  356.             }
  357.             foreach ($sessions as $session) { // $session->getShowtime()
  358.                 if ($json) { $json .= ','; }
  359.                 $seatPlanSaved array_key_exists($session['screenNumber'], $halls) ? 0;
  360.                 $json .= "{\"cinemaId\":\"{$session['cinemaId']}\",\"screenNumber\":\"{$session['screenNumber']}\",\"screenName\":\"{$session['screenName']}\",\"seatPlanSaved\":\"$seatPlanSaved\"}";
  361.             }
  362.         }
  363.         $response = new Response("[$json]");
  364.         $response->headers->set('Content-Type''application/json; charset=UTF-8');
  365.         return $response;
  366.     }
  367. }