src/Service/EventSubscriber/RoutingFormResultSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\EventSubscriber;
  3. use Harmonizely\CalendarEvents;
  4. use Harmonizely\Entity\Company\RoutingFormResultEntity;
  5. use Harmonizely\Model\Event;
  6. use Harmonizely\Repository\Company\Contract\IRoutingFormResultRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. class RoutingFormResultSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SessionInterface
  14.      */
  15.     private SessionInterface $session;
  16.     /**
  17.      * @var IRoutingFormResultRepository
  18.      */
  19.     private IRoutingFormResultRepository $routingFormResultRepository;
  20.     /**
  21.      * @param SessionInterface $session
  22.      * @param IRoutingFormResultRepository $routingFormResultRepository
  23.      */
  24.     public function __construct(
  25.         SessionInterface $session,
  26.         IRoutingFormResultRepository $routingFormResultRepository
  27.     )
  28.     {
  29.         $this->session $session;
  30.         $this->routingFormResultRepository $routingFormResultRepository;
  31.     }
  32.     /**
  33.      * @return string[]
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CalendarEvents::EVENT_POST_CREATE => 'postCreateEvent',
  39.         ];
  40.     }
  41.     /**
  42.      * @param GenericEvent $genericEvent
  43.      * @return void
  44.      */
  45.     public function postCreateEvent(GenericEvent $genericEvent): void
  46.     {
  47.         /** @var Event $event */
  48.         $event $genericEvent->getSubject();
  49.         $routingFormResultId $this->session->get(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
  50.         if ($routingFormResultId) {
  51.             $routingFormResult $this->routingFormResultRepository->findOneById($routingFormResultId);
  52.             if ($routingFormResult) {
  53.                 $eventType $routingFormResult->getRoutingFormRoute()->getEventType();
  54.                 if ($eventType === $event->getEventType()) {
  55.                     $routingFormResult->setEvent($event);
  56.                     $this->routingFormResultRepository->save($routingFormResult);
  57.                 }
  58.             }
  59.         }
  60.         $this->session->remove(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
  61.     }
  62. }