src/EventSubscriber/Report/ReportPostSerializerSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 12/08/2019
  6.  * Time: 12:15 PM
  7.  */
  8. namespace App\EventSubscriber\Report;
  9. use App\Entity\App\Vendor;
  10. use App\Repository\App\VendorRepository;
  11. use App\Services\UtilsService;
  12. use App\Services\ReportService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use ApiPlatform\Core\EventListener\EventPriorities;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. class ReportPostSerializerSubscriber implements EventSubscriberInterface
  19. {
  20.     private $utilsService;
  21.     private $vendorRepository;
  22.     private $reportService;
  23.     private $authorizationChecker;
  24.     public function __construct(
  25.         UtilsService $utilsService,
  26.         VendorRepository $vendorRepository,
  27.         ReportService $reportService,
  28.         AuthorizationCheckerInterface $checker
  29.     ) {
  30.         $this->utilsService $utilsService;
  31.         $this->vendorRepository $vendorRepository;
  32.         $this->reportService $reportService;
  33.         $this->authorizationChecker $checker;
  34.     }
  35.     /**
  36.      * @param ViewEvent $event
  37.      */
  38.     public function onKernelView(ViewEvent $event)
  39.     {
  40.         if ($this->utilsService->isAPublicRequest($event)) {
  41.             return;
  42.         }
  43.         $request $event->getRequest();
  44.         $reportResult $event->getControllerResult();
  45.         $routes = array(
  46.             'api_reports_get_collection'
  47.         );
  48.         $route $request->attributes->get('_route');
  49.         if (!in_array($route$routes)) {
  50.             return;
  51.         }
  52.         $vendor null;
  53.         if ($request->query->has('vendor')) {
  54.             $vendor $this->vendorRepository->find($request->query->get('vendor'));
  55.         }
  56.         $reportResult json_decode($reportResulttrue);
  57.         foreach ($reportResult as &$report) {
  58.             $report $this->reportService->addRouteExportFormat($report$vendor);
  59.         }
  60.         $reportResult json_encode($reportResult);
  61.         $event->setControllerResult($reportResult);
  62.     }
  63.     public static function getSubscribedEvents()
  64.     {
  65.         return [
  66.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  67.         ];
  68.     }
  69. }