<?php
declare(strict_types=1);
namespace Harmonizely\Calendar\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Harmonizely\Calendar\CalendarUnavailableEvent;
use Harmonizely\EmailManager\EmailNotificationSenderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CalendarUnavailableSubscriber implements EventSubscriberInterface
{
private EmailNotificationSenderInterface $emailNotificationSender;
private EntityManagerInterface $entityManager;
public function __construct(
EmailNotificationSenderInterface $emailNotificationSender,
EntityManagerInterface $entityManager
) {
$this->emailNotificationSender = $emailNotificationSender;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
CalendarUnavailableEvent::NAME => 'onCalendarUnavailable',
];
}
public function onCalendarUnavailable(CalendarUnavailableEvent $event): void
{
$calendarAccount = $event->getCalendarAccount();
$calendarService = $event->getCalendarService();
if ($calendarAccount->getNotifiedCalendarServiceId() !== ($serviceId = $calendarService->getId())) {
$this->emailNotificationSender->sendNotificationToUser($calendarAccount->getUser(), $calendarAccount);
$calendarAccount->setNotifiedCalendarServiceId($serviceId);
$this->entityManager->flush();
}
}
}