src/Notification/EventSubscriber/NotificationSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Notification\EventSubscriber;
  3. use App\Notification\Service\NotificationService;
  4. use App\Profile\Event\ProfileEvents;
  5. use App\Profile\Event\ProfileNetworkEvent;
  6. use App\Profile\Messages\Event\MessageSendEvent;
  7. use App\Profile\Messages\Event\MessagesEvents;
  8. use App\Profile\Model\Profile;
  9. use App\Timeline\Event\TimelinePostCommentedEvent;
  10. use App\Timeline\Event\TimelinePostEvents;
  11. use App\Timeline\Event\TimelinePostLikedEvent;
  12. use App\Timeline\Event\TimelinePostMentionedEvent;
  13. use App\Timeline\Event\TimelinePostSharedEvent;
  14. use Pimcore\Model\DataObject\TimelinePost;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Notifier\NotifierInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class NotificationSubscriber implements EventSubscriberInterface
  19. {
  20.   public function __construct(protected NotifierInterface $notifier, protected TranslatorInterface $translator, protected NotificationService $notificationService)
  21.   {
  22.   }
  23.   public static function getSubscribedEvents()
  24.   {
  25.     return [
  26.         ProfileEvents::NETWORK_ADD => 'onNetworkAdd',
  27.         ProfileEvents::NETWORK_REMOVE => 'onNetworkRemove',
  28.         TimelinePostEvents::LIKED => 'onPostLiked',
  29.         TimelinePostEvents::COMMENTED => 'onPostComment',
  30.         TimelinePostEvents::MENTIONED => 'onPostMention',
  31.         TimelinePostEvents::SHARED => 'onPostShared',
  32.         MessagesEvents::SEND => 'onMessageSend',
  33.     ];
  34.   }
  35.   // Event Methods
  36.   public function onNetworkAdd(ProfileNetworkEvent $event)
  37.   {
  38.     $this->notificationService->sendProfileNetworkNotification($event->getUser(), $event->getProfile(), 'profile.network.add''urgent');
  39.   }
  40.   public function onNetworkRemove(ProfileNetworkEvent $event)
  41.   {
  42.   }
  43.   public function onPostLiked(TimelinePostLikedEvent $event)
  44.   {
  45.     $profile $event->post->getProfile();
  46.     $sender $event->getUser();
  47.     $this->notificationService->sendTimelineNotification($event->post$sender$profile'post.liked''urgent');
  48.   }
  49.   public function onPostComment(TimelinePostCommentedEvent $event)
  50.   {
  51.     $profile $event->comment->getParent()->getProfile();
  52.     $sender $event->getUser();
  53.     $post $this->findTimelinePost($event->comment);
  54.     $this->notificationService->sendTimelineNotification($post$sender$profile'post.comment''urgent');
  55.   }
  56.   private function findTimelinePost($post)
  57.   {
  58.     if ($post instanceof TimelinePost) {
  59.       return $post;
  60.     } else {
  61.       return $this->findTimelinePost($post->getParent());
  62.     }
  63.   }
  64.   public function onPostMention(TimelinePostMentionedEvent $event)
  65.   {
  66.     $this->notificationService->sendTimelineNotification($event->post$event->user$event->mentioned'post.mentioned''urgent');
  67.   }
  68.   public function onPostShared(TimelinePostSharedEvent $event)
  69.   {
  70.     $this->notificationService->sendTimelineNotification($event->post$event->user$event->originalPostUser'post.shared''urgent');
  71.   }
  72.   public function onMessageSend(MessageSendEvent $event)
  73.   {
  74.     $profile Profile::getById($event->message->getReceiverId());
  75.     $sender Profile::getById($event->message->getSenderId());
  76.     $this->notificationService->sendMessageNotification($event->message$sender$profile'chat.message''urgent');
  77.   }
  78. }