src/EventSubscriber/ResponseSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;
  4. use App\Entity\CommunityPost;
  5. use App\Entity\CommunityServiceResponse;
  6. use App\Entity\MicroserviceCallInterface;
  7. use App\Entity\NormeResponseInterface;
  8. use App\Service\Api\Community\ResponseService;
  9. use ReflectionException;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  17. class ResponseSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var ResponseService
  21.      */
  22.     private $responseService;
  23.     public function __construct(ResponseService $responseService)
  24.     {
  25.         $this->responseService $responseService;
  26.     }
  27.     /**
  28.      * @param ResponseEvent $event
  29.      * @throws ReflectionException
  30.      * @throws ClientExceptionInterface
  31.      * @throws RedirectionExceptionInterface
  32.      * @throws ServerExceptionInterface
  33.      * @throws TransportExceptionInterface
  34.      */
  35.     public function onKernelResponse(ResponseEvent $event): void
  36.     {
  37.         $request $event->getRequest();
  38.         $object $request->attributes->get('_api_resource_class');
  39.         $routeParams $request->attributes->get('_route_params');
  40.         $routeName is_array($routeParams) && isset($routeParams['_api_collection_operation_name']) ? $routeParams['_api_collection_operation_name'] : null;
  41.         if ($routeName == "clientsales_pdfto_html") {
  42.             return;
  43.         }
  44.         $isObject $object && (new \ReflectionClass($object))->implementsInterface(NormeResponseInterface::class);
  45.         if ($object && $isObject) {
  46.             $response $event->getResponse();
  47.             if ($request->attributes->get('_controller') === 'api_platform.action.get_subresource') {
  48.                 $this->responseService->checkResult($response);
  49.             }
  50.             if (str_contains($response->headers->get('Content-Type'), 'json')) {
  51.                 $responseContent json_decode($response->getContent(), true);
  52.                 $responseDataChange null;
  53.                 if ($object == CommunityPost::class || $object == CommunityServiceResponse::class) {
  54.                     $responseDataChange $this->responseService->formatPostData($response$request);
  55.                 }
  56.                 $data $this->responseService->norme($responseDataChange$response);
  57.                 $paginationData null;
  58.                 if (is_array($responseContent)) {
  59.                     // Check if pagination data is already present in the response
  60.                     $paginationData $this->extractPaginationData($responseContent);
  61.                 }
  62.                 if ($paginationData) {
  63.                     // Use pagination data from the response
  64.                     $data array_merge($data$paginationData);
  65.                 } elseif (($_data $request->attributes->get('data')) && $_data instanceof Paginator) {
  66.                     // Use Paginator for standard API Platform responses
  67.                     if (!is_null($data['data']) && array_key_first($data['data']) === && !(new \ReflectionClass($object))->implementsInterface(MicroserviceCallInterface::class)) {
  68.                         $data['total'] = $_data->getTotalItems();
  69.                         $data['current_page'] = $_data->getCurrentPage();
  70.                         $data['last_page'] = $_data->getLastPage();
  71.                     }
  72.                 } else {
  73.                     // Fallback for custom controllers without pagination
  74.                     $data['total'] = is_array($data['data']) ? count($data['data']) : 1;
  75.                     $data['current_page'] = 1;
  76.                     $data['last_page'] = 1;
  77.                 }
  78.                 $response->setContent(json_encode($data));
  79.             }
  80.         }
  81.     }
  82.     private function extractPaginationData(array $responseContent): ?array
  83.     {
  84.         $paginationKeys = ['total''current_page''last_page'];
  85.         $paginationData array_intersect_key($responseContentarray_flip($paginationKeys));
  86.         return !empty($paginationData) ? $paginationData null;
  87.     }
  88.     /**
  89.      * @return array|string[]
  90.      */
  91.     public static function getSubscribedEvents(): array
  92.     {
  93.         return [
  94.             KernelEvents::RESPONSE => 'onKernelResponse',
  95.         ];
  96.     }
  97. }