src/Service/Panel/Api/PushNotification/EventSubscriber/PushNotificationSubscriber.php line 85

Open in your IDE?
  1. <?php
  2. namespace Harmonizely\Service\Panel\Api\PushNotification\EventSubscriber;
  3. use Harmonizely\Background\Data\PushNotificationData;
  4. use Harmonizely\CalendarEvents;
  5. use Harmonizely\Core\BackgroundWork\Contract\IClient;
  6. use Harmonizely\Model\Event;
  7. use Harmonizely\Model\Member;
  8. use Harmonizely\Organization\OrganizationEvents;
  9. use Harmonizely\Service\Core\Contract\ICompanySelector;
  10. use Harmonizely\Types\Company\UserDeviceNotificationType;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\EventDispatcher\GenericEvent;
  13. class PushNotificationSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var IClient
  17.      */
  18.     private IClient $backgroundClient;
  19.     /**
  20.      * @var ICompanySelector
  21.      */
  22.     private ICompanySelector $companySelector;
  23.     /**
  24.      * @param IClient $backgroundClient
  25.      * @param ICompanySelector $companySelector
  26.      */
  27.     public function __construct(
  28.         IClient $backgroundClient,
  29.         ICompanySelector $companySelector
  30.     )
  31.     {
  32.         $this->backgroundClient $backgroundClient;
  33.         $this->companySelector $companySelector;
  34.     }
  35.     /**
  36.      * @return string[]
  37.      */
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             CalendarEvents::EVENT_POST_CREATE => 'create',
  42.             CalendarEvents::EVENT_POST_RESCHEDULE => 'reschedule',
  43.             CalendarEvents::EVENT_POST_CANCEL => 'cancel',
  44.             OrganizationEvents::MEMBER_POST_JOIN => 'memberPostJoin',
  45.         ];
  46.     }
  47.     /**
  48.      * @param GenericEvent $genericEvent
  49.      * @return void
  50.      */
  51.     public function create(GenericEvent $genericEvent): void
  52.     {
  53.         /** @var Event $event */
  54.         $event $genericEvent->getSubject();
  55.         if ($event->isRecurring()) {
  56.             return;
  57.         }
  58.         $this->backgroundClient->add(new PushNotificationData(
  59.             UserDeviceNotificationType::TYPE_CREATE,
  60.             $event->getId(),
  61.             null,
  62.             $this->companySelector->getCompany()
  63.         ));
  64.         $this->backgroundClient->runJobs();
  65.     }
  66.     /**
  67.      * @param GenericEvent $genericEvent
  68.      * @return void
  69.      */
  70.     public function reschedule(GenericEvent $genericEvent): void
  71.     {
  72.         /** @var Event $event */
  73.         $event $genericEvent->getSubject();
  74.         $this->backgroundClient->add(new PushNotificationData(
  75.             UserDeviceNotificationType::TYPE_RESCHEDULE,
  76.             $event->getId(),
  77.             null,
  78.             $this->companySelector->getCompany()
  79.         ));
  80.         $this->backgroundClient->runJobs();
  81.     }
  82.     /**
  83.      * @param GenericEvent $genericEvent
  84.      * @return void
  85.      */
  86.     public function cancel(GenericEvent $genericEvent): void
  87.     {
  88.         /** @var Event $event */
  89.         $event $genericEvent->getSubject();
  90.         if ($event->isRecurring()) {
  91.             if ($event->getCancellation() && !$event->getCancellation()->isSendNotification()) {
  92.                 return;
  93.             }
  94.         }
  95.         $this->backgroundClient->add(new PushNotificationData(
  96.             UserDeviceNotificationType::TYPE_CANCEL,
  97.             $event->getId(),
  98.             null,
  99.             $this->companySelector->getCompany()
  100.         ));
  101.         $this->backgroundClient->runJobs();
  102.     }
  103.     /**
  104.      * @param GenericEvent $genericEvent
  105.      * @return void
  106.      */
  107.     public function memberPostJoin(GenericEvent $genericEvent)
  108.     {
  109.         /** @var Member $member */
  110.         $member $genericEvent->getSubject();
  111.         $user $member->getUser()->getId();
  112.         $this->backgroundClient->add(new PushNotificationData(
  113.             UserDeviceNotificationType::TYPE_ORGANIZATION,
  114.             null,
  115.             $user,
  116.             $this->companySelector->getCompany()
  117.         ));
  118.         $this->backgroundClient->runJobs();
  119.     }
  120. }