src/EventSubscriber/Indicator/IndicatorPostSerializerSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 05/08/2019
  6.  * Time: 08:12 PM
  7.  */
  8. namespace App\EventSubscriber\Indicator;
  9. use App\Entity\App\Vendor;
  10. use App\Repository\App\VendorRepository;
  11. use App\Services\UtilsService;
  12. use App\Services\IndicatorService;
  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. class IndicatorPostSerializerSubscriber implements EventSubscriberInterface
  18. {
  19.     private $utilsService;
  20.     private $vendorRepository;
  21.     private $indicatorService;
  22.     public function __construct(
  23.         UtilsService $utilsService,
  24.         VendorRepository $vendorRepository,
  25.         IndicatorService $indicatorService
  26.     ) {
  27.         $this->utilsService $utilsService;
  28.         $this->vendorRepository $vendorRepository;
  29.         $this->indicatorService $indicatorService;
  30.     }
  31.     /**
  32.      * @param ViewEvent $event
  33.      * @throws \Doctrine\ORM\NonUniqueResultException
  34.      */
  35.     public function onKernelView(ViewEvent $event)
  36.     {
  37.         if ($this->utilsService->isAPublicRequest($event)) {
  38.             return;
  39.         }
  40.         $request $event->getRequest();
  41.         $indicatorResult $event->getControllerResult();
  42.         $routes = array(
  43.             'api_indicators_get_collection'
  44.         );
  45.         $route $request->attributes->get('_route');
  46.         if (!in_array($route$routes)) {
  47.             return;
  48.         }
  49.         $vendor null;
  50.         if ($request->query->has('vendor')) {
  51.             $vendor $this->vendorRepository->find($request->query->get('vendor'));
  52.         }
  53.         $requestSearch = [];
  54.         if ($request->query->has('year')) {
  55.             $requestSearch['year'] = $request->query->get('year');
  56.         }
  57.         if ($vendor instanceof Vendor) {
  58.             $indicatorResult json_decode($indicatorResulttrue);
  59.             foreach ($indicatorResult as &$indicator) {
  60.                 $indicator $this->indicatorService->calculateIndicators($indicator$vendor$requestSearch);
  61.             }
  62.             $indicatorResult json_encode($indicatorResult);
  63.             $event->setControllerResult($indicatorResult);
  64.         }
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  70.         ];
  71.     }
  72. }