src/Service/EventSubscriber/RoutingFormResultSubscriber.php line 85

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\EventSubscriber;
  3. use DateTimeImmutable;
  4. use Exception;
  5. use Harmonizely\CalendarEvents;
  6. use Harmonizely\Entity\Company\RoutingFormResultEntity;
  7. use Harmonizely\Mailer\UserMailer;
  8. use Harmonizely\Model\Event;
  9. use Harmonizely\Repository\Company\Contract\IRoutingFormResultRepository;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class RoutingFormResultSubscriber implements EventSubscriberInterface
  16. {
  17.     private const EMAIL_TEMPLATE 'routing_form_result_notification.html.twig';
  18.     /**
  19.      * @var SessionInterface
  20.      */
  21.     private SessionInterface $session;
  22.     /**
  23.      * @var IRoutingFormResultRepository
  24.      */
  25.     private IRoutingFormResultRepository $routingFormResultRepository;
  26.     /**
  27.      * @var TranslatorInterface
  28.      */
  29.     private TranslatorInterface $translator;
  30.     /**
  31.      * @var LoggerInterface
  32.      */
  33.     private LoggerInterface $logger;
  34.     /**
  35.      * @var UserMailer
  36.      */
  37.     private UserMailer $mailer;
  38.     /**
  39.      * @param SessionInterface $session
  40.      * @param IRoutingFormResultRepository $routingFormResultRepository
  41.      * @param TranslatorInterface $translator
  42.      * @param LoggerInterface $logger
  43.      * @param UserMailer $mailer
  44.      */
  45.     public function __construct(
  46.         SessionInterface $session,
  47.         IRoutingFormResultRepository $routingFormResultRepository,
  48.         TranslatorInterface $translator,
  49.         LoggerInterface $logger,
  50.         UserMailer $mailer
  51.     )
  52.     {
  53.         $this->session $session;
  54.         $this->routingFormResultRepository $routingFormResultRepository;
  55.         $this->translator $translator;
  56.         $this->logger $logger;
  57.         $this->mailer $mailer;
  58.     }
  59.     /**
  60.      * @return string[]
  61.      */
  62.     public static function getSubscribedEvents(): array
  63.     {
  64.         return [
  65.             CalendarEvents::EVENT_AFTER_PERSIST => 'afterPersistEvent',
  66.         ];
  67.     }
  68.     /**
  69.      * @param GenericEvent $genericEvent
  70.      * @return void
  71.      */
  72.     public function afterPersistEvent(GenericEvent $genericEvent): void
  73.     {
  74.         /** @var Event $event */
  75.         $event $genericEvent->getSubject();
  76.         $routingFormResultId $this->session->get(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
  77.         if ($routingFormResultId) {
  78.             $routingFormResult $this->routingFormResultRepository->findOneById($routingFormResultId);
  79.             if ($routingFormResult) {
  80.                 $routingFormRoute $routingFormResult->getRoutingFormRoute();
  81.                 $eventType $routingFormRoute->getEventType();
  82.                 if ($eventType === $event->getEventType()) {
  83.                     $routingFormResult->setEvent($event);
  84.                     $this->routingFormResultRepository->save($routingFormResult);
  85.                     $routingForm $routingFormRoute->getRoutingForm();
  86.                     if ($routingFormRoute->getRoutingForm()->isEmailNotificationForScheduledUser()) {
  87.                         foreach ($event->getUsers() as $eventUser) {
  88.                             if ($routingForm->isEmailNotification() && $routingForm->getUser()->getId() === $eventUser->getId()) {
  89.                                 continue;
  90.                             }
  91.                             try {
  92.                                 $this->mailer->send(
  93.                                     $eventUser,
  94.                                     self::EMAIL_TEMPLATE,
  95.                                     $this->translator->trans('Routing form update: new routing form answer received'),
  96.                                     [$eventUser->getEmail() => $eventUser->getFullName()],
  97.                                     [
  98.                                         'recipient' => $eventUser->getEmail(),
  99.                                         'user' => $eventUser,
  100.                                         'routingFormId' => $routingForm->getId(),
  101.                                         'routingFormName' => $routingForm->getName(),
  102.                                         'routingFormRouteName' => $routingFormRoute->getName(),
  103.                                         'answers' => $routingFormResult->getAnswers(),
  104.                                         'now' => new DateTimeImmutable(),
  105.                                         'is_admin' => true,
  106.                                     ],
  107.                                     null,
  108.                                     null,
  109.                                     true,
  110.                                     null,
  111.                                     true
  112.                                 );
  113.                             } catch (Exception $exception) {
  114.                                 $this->logger->error($exception->getMessage(), [
  115.                                     'exceptionTrace' => $exception->getTraceAsString(),
  116.                                 ]);
  117.                             }
  118.                         }
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.         $this->session->remove(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
  124.     }
  125. }