<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 21/05/2019
* Time: 02:52 PM
*/
namespace App\EventSubscriber\ChatMessage;
use App\MessageHandler\ChatMessage as ChatMessageTransport;
use App\MessageHandler\LogMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;
use App\Exception\NotFoundException;
use Doctrine\ORM\EntityManagerInterface;
use App\Services\ChatService;
use App\Entity\Chat\ChatMessage;
use App\Entity\App\User;
use App\Services\PushNotificationService;
use App\Services\UtilsService;
use App\Repository\App\UserRepository;
class ChatMessagePostWriteSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
private $authorizationChecker;
private $chatService;
private $entityManager;
private $pushNotificationService;
private $userRepository;
private $utilsService;
private $translator;
private $messageBus;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $checker,
ChatService $chatService,
EntityManagerInterface $entityManager,
PushNotificationService $pushNotificationService,
UserRepository $userRepository,
UtilsService $utilsService,
TranslatorInterface $translator,
MessageBusInterface $messageBus
){
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $checker;
$this->chatService = $chatService;
$this->entityManager = $entityManager;
$this->pushNotificationService = $pushNotificationService;
$this->userRepository = $userRepository;
$this->utilsService = $utilsService;
$this->translator = $translator;
$this->messageBus = $messageBus;
}
/**
* @param ViewEvent $event
* @throws NotFoundException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$chatMessage = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
$userCurrent = $this->tokenStorage->getToken()->getUser();
if (!($chatMessage instanceof ChatMessage) || (Request::METHOD_POST !== $method))
return;
if (!($userCurrent instanceof User)) {
throw new NotFoundException($this->translator->trans('User current not found'));
}
if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN')) {
return;
}
$chat = $chatMessage->getChat();
$chat->setUpdatedAt(new \DateTime('now'));
$this->entityManager->persist($chat);
$this->entityManager->flush();
$this->chatService->markMessagesAsReadByChatAndUser($chat, $userCurrent);
$chatMessage->messagesPendingToReadGlobalCounter = $this->chatService->calculateMessagesPendingToReadForAllChats($userCurrent);
$chatMessage->messagesPendingToReadCounter = $this->chatService->calculateMessagesPendingToReadForChat($chat, $userCurrent);
$data['chatMessage_id'] = $chatMessage->getId();
if ($chatMessage->getType() === 'log') {
$this->messageBus->dispatch(new LogMessage(LogMessage::NEW_LOG_MESSAGE_IN_CHAT, $data));
} else {
$this->messageBus->dispatch(new ChatMessageTransport(ChatMessageTransport::NEW_MESSAGE, $data));
}
return;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_WRITE]
];
}
}