src/Events/ResponseSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use App\Entity\CommunityPost;
  4. use App\Entity\GardenProjectDocument;
  5. use ReflectionException;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use App\Services\Api\Community\ResponseService;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  10. use App\Entity\CommunityServiceResponse;
  11. use App\Entity\NormeResponseInterface;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ResponseSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var ResponseService
  18.      */
  19.     private $responseService;
  20.     public function __construct(ResponseService $responseService)
  21.     {
  22.         $this->responseService $responseService;
  23.     }
  24.     /**
  25.      * @param ResponseEvent $event
  26.      * @return void
  27.      * @throws ReflectionException
  28.      */
  29.     public function onKernelResponse(ResponseEvent $event): void
  30.     {
  31. //        dd($event);
  32.         $request  $event->getRequest();
  33.         $object   $request->attributes->get('_api_resource_class');
  34.         $isObject $object && (new \ReflectionClass($object))->implementsInterface(NormeResponseInterface::class);
  35.         if ($object and $isObject) {
  36.             $response $event->getResponse();
  37.             if ($request->attributes->get('_controller') === 'api_platform.action.get_subresource') {
  38.                 $this->responseService->checkResult($response);
  39.             }
  40.             if (str_contains($response->headers->get('Content-Type'), 'json')) {
  41.                 $responseDataChange null;
  42.                 if ($object == CommunityPost::class || $object == CommunityServiceResponse::class) {
  43.                     $responseDataChange $this->responseService->formatPostData($response$request);
  44.                 }
  45.                 $data $this->responseService->norme($responseDataChange$response);
  46.                
  47.                 if (($_data $request->attributes->get('data')) && $_data instanceof Paginator) {
  48.                    if(!is_null($data['data']) && array_key_first($data['data']) === 0) {
  49.                        $data['total'] = $_data->getTotalItems();
  50.                        $data['current_page'] = $_data->getCurrentPage();
  51.                        $data['last_page'] = $_data->getLastPage();
  52.                    }
  53.                 }
  54.                 $response->setContent(json_encode($data));
  55.             }
  56.         }
  57.     }
  58.     /**
  59.      * @return array|string[]
  60.      */
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             KernelEvents::RESPONSE => 'onKernelResponse',
  65.         ];
  66.     }
  67. }