src/EventSubscriber/Resource/ResourcePostSerializerSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 25/04/2019
  6.  * Time: 11:03 AM
  7.  */
  8. namespace App\EventSubscriber\Resource;
  9. use App\Entity\App\ResourceAndTool;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use ApiPlatform\Core\EventListener\EventPriorities;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use App\Services\ResourceService;
  15. class ResourcePostSerializerSubscriber implements EventSubscriberInterface
  16. {
  17.     private $resourceService;
  18.     public function __construct(
  19.         ResourceService $resourceService
  20.     ) {
  21.         $this->resourceService $resourceService;
  22.     }
  23.     public function onKernelView(ViewEvent $event)
  24.     {
  25.         $request $event->getRequest();
  26.         $resourceResult $event->getControllerResult();
  27.         $routes = array(
  28.             'api_resources_get_collection',
  29.             'api_resources_post_collection',
  30.             'api_resources_get_item',
  31.             'api_resources_put_item',
  32.             'api_vendors_resources_get_subresource',
  33.             'api_locations_resources_get_subresource'
  34.         );
  35.         $routesCollection = array(
  36.             'api_resources_get_collection',
  37.             'api_vendors_resources_get_subresource',
  38.             'api_locations_resources_get_subresource'
  39.         );
  40.         $route $request->attributes->get('_route');
  41.         if (!in_array($route$routes))
  42.             return;
  43.         $withChildren true;
  44.         $withParent true;
  45.         $category $request->query->get('category');
  46.         $location $request->query->get('location');
  47.         if ($category || $location || $route == 'api_locations_resources_get_subresource') {
  48.             $withChildren false;
  49.         }
  50.         if (!ResourceAndTool::SHOW_CHILDREN) {
  51.             $withChildren false;
  52.             $withParent false;
  53.         }
  54.         $resourceResult json_decode($resourceResulttrue);
  55.         if (in_array($route$routesCollection)) {
  56.             foreach ($resourceResult as &$resource) {
  57.                 $resource $this->resourceService->formatNormalize(
  58.                     $resource,
  59.                     false,
  60.                     $withChildren,
  61.                     $route
  62.                 );
  63.             }
  64.             $resourceResult json_encode($resourceResult);
  65.         } else {
  66.             $resourceResult $this->resourceService->formatNormalize(
  67.                 $resourceResult,
  68.                 $withParent,
  69.                 $withChildren,
  70.                 $route
  71.             );
  72.             $resourceResult json_encode($resourceResult);
  73.         }
  74.         $event->setControllerResult($resourceResult);
  75.     }
  76.     public static function getSubscribedEvents()
  77.     {
  78.         return [
  79.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  80.         ];
  81.     }
  82. }