<?php
declare(strict_types=1);
namespace Harmonizely\EventSubscriber;
use Harmonizely\CalendarEvents;
use Harmonizely\Core\Security\Contract\ISecurityHelper;
use Harmonizely\Model\EventInterface;
use Harmonizely\Model\ParticipantInterface;
use Harmonizely\Twig\isBookingFromAdmin;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
final class SendParticipantInviteEventPostCreateSubscriber implements EventSubscriberInterface
{
/**
* @var EventDispatcherInterface
*/
private EventDispatcherInterface $eventDispatcher;
/**
* @var ISecurityHelper
*/
private ISecurityHelper $securityHelper;
private isBookingFromAdmin $isBookingFromAdmin;
/**
* @param EventDispatcherInterface $eventDispatcher
* @param ISecurityHelper $securityHelper
* @param isBookingFromAdmin $isBookingFromAdmin
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
ISecurityHelper $securityHelper,
isBookingFromAdmin $isBookingFromAdmin
)
{
$this->eventDispatcher = $eventDispatcher;
$this->securityHelper = $securityHelper;
$this->isBookingFromAdmin = $isBookingFromAdmin;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CalendarEvents::EVENT_POST_CREATE => 'sendParticipantInviteEventPostCreate',
];
}
/**
* @param GenericEvent $genericEvent
*/
public function sendParticipantInviteEventPostCreate(GenericEvent $genericEvent): void
{
/** @var EventInterface $event */
$event = $genericEvent->getSubject();
$isBookingFromAdmin = $this->securityHelper->getUser() || $this->isBookingFromAdmin->isBookingFromAdmin();
if ($event->getEventType()->isClientCanAddParticipants() || $isBookingFromAdmin) {
$participantsEmails = [];
/** @var ParticipantInterface $participant */
foreach ($event->getParticipants() as $participant) {
$participantsEmails[] = $participant->getEmail();
}
if (!empty($participantsEmails)) {
$this->eventDispatcher->dispatch(new GenericEvent($event, [
'emails' => $participantsEmails,
'message' => null,
'inviter' => $event->getInvitee()->getFullName()
]), CalendarEvents::EVENT_POST_INVITE_MORE);
}
}
}
}