<?php
/**
* Created by PhpStorm.
* User: SUSAN MEDINA
* Date: 12/08/2019
* Time: 12:15 PM
*/
namespace App\EventSubscriber\Report;
use App\Entity\App\Vendor;
use App\Repository\App\VendorRepository;
use App\Services\UtilsService;
use App\Services\ReportService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class ReportPostSerializerSubscriber implements EventSubscriberInterface
{
private $utilsService;
private $vendorRepository;
private $reportService;
private $authorizationChecker;
public function __construct(
UtilsService $utilsService,
VendorRepository $vendorRepository,
ReportService $reportService,
AuthorizationCheckerInterface $checker
) {
$this->utilsService = $utilsService;
$this->vendorRepository = $vendorRepository;
$this->reportService = $reportService;
$this->authorizationChecker = $checker;
}
/**
* @param ViewEvent $event
*/
public function onKernelView(ViewEvent $event)
{
if ($this->utilsService->isAPublicRequest($event)) {
return;
}
$request = $event->getRequest();
$reportResult = $event->getControllerResult();
$routes = array(
'api_reports_get_collection'
);
$route = $request->attributes->get('_route');
if (!in_array($route, $routes)) {
return;
}
$vendor = null;
if ($request->query->has('vendor')) {
$vendor = $this->vendorRepository->find($request->query->get('vendor'));
}
$reportResult = json_decode($reportResult, true);
foreach ($reportResult as &$report) {
$report = $this->reportService->addRouteExportFormat($report, $vendor);
}
$reportResult = json_encode($reportResult);
$event->setControllerResult($reportResult);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onKernelView', EventPriorities::POST_SERIALIZE]
];
}
}