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

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_REQUEST => 'request',
  43.             CalendarEvents::EVENT_POST_RESCHEDULE => 'reschedule',
  44.             CalendarEvents::EVENT_POST_CANCEL => 'cancel',
  45.             OrganizationEvents::MEMBER_POST_JOIN => 'memberPostJoin',
  46.         ];
  47.     }
  48.     /**
  49.      * @param GenericEvent $genericEvent
  50.      * @return void
  51.      */
  52.     public function create(GenericEvent $genericEvent): void
  53.     {
  54.         /** @var Event $event */
  55.         $event $genericEvent->getSubject();
  56.         if ($event->isRecurring()) {
  57.             return;
  58.         }
  59.         $this->backgroundClient->add(new PushNotificationData(
  60.             UserDeviceNotificationType::TYPE_CREATE,
  61.             $event->getId(),
  62.             null,
  63.             $this->companySelector->getCompany()
  64.         ));
  65.         $this->backgroundClient->runJobs();
  66.     }
  67.     /**
  68.      * @param GenericEvent $genericEvent
  69.      * @return void
  70.      */
  71.     public function request(GenericEvent $genericEvent): void
  72.     {
  73.         /** @var Event $event */
  74.         $event $genericEvent->getSubject();
  75.         $this->backgroundClient->add(new PushNotificationData(
  76.             UserDeviceNotificationType::TYPE_REQUEST,
  77.             $event->getId(),
  78.             null,
  79.             $this->companySelector->getCompany()
  80.         ));
  81.         $this->backgroundClient->runJobs();
  82.     }
  83.     /**
  84.      * @param GenericEvent $genericEvent
  85.      * @return void
  86.      */
  87.     public function reschedule(GenericEvent $genericEvent): void
  88.     {
  89.         /** @var Event $event */
  90.         $event $genericEvent->getSubject();
  91.         $this->backgroundClient->add(new PushNotificationData(
  92.             UserDeviceNotificationType::TYPE_RESCHEDULE,
  93.             $event->getId(),
  94.             null,
  95.             $this->companySelector->getCompany()
  96.         ));
  97.         $this->backgroundClient->runJobs();
  98.     }
  99.     /**
  100.      * @param GenericEvent $genericEvent
  101.      * @return void
  102.      */
  103.     public function cancel(GenericEvent $genericEvent): void
  104.     {
  105.         /** @var Event $event */
  106.         $event $genericEvent->getSubject();
  107.         if ($event->isRecurring()) {
  108.             if ($event->getCancellation() && !$event->getCancellation()->isSendNotification()) {
  109.                 return;
  110.             }
  111.         }
  112.         $this->backgroundClient->add(new PushNotificationData(
  113.             UserDeviceNotificationType::TYPE_CANCEL,
  114.             $event->getId(),
  115.             null,
  116.             $this->companySelector->getCompany()
  117.         ));
  118.         $this->backgroundClient->runJobs();
  119.     }
  120.     /**
  121.      * @param GenericEvent $genericEvent
  122.      * @return void
  123.      */
  124.     public function memberPostJoin(GenericEvent $genericEvent)
  125.     {
  126.         /** @var Member $member */
  127.         $member $genericEvent->getSubject();
  128.         $user $member->getUser()->getId();
  129.         $this->backgroundClient->add(new PushNotificationData(
  130.             UserDeviceNotificationType::TYPE_ORGANIZATION,
  131.             null,
  132.             $user,
  133.             $this->companySelector->getCompany()
  134.         ));
  135.         $this->backgroundClient->runJobs();
  136.     }
  137. }