<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 08/09/2019
* Time: 11:33 AM
*/
namespace App\EventSubscriber\Log;
use App\Services\LogService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\HttpKernel\KernelEvents;
class LogPostSerializerSubscriber implements EventSubscriberInterface
{
private $logService;
public function __construct(LogService $logService)
{
$this->logService = $logService;
}
/**
* @param ViewEvent $event
*/
public function onKernelView(ViewEvent $event)
{
$request = $event->getRequest();
$logResult = $event->getControllerResult();
$routes = array('api_log_entries_get_collection');
$route = $request->attributes->get('_route');
if (!in_array($route, $routes)) {
return;
}
$logResult = json_decode($logResult, true);
foreach ($logResult as &$log) {
$log = $this->logService->formatNormalize($log);
}
$logResult = json_encode($logResult);
$event->setControllerResult($logResult);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_SERIALIZE]
];
}
}