<?phpnamespace App\EventSubscriber;use ApiPlatform\Core\Bridge\Doctrine\Orm\Paginator;use App\Entity\CommunityPost;use App\Entity\CommunityServiceResponse;use App\Entity\MicroserviceCallInterface;use App\Entity\NormeResponseInterface;use App\Service\Api\Community\ResponseService;use ReflectionException;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;class ResponseSubscriber implements EventSubscriberInterface{ /** * @var ResponseService */ private $responseService; public function __construct(ResponseService $responseService) { $this->responseService = $responseService; } /** * @param ResponseEvent $event * @throws ReflectionException * @throws ClientExceptionInterface * @throws RedirectionExceptionInterface * @throws ServerExceptionInterface * @throws TransportExceptionInterface */ public function onKernelResponse(ResponseEvent $event): void { $request = $event->getRequest(); $object = $request->attributes->get('_api_resource_class'); $routeParams = $request->attributes->get('_route_params'); $routeName = is_array($routeParams) && isset($routeParams['_api_collection_operation_name']) ? $routeParams['_api_collection_operation_name'] : null; if ($routeName == "clientsales_pdfto_html") { return; } $isObject = $object && (new \ReflectionClass($object))->implementsInterface(NormeResponseInterface::class); if ($object && $isObject) { $response = $event->getResponse(); if ($request->attributes->get('_controller') === 'api_platform.action.get_subresource') { $this->responseService->checkResult($response); } if (str_contains($response->headers->get('Content-Type'), 'json')) { $responseContent = json_decode($response->getContent(), true); $responseDataChange = null; if ($object == CommunityPost::class || $object == CommunityServiceResponse::class) { $responseDataChange = $this->responseService->formatPostData($response, $request); } $data = $this->responseService->norme($responseDataChange, $response); $paginationData = null; if (is_array($responseContent)) { // Check if pagination data is already present in the response $paginationData = $this->extractPaginationData($responseContent); } if ($paginationData) { // Use pagination data from the response $data = array_merge($data, $paginationData); } elseif (($_data = $request->attributes->get('data')) && $_data instanceof Paginator) { // Use Paginator for standard API Platform responses if (!is_null($data['data']) && array_key_first($data['data']) === 0 && !(new \ReflectionClass($object))->implementsInterface(MicroserviceCallInterface::class)) { $data['total'] = $_data->getTotalItems(); $data['current_page'] = $_data->getCurrentPage(); $data['last_page'] = $_data->getLastPage(); } } else { // Fallback for custom controllers without pagination $data['total'] = is_array($data['data']) ? count($data['data']) : 1; $data['current_page'] = 1; $data['last_page'] = 1; } $response->setContent(json_encode($data)); } } } private function extractPaginationData(array $responseContent): ?array { $paginationKeys = ['total', 'current_page', 'last_page']; $paginationData = array_intersect_key($responseContent, array_flip($paginationKeys)); return !empty($paginationData) ? $paginationData : null; } /** * @return array|string[] */ public static function getSubscribedEvents(): array { return [ KernelEvents::RESPONSE => 'onKernelResponse', ]; }}