src/EventSubscriber/EventSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\InternalEventRegistration;
  4. use App\Entity\InternalEventRegistrationAttendance;
  5. use App\Entity\MainEventAttendance;
  6. use App\Events;
  7. use App\Mailer\EventMailer;
  8. use Doctrine\Common\Persistence\ObjectManager;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. class EventSubscriber implements EventSubscriberInterface
  12. {
  13.     protected $eventMailer;
  14.     protected $objectManager;
  15.     public function __construct(
  16.         EventMailer $eventMailer,
  17.         ObjectManager $objectManager
  18.     ) {
  19.         $this->eventMailer $eventMailer;
  20.         $this->objectManager $objectManager;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             Events::MAIN_EVENT_DECLINED => 'onMainEventDeclined',
  26.             Events::EVENT_REGISTRATION => 'onEventRegistration',
  27.             Events::ATTENDANCE_SEND_CONFIRMATION => 'onSendAttendanceConfirmation',
  28.             Events::ATTENDANCE_CANCELLED => 'onAttendanceCancelled'
  29.         ];
  30.     }
  31.     /**
  32.      * For task #89
  33.      *
  34.      * @param GenericEvent $event
  35.      */
  36.     public function onMainEventDeclined(GenericEvent $event): void
  37.     {
  38.         /** @var MainEventAttendance $attendance */
  39.         $attendance $event->getSubject();
  40.         $this->eventMailer->sendMainEventRefusalByOrganisation($attendance);
  41.     }
  42.     public function onEventRegistration(GenericEvent $event): void
  43.     {
  44.         /** @var InternalEventRegistration $registration */
  45.         $registration $event->getSubject();
  46.         /** @var InternalEventRegistrationAttendance $attendance */
  47.         foreach ($registration->getAttendees() as $attendance) {
  48.             // #37: message should only be sent to newly added users, not including users that were previously added
  49.             if ($attendance->getEmail() && $attendance->isCanAttend() && !$attendance->isGivenInvitation()) {
  50.                 $this->eventMailer->sendEventRegistrationEmailMessage($attendance);
  51.                 $attendance->setGivenInvitation(true);
  52.                 $this->objectManager->persist($attendance);
  53.                 $this->objectManager->flush();
  54.             }
  55.         }
  56.     }
  57.     public function onSendAttendanceConfirmation(GenericEvent $event): void
  58.     {
  59.         /** @var InternalEventRegistrationAttendance $attendance */
  60.         $attendance $event->getSubject();
  61.         if ($attendance->getEmail()) {
  62.             $this->eventMailer->sendAttendanceConfirmation($attendance);
  63.         }
  64.     }
  65.     public function onAttendanceCancelled(GenericEvent $event): void
  66.     {
  67.         /** @var InternalEventRegistrationAttendance $attendance */
  68.         $attendance $event->getSubject();
  69.         if ($attendance->getEmail()) {
  70.             $this->eventMailer->sendAttendanceCancellation($attendance);
  71.         }
  72.     }
  73. }