src/Controller/ApiController.php line 195

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Exception;
  4. use App\Manager\Api\ApiManager;
  5. use App\Manager\Api\ApiManagerInterface;
  6. use App\Service\ResponseGeneratorInterface;
  7. use Psr\Cache\CacheItemPoolInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. /**
  13.  * Class ApiController
  14.  */
  15. class ApiController extends BaseController
  16. {
  17.     protected ?string $mockupFile null;
  18.     protected ?string $endpoint null;
  19.     protected ?string $finalPath null;
  20.     protected ?string $queryString null;
  21.     protected array $config = [];
  22.     /**
  23.      * ApiController constructor.
  24.      */
  25.     public function __construct(
  26.         array $config,
  27.         ResponseGeneratorInterface $responseGenerator,
  28.         CacheItemPoolInterface $cacheItemPool,
  29.         private readonly ApiManagerInterface $apiManager
  30.     ) {
  31.         parent::__construct($config$responseGenerator$cacheItemPool);
  32.     }
  33.     /**
  34.      * This pages actions.
  35.      */
  36.     public function pages(RequestStack $requestSession $session): Response
  37.     {
  38.         $querySearch = [];
  39.         $apiResponse $this->getApiResponse($request);
  40.         if (ApiManager::isProgramTv($apiResponse)) {
  41.             return $this->apiManager->getResponseProgram(
  42.                 $apiResponse,
  43.                 $this->setMode(self::MODE_STATIC)->getResponseStatic($request)
  44.             );
  45.         }
  46.         if (ApiManager::isOperation($apiResponse)) {
  47.             return $this->apiManager::redirectResponse($apiResponse);
  48.         }
  49.         if (ApiManager::isSearch($apiResponse)) {
  50.             $apiResponse $this->apiManager->getApiSearchResponse($request$querySearch$apiResponse);
  51.         }
  52.         if (ApiManager::isDeleteAccount($session)) {
  53.             $apiResponse['notification_type'] = 'account_deleted';
  54.             $session->remove('notification_type');
  55.         }
  56.         return $this->apiManager->renderTemplate($apiResponse$this->getMode());
  57.     }
  58.     /**
  59.      * This method allows getting final path.
  60.      */
  61.     protected function getFinalPath(RequestStack $request): ?string
  62.     {
  63.         if (empty($this->finalPath)) {
  64.             $this->initFinalPath($request);
  65.         }
  66.         return $this->finalPath;
  67.     }
  68.     /**
  69.      * Init the final path.
  70.      */
  71.     protected function initFinalPath(RequestStack $request): self
  72.     {
  73.         $endpoint $this->getEndpoint($request);
  74.         if (!str_starts_with($endpoint'/')) {
  75.             $endpoint sprintf('/%s'$endpoint);
  76.         }
  77.         $queryString = (string)$this->queryString;
  78.         if (!empty($queryString)) {
  79.             $queryString "?$queryString";
  80.         }
  81.         $this->finalPath $this->apiManager->getApi($endpoint$queryString);
  82.         return $this;
  83.     }
  84.     /**
  85.      * Load the final file path (internal|remote).
  86.      */
  87.     protected function initMockFile(RequestStack $request): self
  88.     {
  89.         $this->mockupFile $this->apiManager->getAssetsPath(
  90.             $this->getMode(),
  91.             $this->getEndpoint($request)
  92.         );
  93.         return $this;
  94.     }
  95.     /**
  96.      * Load the appropriate json error file.
  97.      */
  98.     protected function getMockFile(RequestStack $request): ?string
  99.     {
  100.         if (empty($this->mockupFile)) {
  101.             $this->initMockFile($request);
  102.         }
  103.         return $this->mockupFile;
  104.     }
  105.     /**
  106.      * Get the file path contents.
  107.      *
  108.      * @return mixed[]
  109.      */
  110.     protected function getResponseStatic(RequestStack $request): array
  111.     {
  112.         $content json_decode((string) @file_get_contents($this->getMockFile($request)));
  113.         if (empty($content)) {
  114.             if ('prod' !== getenv('APP_ENV')) {
  115.                 $content json_decode(
  116.                     (string)@file_get_contents(
  117.                         $this->setMode(self::MODE_LOCAL)
  118.                              ->initMockFile($request)
  119.                              ->getMockFile($request)
  120.                     )
  121.                 );
  122.             } else {
  123.                 throw new NotFoundHttpException();
  124.             }
  125.         }
  126.         return (array)$content;
  127.     }
  128.     /**
  129.      * Get the endpoint.
  130.      */
  131.     protected function getEndpoint(RequestStack $request): ?string
  132.     {
  133.         if (empty($this->endpoint)) {
  134.             $this->initEndpoint($request);
  135.         }
  136.         return $this->endpoint;
  137.     }
  138.     /**
  139.      * Init the endpoint.
  140.      *
  141.      * @return $this
  142.      */
  143.     protected function initEndpoint(RequestStack $request): self
  144.     {
  145.         $request $request->getCurrentRequest();
  146.         $this->endpoint = (string)$request->get('endpoint');
  147.         if ('/' == substr($this->endpoint, -1)) {
  148.             $this->endpoint rtrim($this->endpoint'/');
  149.         }
  150.         if (empty($this->endpoint)) {
  151.             $this->endpoint 'portal';
  152.             throw new NotFoundHttpException();
  153.         }
  154.         $this->queryString $request->getQueryString();
  155.         return $this;
  156.     }
  157.     /**
  158.      * Get response api.
  159.      */
  160.     protected function getApiResponse(RequestStack $request): array
  161.     {
  162.         try {
  163.             $apiResponse $this->apiManager->getResponseDynamic($this->getFinalPath($request));
  164.         } catch (Exception) {
  165.             $apiResponse $this
  166.                 ->setMode(self::MODE_STATIC)
  167.                 ->getResponseStatic($request)
  168.             ;
  169.         }
  170.         return $apiResponse;
  171.     }
  172. }