src/EventSubscriber/ChatMessage/ChatMessagePreSerializeSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 21/05/2019
  6.  * Time: 02:15 PM
  7.  */
  8. namespace App\EventSubscriber\ChatMessage;
  9. use App\Entity\App\User;
  10. use App\Entity\Chat\Chat;
  11. use App\Exception\NotFoundException;
  12. use App\Repository\Chat\ChatRepository;
  13. use App\Services\UtilsService;
  14. use App\Services\ChatService;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use ApiPlatform\Core\EventListener\EventPriorities;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. class ChatMessagePreSerializeSubscriber implements EventSubscriberInterface
  24. {
  25.     private $tokenStorage;
  26.     private $authorizationChecker;
  27.     private $chatService;
  28.     private $utilsService;
  29.     private $translator;
  30.     private $chatRepository;
  31.     public function __construct(
  32.         TokenStorageInterface $tokenStorage,
  33.         AuthorizationCheckerInterface $checker,
  34.         ChatService $chatService,
  35.         UtilsService $utilsService,
  36.         TranslatorInterface $translator,
  37.         ChatRepository $chatRepository)
  38.     {
  39.         $this->tokenStorage $tokenStorage;
  40.         $this->authorizationChecker $checker;
  41.         $this->chatService $chatService;
  42.         $this->utilsService $utilsService;
  43.         $this->translator $translator;
  44.         $this->chatRepository $chatRepository;
  45.     }
  46.     /**
  47.      * @param ViewEvent $event
  48.      * @throws NotFoundException
  49.      */
  50.     public function onKernelView(ViewEvent $event)
  51.     {
  52.         if ($this->utilsService->isAPublicRequest($event)) {
  53.             return;
  54.         }
  55.         $request $event->getRequest();
  56.         $method $request->getMethod();
  57.         $userCurrent $this->tokenStorage->getToken()->getUser();
  58.         if (Request::METHOD_GET !== $method)
  59.             return;
  60.         $routes = array('api_chats_messages_get_subresource');
  61.         $route $request->attributes->get('_route');
  62.         if (!in_array($route$routes))
  63.             return;
  64.         $chatId $route $request->attributes->get('id');
  65.         $chat $this->chatRepository->findOneBy(['id' => $chatId]);
  66.         if (!$chat instanceof Chat) {
  67.             throw new NotFoundException($this->translator->trans('entity_not_found', ['%entity%' => 'chat']));
  68.         }
  69.         if (!$userCurrent instanceof User) {
  70.             throw new NotFoundException($this->translator->trans('User current not found'));
  71.         }
  72.         $this->chatService->markMessagesAsReadByChatAndUser($chat$userCurrent);
  73.     }
  74.     public static function getSubscribedEvents()
  75.     {
  76.         return [
  77.             KernelEvents::VIEW => ['onKernelView'EventPriorities::PRE_SERIALIZE]
  78.         ];
  79.     }
  80. }