src/Controller/V2/MovieController.php line 316

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Controller\V2;
  3. use App\Helper\SessionHelper;
  4. use App\Repository\LocationRepository;
  5. use App\Repository\MovieFiltersRepository;
  6. use App\Repository\MovieRepositoryInterface as MovieRepository;
  7. use App\Repository\ScheduledMovieRepository;
  8. use Nelmio\ApiDocBundle\Annotation\Model;
  9. use FOS\RestBundle\Controller\Annotations as Rest;
  10. use Swagger\Annotations as SWG;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use App\Repository\EventRepository;
  13. /**
  14.  * @Rest\Route("/movies")
  15.  * @SWG\Tag(name="Movie_v2")
  16.  */
  17. class MovieController extends BaseMovieController
  18. {
  19.     /** @var MovieRepository */
  20.     private $movieRepository;
  21.     /** @var EventRepository */
  22.     protected $eventRepository;
  23.     public function __construct(
  24.         MovieRepository $movieRepository,
  25.         EventRepository $eventRepository,
  26.         LocationRepository $locationRepository,
  27.         SessionHelper $sessionHelper,
  28.         MovieFiltersRepository $movieFiltersRepository,
  29.         ScheduledMovieRepository $scheduledMovieRepository
  30.     ) {
  31.         parent::__construct($locationRepository$sessionHelper$movieFiltersRepository$scheduledMovieRepository);
  32.         $this->movieRepository $movieRepository;
  33.         $this->eventRepository $eventRepository;
  34.     }
  35.     /**
  36.      * @param array $films
  37.      * @return array
  38.      * @throws \Exception
  39.      */
  40.     private function prepareResponse(array $films = [])
  41.     {
  42.         $result = [];
  43.         foreach ($films as $film) {
  44.             $result[] = $this->prepareFilm($film);
  45.         }
  46.         return $result;
  47.     }
  48.     /**
  49.      * @param array $films
  50.      * @param array $events
  51.      * @return array
  52.      * @throws \Exception
  53.      */
  54.     private function prepareMovieEventResponse(array $films = [], array $events = [])
  55.     {
  56.         $result = [];
  57.         foreach ($films as $index => $film) {
  58.             if (isset($events[$index 1])) {
  59.                 foreach ($events[$index 1] as $event) {
  60.                     $result[] = $event;
  61.                 }
  62.             }
  63.             $result[] = $this->prepareFilm($film);
  64.         }
  65.         return $result;
  66.     }
  67.     /**
  68.      * @Rest\Route("", methods={"GET"})
  69.      * @Rest\QueryParam(name="filter", map=true, allowBlank=true)
  70.      * @Rest\QueryParam(name="order", allowBlank=true)
  71.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  72.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  73.      * @Rest\QueryParam(name="location", allowBlank=true)
  74.      * @Rest\QueryParam(name="cinema", allowBlank=true)
  75.      * @Rest\QueryParam(name="date", allowBlank=true)
  76.      * @Rest\View(serializerGroups={"Default", "movie_list"})
  77.      *
  78.      * @SWG\Response(
  79.      *     response="200",
  80.      *     description="All movies",
  81.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Film::class))))
  82.      *
  83.      * @param $filter
  84.      * @param $order
  85.      * @param $limit
  86.      * @param $offset
  87.      * @param $location
  88.      * @param $cinema
  89.      * @param \DateTime|null $date
  90.      * @return array
  91.      * @throws \Exception
  92.      */
  93.     public function indexAction($filter$order$limit$offset$location$cinema, ?\DateTime $date)
  94.     {
  95.         [
  96.             $filter,
  97.             $order,
  98.             $limit,
  99.             $offset,
  100.         ] = $this->prepareInput(
  101.             $filter,
  102.             $order,
  103.             $limit,
  104.             $offset,
  105.             $location,
  106.             $cinema,
  107.             $date,
  108.             true
  109.         );
  110.         $events $this->eventRepository->movieEvents();
  111.         return $this->prepareMovieEventResponse($this->movieRepository->search($filter$order$limit$offset), $events);
  112.     }
  113.     /**
  114.      * @Rest\Route("/top", methods={"GET"})
  115.      * @Rest\QueryParam(name="filter", map=true, allowBlank=true)
  116.      * @Rest\QueryParam(name="order", allowBlank=true)
  117.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  118.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  119.      * @Rest\QueryParam(name="location", allowBlank=true)
  120.      * @Rest\QueryParam(name="date", allowBlank=true)
  121.      * @Rest\View(serializerGroups={"Default", "movie_list"})
  122.      *
  123.      * @SWG\Response(
  124.      *     response=200,
  125.      *     description="Top movies",
  126.      *         @SWG\Schema(type="array", items=@SWG\Items(@Model(type=\App\Entity\Vista\Film::class))))
  127.      *
  128.      * @param $filter
  129.      * @param $order
  130.      * @param $limit
  131.      * @param $offset
  132.      * @param $location
  133.      * @param \DateTime|null $date
  134.      * @return array
  135.      * @throws \Exception
  136.      */
  137.     public function topAction($filter$order$limit$offset$location, ?\DateTime $date)
  138.     {
  139.         [$filter$order$limit$offset] = $this->prepareInput(
  140.             $filter,
  141.             $order,
  142.             $limit,
  143.             $offset,
  144.             $location,
  145.             null,
  146.             $date,
  147.             true
  148.         );
  149.         $filter['top'] = true;
  150.         return $this->prepareResponse($this->movieRepository->search(
  151.             $filter ?: [],
  152.             $order ?: null,
  153.             $limit ?: null,
  154.             $offset ?: null
  155.         ));
  156.     }
  157.     /**
  158.      * @Rest\Route("/coming-soon", methods={"GET"})
  159.      * @Rest\QueryParam(name="filter", map=true, allowBlank=true)
  160.      * @Rest\QueryParam(name="order", allowBlank=true)
  161.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  162.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  163.      * @Rest\QueryParam(name="location", allowBlank=true)
  164.      * @Rest\QueryParam(name="date", allowBlank=true)
  165.      * @Rest\View(serializerGroups={"Default", "movie_list"})
  166.      *
  167.      * @SWG\Response(
  168.      *     response=200,
  169.      *     description="Coming soon movies",
  170.      *         @SWG\Schema(type="array", items=@SWG\Items(@Model(type=\App\Entity\Vista\Film::class))))
  171.      *
  172.      * @param $filter
  173.      * @param $order
  174.      * @param $limit
  175.      * @param $offset
  176.      * @param $location
  177.      * @param \DateTimeImmutable|null $date
  178.      * @return array
  179.      * @throws \Exception
  180.      */
  181.     public function comingSoonAction($filter$order$limit$offset$location, ?\DateTimeImmutable $date)
  182.     {
  183.         [$filter$order$limit$offset] = $this->prepareInput($filter$order$limit$offset$locationnull$date);
  184.         unset($filter['cinemaId']);
  185.         $filter['>=openingDate'] = new \DateTimeImmutable('now');
  186.         if ($date) {
  187.             unset($filter['date']);
  188.             $filter['>=openingDate'] = $date;
  189.             $date and $filter['<openingDate'] = $date->modify('+1 month');
  190.         }
  191.         return $this->prepareResponse($this->movieRepository->search(
  192.             $filter ?: [],
  193.             $order ?: null,
  194.             $limit ?: null,
  195.             $offset ?: null,
  196.             ['coming-soon']
  197.         ));
  198.     }
  199.     /**
  200.      * @Rest\Route("/{id}/sessions", methods={"GET"})
  201.      * @Rest\QueryParam(name="filter", map=true, allowBlank=true)
  202.      * @Rest\QueryParam(name="order", allowBlank=true)
  203.      * @Rest\QueryParam(name="limit", requirements="\d+", allowBlank=true)
  204.      * @Rest\QueryParam(name="offset", requirements="\d+", allowBlank=true)
  205.      * @Rest\QueryParam(name="location", allowBlank=true)
  206.      * @Rest\QueryParam(name="cinema", allowBlank=true)
  207.      * @Rest\QueryParam(name="date", allowBlank=true)
  208.      * @Rest\View(serializerGroups={"Default", "session_list"})
  209.      *
  210.      * @SWG\Response(
  211.      *     response="200",
  212.      *     description="Get list of sessions by movie id",
  213.      *         @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Session::class))))
  214.      *
  215.      * @param Request        $request
  216.      * @param                $id
  217.      * @param array          $filter
  218.      * @param array          $order
  219.      * @param int            $limit
  220.      * @param int            $offset
  221.      * @param null           $location
  222.      * @param null           $cinema
  223.      * @param \DateTime|null $date
  224.      *
  225.      * @return array
  226.      * @throws \Exception
  227.      */
  228.     public function sessionsAction(
  229.         Request $request,
  230.         $id,
  231.         $filter = [],
  232.         $order = [],
  233.         $limit 0,
  234.         $offset 0,
  235.         $location null,
  236.         $cinema null,
  237.         ?\DateTime $date null
  238.     ) {
  239.         $filter or $filter = [];
  240.         [$filter$order$limit$offset] = $this->prepareInput($filter$order$limit$offset$location$cinemanull);
  241.         if (isset($filter['date'])) {
  242.             unset($filter['date']);
  243.         }
  244.         $filter['scheduledFilm'] = $this->scheduledMovieRepository->fetchMovieByIdHOFilmCode($id);
  245.         if ('mobile' === $request->attributes->get('_platform')) {
  246.             if ($date) {
  247.                 $filter['>=showtime'] = $date->setTime(000);
  248.                 $filter['<=showtime'] = (clone $date)->add(new \DateInterval('P1D'));
  249.             }
  250.         } else {
  251.             $date or $date $this->sessionHelper->getDefaultDate();
  252.             is_string($date) and $date = \DateTime::createFromFormat('Y-m-d'$date);
  253.             if ($date) {
  254.                 $filter['>=showtime'] = $date;
  255.                 $filter['<=showtime'] = (clone $date)
  256.                     ->setTime(000)
  257.                     ->add(new \DateInterval('P1D'));
  258.             }
  259.         }
  260.         $response $this->prepareSessionsResponse($this->sessionHelper->findBy(
  261.             $filter ?: [],
  262.             $order ?: null,
  263.             $limit ?: null,
  264.             $offset ?: null
  265.         ));
  266.         return $this->filterSessionByType($response'CELL');
  267.     }
  268.     /**
  269.      * @Rest\Route("/filters/dates/list", methods={"GET"})
  270.      * @Rest\QueryParam(name="id", allowBlank=true, strict=true, nullable=true)
  271.      * @Rest\QueryParam(name="cinemaId", allowBlank=true, requirements="\d+", strict=true, nullable=true)
  272.      * @Rest\QueryParam(name="location", allowBlank=true, nullable=true)
  273.      * @Rest\QueryParam(name="top", allowBlank=true, strict=true, nullable=true)
  274.      * @Rest\QueryParam(name="comingSoon", allowBlank=true, strict=true, nullable=true)
  275.      * @Rest\View()
  276.      *
  277.      * @SWG\Response(
  278.      *     response="200",
  279.      *     description="Success",
  280.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Session::class))))
  281.      *
  282.      * @param string|null $id
  283.      * @param string|null $cinemaId
  284.      * @param int|null    $location
  285.      * @param bool|null   $top
  286.      * @param bool|null   $comingSoon
  287.      *
  288.      * @return array
  289.      */
  290.     public function filtersDatesListAction(
  291.         ?string $id null,
  292.         ?string $cinemaId null,
  293.         $location null,
  294.         ?bool $top null,
  295.         ?bool $comingSoon null
  296.     ) {
  297.         if (!is_numeric($location)) {
  298.             $location null;
  299.         } else {
  300.             $location = (int) $location;
  301.         }
  302.         return $this->sessionHelper->getFiltersDatesListV2(
  303.             $id,
  304.             $cinemaId ? [$cinemaId] : null,
  305.             $location,
  306.             $top,
  307.             $comingSoon
  308.         );
  309.     }
  310.     /**
  311.      * @Rest\Route("/filters/months/list", methods={"GET"})
  312.      * @Rest\QueryParam(name="id", allowBlank=true, strict=true, nullable=true)
  313.      * @Rest\QueryParam(name="comingSoon", allowBlank=true, strict=true, nullable=true)
  314.      * @Rest\View()
  315.      *
  316.      * @SWG\Response(
  317.      *     response="200",
  318.      *     description="Success",
  319.      *     @SWG\Schema(type="array", items=@SWG\Items(ref=@Model(type=\App\Entity\Vista\Film::class))))
  320.      *
  321.      * @param string|null $id
  322.      * @param bool|null   $comingSoon
  323.      *
  324.      * @return array
  325.      */
  326.     public function filtersMonthListAction(?string $id null, ?bool $comingSoon false)
  327.     {
  328.         $filter array_filter([
  329.             'id' => $id,
  330.         ]);
  331.         $flags array_filter([
  332.             $comingSoon 'coming-soon' null,
  333.         ]);
  334.         return $this->movieRepository->findMonthsBy($filternullnullnull$flags);
  335.     }
  336. }