src/EventSubscriber/SendParticipantInviteEventPostCreateSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Harmonizely\CalendarEvents;
  5. use Harmonizely\Core\Security\Contract\ISecurityHelper;
  6. use Harmonizely\Model\EventInterface;
  7. use Harmonizely\Model\ParticipantInterface;
  8. use Harmonizely\Service\Subscription\Voter\SubscriptionVoter;
  9. use Harmonizely\Twig\isBookingFromAdmin;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. final class SendParticipantInviteEventPostCreateSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EventDispatcherInterface
  17.      */
  18.     private EventDispatcherInterface $eventDispatcher;
  19.     /**
  20.      * @var ISecurityHelper
  21.      */
  22.     private ISecurityHelper $securityHelper;
  23.     private isBookingFromAdmin $isBookingFromAdmin;
  24.     /**
  25.      * @param EventDispatcherInterface $eventDispatcher
  26.      * @param ISecurityHelper $securityHelper
  27.      * @param isBookingFromAdmin $isBookingFromAdmin
  28.      */
  29.     public function __construct(
  30.         EventDispatcherInterface $eventDispatcher,
  31.         ISecurityHelper $securityHelper,
  32.         isBookingFromAdmin $isBookingFromAdmin
  33.     )
  34.     {
  35.         $this->eventDispatcher $eventDispatcher;
  36.         $this->securityHelper $securityHelper;
  37.         $this->isBookingFromAdmin $isBookingFromAdmin;
  38.     }
  39.     /**
  40.      * @return string[]
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             CalendarEvents::EVENT_POST_CREATE => 'sendParticipantInviteEventPostCreate',
  46.         ];
  47.     }
  48.     /**
  49.      * @param GenericEvent $genericEvent
  50.      */
  51.     public function sendParticipantInviteEventPostCreate(GenericEvent $genericEvent): void
  52.     {
  53.         /** @var EventInterface $event */
  54.         $event $genericEvent->getSubject();
  55.         if ($event->isRecurring() && $event->getRelatedEvents()->isEmpty()) {
  56.             return;
  57.         }
  58.         $isBookingFromAdmin $this->securityHelper->getUser() || $this->isBookingFromAdmin->isBookingFromAdmin();
  59.         if (!SubscriptionVoter::staticVoteOnAttribute(SubscriptionVoter::INVITEE$event->getUser())) {
  60.             return;
  61.         }
  62.         if ($event->getEventType()->isClientCanAddParticipants() || $isBookingFromAdmin) {
  63.             $participantsEmails = [];
  64.             /** @var ParticipantInterface $participant */
  65.             foreach ($event->getParticipants() as $participant) {
  66.                 $participantsEmails[] = $participant->getEmail();
  67.             }
  68.             if (!empty($participantsEmails)) {
  69.                 $this->eventDispatcher->dispatch(new GenericEvent($event, [
  70.                     'emails' => $participantsEmails,
  71.                     'message' => null,
  72.                     'inviter' => $event->getInvitee()->getFullName(),
  73.                     'showScheduledAtList' => true
  74.                 ]), CalendarEvents::EVENT_POST_INVITE_MORE);
  75.             }
  76.         }
  77.     }
  78. }