src/EventSubscriber/Chat/ChatPostSerializerSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 21/05/2019
  6.  * Time: 08:43 PM
  7.  */
  8. namespace App\EventSubscriber\Chat;
  9. use App\Entity\App\User;
  10. use App\Entity\Chat\Chat;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use ApiPlatform\Core\EventListener\EventPriorities;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use App\Services\ChatService;
  20. use App\Services\UtilsService;
  21. class ChatPostSerializerSubscriber implements EventSubscriberInterface
  22. {
  23.     private $tokenStorage;
  24.     private $authorizationChecker;
  25.     private $chatService;
  26.     private $utilsService;
  27.     private $translator;
  28.     public function __construct(
  29.         TokenStorageInterface $tokenStorage,
  30.         AuthorizationCheckerInterface $checker,
  31.         UtilsService $utilsService,
  32.         ChatService $chatService,
  33.         TranslatorInterface $translator
  34.     ) {
  35.         $this->tokenStorage $tokenStorage;
  36.         $this->authorizationChecker $checker;
  37.         $this->utilsService $utilsService;
  38.         $this->chatService $chatService;
  39.         $this->translator $translator;
  40.     }
  41.     /**
  42.      * @param ViewEvent $event
  43.      * @throws \Doctrine\ORM\NonUniqueResultException
  44.      */
  45.     public function onKernelView(ViewEvent $event)
  46.     {
  47.         if ($this->utilsService->isAPublicRequest($event)) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         $chatResult $event->getControllerResult();
  52.         $routes = array(
  53.             'api_chats_get_collection',
  54.             'api_chats_post_collection',
  55.             'api_chats_get_item'
  56.         );
  57.         $route $request->attributes->get('_route');
  58.         if (!in_array($route$routes)) {
  59.             return;
  60.         }
  61.         $userCurrent $this->tokenStorage->getToken()->getUser();
  62.         if (!$userCurrent instanceof User) {
  63.             $response = new Response();
  64.             $response->setContent(json_encode([
  65.                 'detail' => $this->translator->trans(
  66.                         'User not found',
  67.                         [],
  68.                         null,
  69.                         $request->getLocale()
  70.                     )
  71.             ]));
  72.             $response->setStatusCode(Response::HTTP_UNAUTHORIZED);
  73.             $event->setResponse($response);
  74.             return;
  75.         }
  76.         $chatResult json_decode($chatResulttrue);
  77.         //var_dump($chatResult);
  78.         //exit();
  79.         if ('api_chats_get_collection' === $route) {
  80.             foreach ($chatResult as $key => &$chat) {
  81.                 $chat $this->chatService->formatNormalize($chat$userCurrent);
  82.             }
  83.         } else {
  84.             $chatResult $this->chatService->formatNormalize($chatResult$userCurrent);
  85.         }
  86.         $chatResultMain['chats'] = $chatResult;
  87.         $chatResultMain['messagesPendingToReadGlobalCounter'] = $this->chatService
  88.             ->calculateMessagesPendingToReadForAllChats($userCurrent);
  89.         $chatResultMain['messagesPendingToReadGlobalTicketCounter'] = $this->chatService
  90.             ->calculateMessagesPendingToReadForTypeChats($userCurrentChat::TYPE_TICKET);
  91.         $chatResultMain['messagesPendingToReadGlobalTaskCounter'] = $this->chatService
  92.             ->calculateMessagesPendingToReadForTypeChats($userCurrentChat::TYPE_TASK);
  93.         $chatResultMain['messagesGlobalCounter'] = $this->chatService
  94.             ->calculateMessagesTotalForAllChats($userCurrent);
  95.         $event->setControllerResult(json_encode($chatResultMain));
  96.     }
  97.     public static function getSubscribedEvents()
  98.     {
  99.         return [
  100.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  101.         ];
  102.     }
  103. }