<?php
namespace Harmonizely\Service\EventSubscriber;
use DateTimeImmutable;
use Exception;
use Harmonizely\CalendarEvents;
use Harmonizely\Entity\Company\RoutingFormResultEntity;
use Harmonizely\Mailer\UserMailer;
use Harmonizely\Model\Event;
use Harmonizely\Repository\Company\Contract\IRoutingFormResultRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class RoutingFormResultSubscriber implements EventSubscriberInterface
{
private const EMAIL_TEMPLATE = 'routing_form_result_notification.html.twig';
/**
* @var SessionInterface
*/
private SessionInterface $session;
/**
* @var IRoutingFormResultRepository
*/
private IRoutingFormResultRepository $routingFormResultRepository;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var UserMailer
*/
private UserMailer $mailer;
/**
* @param SessionInterface $session
* @param IRoutingFormResultRepository $routingFormResultRepository
* @param TranslatorInterface $translator
* @param LoggerInterface $logger
* @param UserMailer $mailer
*/
public function __construct(
SessionInterface $session,
IRoutingFormResultRepository $routingFormResultRepository,
TranslatorInterface $translator,
LoggerInterface $logger,
UserMailer $mailer
)
{
$this->session = $session;
$this->routingFormResultRepository = $routingFormResultRepository;
$this->translator = $translator;
$this->logger = $logger;
$this->mailer = $mailer;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::EVENT_AFTER_PERSIST => 'afterPersistEvent',
];
}
/**
* @param GenericEvent $genericEvent
* @return void
*/
public function afterPersistEvent(GenericEvent $genericEvent): void
{
/** @var Event $event */
$event = $genericEvent->getSubject();
$routingFormResultId = $this->session->get(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
if ($routingFormResultId) {
$routingFormResult = $this->routingFormResultRepository->findOneById($routingFormResultId);
if ($routingFormResult) {
$routingFormRoute = $routingFormResult->getRoutingFormRoute();
$eventType = $routingFormRoute->getEventType();
if ($eventType === $event->getEventType()) {
$routingFormResult->setEvent($event);
$this->routingFormResultRepository->save($routingFormResult);
$routingForm = $routingFormRoute->getRoutingForm();
if ($routingFormRoute->getRoutingForm()->isEmailNotificationForScheduledUser()) {
foreach ($event->getUsers() as $eventUser) {
if ($routingForm->isEmailNotification() && $routingForm->getUser()->getId() === $eventUser->getId()) {
continue;
}
try {
$this->mailer->send(
$eventUser,
self::EMAIL_TEMPLATE,
$this->translator->trans('Routing form update: new routing form answer received'),
[$eventUser->getEmail() => $eventUser->getFullName()],
[
'recipient' => $eventUser->getEmail(),
'user' => $eventUser,
'routingFormId' => $routingForm->getId(),
'routingFormName' => $routingForm->getName(),
'routingFormRouteName' => $routingFormRoute->getName(),
'answers' => $routingFormResult->getAnswers(),
'now' => new DateTimeImmutable(),
'is_admin' => true,
],
null,
null,
true,
null,
true
);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage(), [
'exceptionTrace' => $exception->getTraceAsString(),
]);
}
}
}
}
}
}
$this->session->remove(RoutingFormResultEntity::ROUTING_FORM_RESULT_SESSION_KEY);
}
}