vendor/tattali/calendar-bundle/src/Controller/CalendarController.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CalendarBundle\Controller;
  4. use CalendarBundle\CalendarEvents;
  5. use CalendarBundle\Event\CalendarEvent;
  6. use CalendarBundle\Serializer\SerializerInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
  11. class CalendarController
  12. {
  13.     /**
  14.      * @var SerializerInterface
  15.      */
  16.     protected $serializer;
  17.     /**
  18.      * @var EventDispatcherInterface
  19.      */
  20.     protected $eventDispatcher;
  21.     public function __construct(
  22.         EventDispatcherInterface $eventDispatcher,
  23.         SerializerInterface $serializer
  24.     ) {
  25.         $this->eventDispatcher $eventDispatcher;
  26.         $this->serializer $serializer;
  27.     }
  28.     public function loadAction(Request $request): Response
  29.     {
  30.         $start = new \DateTime($request->get('start'));
  31.         $end = new \DateTime($request->get('end'));
  32.         $filters $request->get('filters''{}');
  33.         $filters \is_array($filters) ? $filters json_decode($filterstrue);
  34.         $event $this->dispatchWithBC(
  35.             new CalendarEvent($start$end$filters),
  36.             CalendarEvents::SET_DATA
  37.         );
  38.         $content $this->serializer->serialize($event->getEvents());
  39.         $response = new Response();
  40.         $response->headers->set('Content-Type''application/json');
  41.         $response->setContent($content);
  42.         $response->setStatusCode(empty($content) ? Response::HTTP_NO_CONTENT Response::HTTP_OK);
  43.         return $response;
  44.     }
  45.     public function dispatchWithBC($event, ?string $eventName null)
  46.     {
  47.         if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
  48.             return $this->eventDispatcher->dispatch($event$eventName);
  49.         }
  50.         return $this->eventDispatcher->dispatch($eventName$event);
  51.     }
  52. }