src/EventSubscriber/SendParticipantInviteEventPostCreateSubscriber.php line 62

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