<?php
namespace App\Notification\EventSubscriber;
use App\Notification\Service\NotificationService;
use App\Profile\Event\ProfileEvents;
use App\Profile\Event\ProfileNetworkEvent;
use App\Profile\Messages\Event\MessageSendEvent;
use App\Profile\Messages\Event\MessagesEvents;
use App\Profile\Model\Profile;
use App\Timeline\Event\TimelinePostCommentedEvent;
use App\Timeline\Event\TimelinePostEvents;
use App\Timeline\Event\TimelinePostLikedEvent;
use App\Timeline\Event\TimelinePostMentionedEvent;
use App\Timeline\Event\TimelinePostSharedEvent;
use Pimcore\Model\DataObject\TimelinePost;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class NotificationSubscriber implements EventSubscriberInterface
{
public function __construct(protected NotifierInterface $notifier, protected TranslatorInterface $translator, protected NotificationService $notificationService)
{
}
public static function getSubscribedEvents()
{
return [
ProfileEvents::NETWORK_ADD => 'onNetworkAdd',
ProfileEvents::NETWORK_REMOVE => 'onNetworkRemove',
TimelinePostEvents::LIKED => 'onPostLiked',
TimelinePostEvents::COMMENTED => 'onPostComment',
TimelinePostEvents::MENTIONED => 'onPostMention',
TimelinePostEvents::SHARED => 'onPostShared',
MessagesEvents::SEND => 'onMessageSend',
];
}
// Event Methods
public function onNetworkAdd(ProfileNetworkEvent $event)
{
$this->notificationService->sendProfileNetworkNotification($event->getUser(), $event->getProfile(), 'profile.network.add', 'urgent');
}
public function onNetworkRemove(ProfileNetworkEvent $event)
{
}
public function onPostLiked(TimelinePostLikedEvent $event)
{
$profile = $event->post->getProfile();
$sender = $event->getUser();
$this->notificationService->sendTimelineNotification($event->post, $sender, $profile, 'post.liked', 'urgent');
}
public function onPostComment(TimelinePostCommentedEvent $event)
{
$profile = $event->comment->getParent()->getProfile();
$sender = $event->getUser();
$post = $this->findTimelinePost($event->comment);
$this->notificationService->sendTimelineNotification($post, $sender, $profile, 'post.comment', 'urgent');
}
private function findTimelinePost($post)
{
if ($post instanceof TimelinePost) {
return $post;
} else {
return $this->findTimelinePost($post->getParent());
}
}
public function onPostMention(TimelinePostMentionedEvent $event)
{
$this->notificationService->sendTimelineNotification($event->post, $event->user, $event->mentioned, 'post.mentioned', 'urgent');
}
public function onPostShared(TimelinePostSharedEvent $event)
{
$this->notificationService->sendTimelineNotification($event->post, $event->user, $event->originalPostUser, 'post.shared', 'urgent');
}
public function onMessageSend(MessageSendEvent $event)
{
$profile = Profile::getById($event->message->getReceiverId());
$sender = Profile::getById($event->message->getSenderId());
$this->notificationService->sendMessageNotification($event->message, $sender, $profile, 'chat.message', 'urgent');
}
}