vendor/artibox/middleware/src/EventListener/Notification/NotificationCreateEventSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\Notification;
  3. use App\Entity\Company\Company;
  4. use App\Enum\NotificationEvents;
  5. use App\Event\NotificationCreateEvent;
  6. use App\Repository\Company\UserRepository;
  7. use App\Service\Notification\NotificationService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class NotificationCreateEventSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private NotificationService $notificationService,
  13.         private UserRepository $userRepository
  14.     ) {
  15.         $this->notificationService $notificationService;
  16.         $this->userRepository $userRepository;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         return [
  21.             NotificationCreateEvent::NAME => 'onNotificationCreation'
  22.         ];
  23.     }
  24.     public function onNotificationCreation(NotificationCreateEvent $event)
  25.     {
  26.         $data $event->getData();
  27.         $data['link'] = $event->getLink();
  28.         if ($event->getTitleOptions()) {
  29.             $data['title'] = NotificationEvents::getTitle($event->getEvent(), $event->getTitleOptions());
  30.         }
  31.         if ($event->getContentOptions()) {
  32.             $data['content'] = NotificationEvents::getContent($event->getEvent(), $event->getContentOptions());
  33.         }
  34.         $data['event'] = $event->getEvent();
  35.         $data['route'] = $event->getRoute();
  36.         $data['route_params'] = $event->getRouteParams();
  37.         // envoyer notification web
  38.         $this->notificationService->send($data);
  39.         // envoyer notification push
  40.         $recipient $data['recipient'];
  41.         $user $recipient instanceof Company $this->userRepository->getCompanyPrincipalUser($recipient->getId()) : $this->userRepository->getCustomerCompanyPrincipalUser($recipient->getId());
  42.         if ($user && $user->getDeviceToken()) {
  43.             $this->notificationService->sendPushNotification($user->getDeviceToken(), $event->getEvent());
  44.         }
  45.     }
  46. }