src/Calendar/EventSubscriber/CalendarUnavailableSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Calendar\EventSubscriber;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Harmonizely\Calendar\CalendarUnavailableEvent;
  6. use Harmonizely\EmailManager\EmailNotificationSenderInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CalendarUnavailableSubscriber implements EventSubscriberInterface
  9. {
  10.     private EmailNotificationSenderInterface $emailNotificationSender;
  11.     private EntityManagerInterface $entityManager;
  12.     public function __construct(
  13.         EmailNotificationSenderInterface $emailNotificationSender,
  14.         EntityManagerInterface $entityManager
  15.     ) {
  16.         $this->emailNotificationSender $emailNotificationSender;
  17.         $this->entityManager $entityManager;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CalendarUnavailableEvent::NAME => 'onCalendarUnavailable',
  23.         ];
  24.     }
  25.     public function onCalendarUnavailable(CalendarUnavailableEvent $event): void
  26.     {
  27.         $calendarAccount $event->getCalendarAccount();
  28.         $calendarService $event->getCalendarService();
  29.         if ($calendarAccount->getNotifiedCalendarServiceId() !== ($serviceId $calendarService->getId())) {
  30.             $this->emailNotificationSender->sendNotificationToUser($calendarAccount->getUser(), $calendarAccount);
  31.             $calendarAccount->setNotifiedCalendarServiceId($serviceId);
  32.             $this->entityManager->flush();
  33.         }
  34.     }
  35. }