src/EventListener/NewEventScheduledListener.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventListener;
  4. use Harmonizely\Background\Data\EventMailManagerData;
  5. use Harmonizely\EmailManager\EventEmailManagerInterface;
  6. use Harmonizely\Mailer\AllowedSendingEmailNotificationToInviteePolicy;
  7. use Harmonizely\Model\EventInterface;
  8. use Harmonizely\Model\EventTypeInterface;
  9. use Harmonizely\Service\Core\Contract\ICompanySelector;
  10. use Symfony\Component\EventDispatcher\GenericEvent;
  11. use Webmozart\Assert\Assert;
  12. final class NewEventScheduledListener
  13. {
  14.     private AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy;
  15.     private ICompanySelector $companySelector;
  16.     private EventEmailManagerInterface $eventEmailManager;
  17.     public function __construct(
  18.         AllowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy,
  19.         ICompanySelector $companySelector,
  20.         EventEmailManagerInterface $eventEmailManager
  21.     ) {
  22.         $this->allowedSendingEmailNotificationToInviteePolicy $allowedSendingEmailNotificationToInviteePolicy;
  23.         $this->companySelector $companySelector;
  24.         $this->eventEmailManager $eventEmailManager;
  25.     }
  26.     public function sendEmail(GenericEvent $event): void
  27.     {
  28.         /** @var EventInterface $event */
  29.         $event $event->getSubject();
  30.         Assert::isInstanceOf($eventEventInterface::class);
  31.         if (false === ($this->allowedSendingEmailNotificationToInviteePolicy)($event)) {
  32.             return;
  33.         }
  34.         if ($event->isRecurring() && $event->getRelatedEvents()->isEmpty()) {
  35.             return;
  36.         }
  37.         if (EventTypeInterface::TYPE_COLLECTIVE === $event->getEventType()->getType()) {
  38.             $users $event->getEventType()->getUsers();
  39.         } else {
  40.             $users = [$event->getUser()];
  41.         }
  42.         foreach ($users as $eventUser) {
  43.             if ($eventUser->getEmail() === $event->getInvitee()->getEmail()) {
  44.                 return;
  45.             }
  46.         }
  47.         $this->eventEmailManager->sendBackgroundNotification(new EventMailManagerData(
  48.             $event->getId(),
  49.             EventMailManagerData::NEW_EVENT_INVITEE_CONFIRMATION_ACTION,
  50.             $this->companySelector->getCompany(),
  51.             [
  52.                 'showScheduledAtList' => true
  53.             ]
  54.         ));
  55.     }
  56.     public function sendNewEventEmailConfirmation(GenericEvent $event): void
  57.     {
  58.         /** @var EventInterface $event */
  59.         $event $event->getSubject();
  60.         Assert::isInstanceOf($eventEventInterface::class);
  61.         if ($event->isRecurring() && $event->getRelatedEvents()->isEmpty()) {
  62.             return;
  63.         }
  64.         $this->eventEmailManager->sendBackgroundNotification(new EventMailManagerData(
  65.             $event->getId(),
  66.             EventMailManagerData::NEW_EVENT_EMAIL_CONFIRMATION,
  67.             $this->companySelector->getCompany()
  68.         ));
  69.     }
  70. }