<?php
namespace App\Api\EventSubscriber;
use App\Api\Event\UserEventStatusChangedEvent;
use App\Entity\Email\UseCase;
use App\Entity\User\User;
use App\Notification\MailerService;
use App\Notification\Template;
use App\Notification\TemplateReplaceTag;
use App\Notification\VCalendarGenerator;
use App\Repository\Email\TemplateEmailRepository;
use App\Repository\Email\UseCaseRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class UserEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private Template $template,
private TemplateReplaceTag $templateReplaceTag,
private VCalendarGenerator $vCalendarGenerator,
private MailerService $mailerService
)
{
}
public static function getSubscribedEvents(): array
{
return [
UserEventStatusChangedEvent::class => 'onStatusChanged',
];
}
public function onStatusChanged(UserEventStatusChangedEvent $userEventStatusChangedEvent)
{
$userEvent = $userEventStatusChangedEvent->getUserEvent();
/** @var User $user */
$user = $userEvent->getUser();
$event = $userEvent->getEvent();
$findTemplate = $this->template->find(UseCase::INSCRIPTION_EVENT, [
'eventType' => $event->getEventType(),
'eventProfile' => $userEvent->getProfile(),
'statusUserEvent' => $userEvent->getStatus()
]);
if (!$findTemplate) {
return;
}
$icsAttachments = [];
if ($userEventStatusChangedEvent->getGenerateICS()) {
$icsAttachments[$event->getTitle()] = $this->vCalendarGenerator->render($userEvent);
}
$templateData = $this->templateReplaceTag->replace($findTemplate, $userEvent->getUser(), $userEvent->getEvent(), $userEvent->getProfile());
$this->mailerService->sendMail($templateData['subject'], $templateData['body'], $templateData['recipents'], null, null, $icsAttachments);
// if ($user->isEmailNotificationEnabled()) {
// // si envoie de mail activé
// }
// if ($user->isPushNotificationEnabled()) {
// /**@todo push notification */
// }
}
}