<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 21/05/2019
* Time: 08:43 PM
*/
namespace App\EventSubscriber\Chat;
use App\Entity\App\User;
use App\Entity\Chat\Chat;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Services\ChatService;
use App\Services\UtilsService;
class ChatPostSerializerSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
private $authorizationChecker;
private $chatService;
private $utilsService;
private $translator;
public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $checker,
UtilsService $utilsService,
ChatService $chatService,
TranslatorInterface $translator
) {
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $checker;
$this->utilsService = $utilsService;
$this->chatService = $chatService;
$this->translator = $translator;
}
/**
* @param ViewEvent $event
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$request = $event->getRequest();
$chatResult = $event->getControllerResult();
$routes = array(
'api_chats_get_collection',
'api_chats_post_collection',
'api_chats_get_item'
);
$route = $request->attributes->get('_route');
if (!in_array($route, $routes)) {
return;
}
$userCurrent = $this->tokenStorage->getToken()->getUser();
if (!$userCurrent instanceof User) {
$response = new Response();
$response->setContent(json_encode([
'detail' => $this->translator->trans(
'User not found',
[],
null,
$request->getLocale()
)
]));
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
$event->setResponse($response);
return;
}
$chatResult = json_decode($chatResult, true);
//var_dump($chatResult);
//exit();
if ('api_chats_get_collection' === $route) {
foreach ($chatResult as $key => &$chat) {
$chat = $this->chatService->formatNormalize($chat, $userCurrent);
}
} else {
$chatResult = $this->chatService->formatNormalize($chatResult, $userCurrent);
}
$chatResultMain['chats'] = $chatResult;
$chatResultMain['messagesPendingToReadGlobalCounter'] = $this->chatService
->calculateMessagesPendingToReadForAllChats($userCurrent);
$chatResultMain['messagesPendingToReadGlobalTicketCounter'] = $this->chatService
->calculateMessagesPendingToReadForTypeChats($userCurrent, Chat::TYPE_TICKET);
$chatResultMain['messagesPendingToReadGlobalTaskCounter'] = $this->chatService
->calculateMessagesPendingToReadForTypeChats($userCurrent, Chat::TYPE_TASK);
$chatResultMain['messagesGlobalCounter'] = $this->chatService
->calculateMessagesTotalForAllChats($userCurrent);
$event->setControllerResult(json_encode($chatResultMain));
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_SERIALIZE]
];
}
}