<?php
namespace App\EventSubscriber;
use App\Entity\InternalEventRegistration;
use App\Entity\InternalEventRegistrationAttendance;
use App\Entity\MainEventAttendance;
use App\Events;
use App\Mailer\EventMailer;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class EventSubscriber implements EventSubscriberInterface
{
protected $eventMailer;
protected $objectManager;
public function __construct(
EventMailer $eventMailer,
ObjectManager $objectManager
) {
$this->eventMailer = $eventMailer;
$this->objectManager = $objectManager;
}
public static function getSubscribedEvents(): array
{
return [
Events::MAIN_EVENT_DECLINED => 'onMainEventDeclined',
Events::EVENT_REGISTRATION => 'onEventRegistration',
Events::ATTENDANCE_SEND_CONFIRMATION => 'onSendAttendanceConfirmation',
Events::ATTENDANCE_CANCELLED => 'onAttendanceCancelled'
];
}
/**
* For task #89
*
* @param GenericEvent $event
*/
public function onMainEventDeclined(GenericEvent $event): void
{
/** @var MainEventAttendance $attendance */
$attendance = $event->getSubject();
$this->eventMailer->sendMainEventRefusalByOrganisation($attendance);
}
public function onEventRegistration(GenericEvent $event): void
{
/** @var InternalEventRegistration $registration */
$registration = $event->getSubject();
/** @var InternalEventRegistrationAttendance $attendance */
foreach ($registration->getAttendees() as $attendance) {
// #37: message should only be sent to newly added users, not including users that were previously added
if ($attendance->getEmail() && $attendance->isCanAttend() && !$attendance->isGivenInvitation()) {
$this->eventMailer->sendEventRegistrationEmailMessage($attendance);
$attendance->setGivenInvitation(true);
$this->objectManager->persist($attendance);
$this->objectManager->flush();
}
}
}
public function onSendAttendanceConfirmation(GenericEvent $event): void
{
/** @var InternalEventRegistrationAttendance $attendance */
$attendance = $event->getSubject();
if ($attendance->getEmail()) {
$this->eventMailer->sendAttendanceConfirmation($attendance);
}
}
public function onAttendanceCancelled(GenericEvent $event): void
{
/** @var InternalEventRegistrationAttendance $attendance */
$attendance = $event->getSubject();
if ($attendance->getEmail()) {
$this->eventMailer->sendAttendanceCancellation($attendance);
}
}
}