src/EventSubscriber/MaintenanceElement/MaintenanceElementPostSerializerSubscriber.php line 27

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