src/EventSubscriber/ChatMessage/ChatMessagePostWriteSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 21/05/2019
  6.  * Time: 02:52 PM
  7.  */
  8. namespace App\EventSubscriber\ChatMessage;
  9. use App\MessageHandler\ChatMessage as ChatMessageTransport;
  10. use App\MessageHandler\LogMessage;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use ApiPlatform\Core\EventListener\EventPriorities;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use App\Exception\NotFoundException;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use App\Services\ChatService;
  23. use App\Entity\Chat\ChatMessage;
  24. use App\Entity\App\User;
  25. use App\Services\PushNotificationService;
  26. use App\Services\UtilsService;
  27. use App\Repository\App\UserRepository;
  28. class ChatMessagePostWriteSubscriber implements EventSubscriberInterface
  29. {
  30.     private $tokenStorage;
  31.     private $authorizationChecker;
  32.     private $chatService;
  33.     private $entityManager;
  34.     private $pushNotificationService;
  35.     private $userRepository;
  36.     private $utilsService;
  37.     private $translator;
  38.     private $messageBus;
  39.     public function __construct(
  40.         TokenStorageInterface $tokenStorage,
  41.         AuthorizationCheckerInterface $checker,
  42.         ChatService $chatService,
  43.         EntityManagerInterface $entityManager,
  44.         PushNotificationService $pushNotificationService,
  45.         UserRepository $userRepository,
  46.         UtilsService $utilsService,
  47.         TranslatorInterface $translator,
  48.         MessageBusInterface $messageBus
  49.     ){
  50.         $this->tokenStorage $tokenStorage;
  51.         $this->authorizationChecker $checker;
  52.         $this->chatService $chatService;
  53.         $this->entityManager $entityManager;
  54.         $this->pushNotificationService $pushNotificationService;
  55.         $this->userRepository $userRepository;
  56.         $this->utilsService $utilsService;
  57.         $this->translator $translator;
  58.         $this->messageBus $messageBus;
  59.     }
  60.     /**
  61.      * @param ViewEvent $event
  62.      * @throws NotFoundException
  63.      * @throws \Doctrine\ORM\NonUniqueResultException
  64.      */
  65.     public function onKernelView(ViewEvent $event)
  66.     {
  67.         if ($this->utilsService->isAPublicRequest($event)) {
  68.             return;
  69.         }
  70.         $chatMessage $event->getControllerResult();
  71.         $request $event->getRequest();
  72.         $method $request->getMethod();
  73.         $userCurrent $this->tokenStorage->getToken()->getUser();
  74.         if (!($chatMessage instanceof ChatMessage) || (Request::METHOD_POST !== $method))
  75.             return;
  76.         if (!($userCurrent instanceof User)) {
  77.             throw new NotFoundException($this->translator->trans('User current not found'));
  78.         }
  79.         if ($this->authorizationChecker->isGranted('ROLE_SUPERADMIN')) {
  80.             return;
  81.         }
  82.         $chat $chatMessage->getChat();
  83.         $chat->setUpdatedAt(new \DateTime('now'));
  84.         $this->entityManager->persist($chat);
  85.         $this->entityManager->flush();
  86.         $this->chatService->markMessagesAsReadByChatAndUser($chat$userCurrent);
  87.         $chatMessage->messagesPendingToReadGlobalCounter $this->chatService->calculateMessagesPendingToReadForAllChats($userCurrent);
  88.         $chatMessage->messagesPendingToReadCounter $this->chatService->calculateMessagesPendingToReadForChat($chat$userCurrent);
  89.         $data['chatMessage_id'] = $chatMessage->getId();
  90.         if ($chatMessage->getType() === 'log') {
  91.             $this->messageBus->dispatch(new LogMessage(LogMessage::NEW_LOG_MESSAGE_IN_CHAT$data));
  92.         } else {
  93.             $this->messageBus->dispatch(new ChatMessageTransport(ChatMessageTransport::NEW_MESSAGE$data));
  94.         }
  95.         return;
  96.     }
  97.     public static function getSubscribedEvents()
  98.     {
  99.         return [
  100.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_WRITE]
  101.         ];
  102.     }
  103. }