src/EventSubscriber/ChatMessage/ChatMessagePostSerializeSubscriber.php line 56

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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. use ApiPlatform\Core\EventListener\EventPriorities;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. use App\Services\ChatService;
  23. class ChatMessagePostSerializeSubscriber 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.      * @throws \Doctrine\ORM\NonUniqueResultException
  50.      */
  51.     public function onKernelView(ViewEvent $event)
  52.     {
  53.         if ($this->utilsService->isAPublicRequest($event)) {
  54.             return;
  55.         }
  56.         $chatMessage $event->getControllerResult();
  57.         $request $event->getRequest();
  58.         $method $request->getMethod();
  59.         $userCurrent $this->tokenStorage->getToken()->getUser();
  60.         if (Request::METHOD_GET !== $method) {
  61.             return;
  62.         }
  63.         $routes = array('api_chats_messages_get_subresource');
  64.         $route $request->attributes->get('_route');
  65.         if (!in_array($route$routes)) {
  66.             return;
  67.         }
  68.         if (!$userCurrent instanceof User) {
  69.             throw new NotFoundException($this->translator->trans('User not found'));
  70.         }
  71.         $chatId $route $request->attributes->get('id');
  72.         $chat $this->chatRepository->findOneBy(['id' => $chatId]);
  73.         if (!$chat instanceof Chat) {
  74.             return;
  75.         }
  76.         $locale $userCurrent->getLocale();
  77.         if ($request->query->has('locale') &&
  78.             in_array(strtolower($request->query->get('locale')), [User::LOCALE_ENUser::LOCALE_ES])
  79.         ) {
  80.             $locale strtolower($request->query->get('locale'));
  81.         }
  82.         $chatMessageResult json_decode($chatMessagetrue);
  83.         $order $request->query->get('order') ?? [];
  84.         if (isset($order['createdAt']) && isset($order['id'])) {
  85.             if ($order['id'] === 'asc') {
  86.                 $chatMessageResult $this->sorting($chatMessageResulttrue);
  87.             }
  88.             if ($order['id'] === 'desc') {
  89.                 $chatMessageResult $this->sorting($chatMessageResultfalse);
  90.             }
  91.         }
  92.         foreach ($chatMessageResult as &$message) {
  93.             if ($message['type'] === 'log') {
  94.                 $body $message['body'];
  95.                 if (is_string($body)) {
  96.                     $body json_decode($message['body'], true);
  97.                 }
  98.                 $parameters = [];
  99.                 if (isset($body['parameters'])) {
  100.                     $parameters $body['parameters'];
  101.                 }
  102.                 if (isset($body['parametersTrans'])) {
  103.                     foreach ($body['parametersTrans'] as $key => $value) {
  104.                         $parameters[$key] = $this->translator->trans($value, [], null$userCurrent->getLocale());
  105.                     }
  106.                 }
  107.                 if (isset($body['message'])) {
  108.                     $message['body'] = $this->translator->trans(
  109.                         $body['message'],
  110.                         $parameters,
  111.                         null,
  112.                         $locale
  113.                     );
  114.                 }
  115.             }
  116.         }
  117.         $chatMessageResultMain['messages'] = $chatMessageResult;
  118.         $chatMessageResultMain['messagesPendingToReadGlobalCounter'] = $this->chatService->calculateMessagesPendingToReadForAllChats(
  119.             $userCurrent
  120.         );
  121.         $chatMessageResultMain['messagesPendingToReadCounter'] = $this->chatService->calculateMessagesPendingToReadForChat(
  122.             $chat,
  123.             $userCurrent
  124.         );
  125.         $chatMessageResultMain json_encode($chatMessageResultMain);
  126.         $event->setControllerResult($chatMessageResultMain);
  127.     }
  128.     public static function getSubscribedEvents()
  129.     {
  130.         return [
  131.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  132.         ];
  133.     }
  134.     protected function sorting($array$ascending true)
  135.     {
  136.         $ids = [];
  137.         foreach ($array as $key => $value) {
  138.             $ids[$key] = $value['id'];
  139.         }
  140.         if ($ascending) {
  141.             asort($ids);
  142.         } else {
  143.             arsort($ids);
  144.         }
  145.         $result = [];
  146.         foreach ($ids as $index => $values) {
  147.             $result[] = $array[$index];
  148.         }
  149.         if (count($result) === 0) {
  150.             $result $array;
  151.         }
  152.         return $result;
  153.     }
  154. }