src/Manager/Api/ApiManager.php line 188

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Manager\Api;
  4. use App\Client\Api\ApiDynamicClientInterface;
  5. use App\Manager\Search\SearchManagerInterface;
  6. use App\Service\ResponseGeneratorInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Twig\Environment;
  14. /**
  15.  * Class ApiManager
  16.  */
  17. final class ApiManager implements ApiManagerInterface
  18. {
  19.     private const TEMPLATE_PROGRAM '/static/et-vous/programme-tv.html.twig';
  20.     private const MAX_AGE 300;
  21.     public const OP_ARCHIVED 'Archivee';
  22.     public const OPRATIONS 'operation';
  23.     public const SEARCH 'recherche';
  24.     public const PROGRAMTV 'programme-tv';
  25.     public const ACCOUNT_DELETED 'account_deleted';
  26.     /**
  27.      * ApiManager constructor.
  28.      */
  29.     public function __construct(
  30.         public array $config,
  31.         private readonly SearchManagerInterface $searchManager,
  32.         private readonly ApiDynamicClientInterface $apiDynamicClient,
  33.         private readonly ResponseGeneratorInterface $responseGenerator,
  34.         private readonly Environment $twig
  35.     ) {
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function getApi(?string $endpoint, ?string $queryString): string
  41.     {
  42.         return "{$this->config["url_api"]}ftvev_api{$endpoint}{$queryString}";
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function getAssetsPath(string $modestring $endPoint): string
  48.     {
  49.         return "{$this->config["assets_path"]}/mockups/{$mode}/{$endPoint}.json";
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function getResponseDynamic(string $url): array
  55.     {
  56.         return $this->apiDynamicClient->findDynamicPages($url);
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getResponseProgram(array $apiResponse, array $staticResponse = []): Response
  62.     {
  63.         $apiResponse ApiManager::mergeApiResponse($apiResponse$staticResponse);
  64.         $response $this->responseGenerator->create((int) $apiResponse['code'], []);
  65.         $template $this->twig->render(self::TEMPLATE_PROGRAM$apiResponse);
  66.         $response->setContent($template);
  67.         $this->addMaxAge($response);
  68.         return $response;
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getApiSearchResponse(RequestStack $request, array $querySearch, array $apiResponse): array
  74.     {
  75.         [$searchResult$searchQuery] = $this->searchManager->getSearchApi($request$querySearch);
  76.         $apiResponse['search_result'] = $searchResult;
  77.         $apiResponse['search_query']  = $searchQuery;
  78.         return $apiResponse;
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function renderTemplate(array $apiResponsestring $mode, array $headers = []): Response
  84.     {
  85.         $apiResponse array_merge(
  86.             ['code' => Response::HTTP_OK'universe' => '''layout' => '''template' => ''],
  87.             $apiResponse
  88.         );
  89.         $template self::buildTemplatePath($mode$apiResponse['universe'], $apiResponse['template']);
  90.         $response $this->responseGenerator->create((int) $apiResponse['code'], $headers);
  91.         $render $this->twig->render($template$apiResponse);
  92.         $response->setContent($render);
  93.         $this->addMaxAge($response);
  94.         return $response;
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function getPlayer(): array
  100.     {
  101.         return [
  102.             $this->getResponseDynamic($this->getApi(sprintf("/et-vous/%s"self::PROGRAMTV), null)),
  103.             $this->config['authorized_url_player'],
  104.         ];
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public static function buildTemplatePath(string $modestring $universestring $template): string
  110.     {
  111.         if (empty($template)) {
  112.             throw new NotFoundHttpException();
  113.         }
  114.         $universe = empty($universe) ? '' sprintf('%s%s'$universeDIRECTORY_SEPARATOR);
  115.         return sprintf('%s%s%s%s.html.twig'$modeDIRECTORY_SEPARATOR$universe$template);
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public static function redirectResponse(array $apiResponse): RedirectResponse
  121.     {
  122.         return new RedirectResponse(self::getPath($apiResponse), Response::HTTP_MOVED_PERMANENTLY);
  123.     }
  124.     /**
  125.      * {@inheritdoc}
  126.      */
  127.     public static function getPath(array $apiResponse): string
  128.     {
  129.         $path $apiResponse['subrubric']['uri'] ?? $apiResponse['rubric']['uri'];
  130.         if (empty($path)) {
  131.             $path '/';
  132.         }
  133.         return $path;
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public static function isOperationArchived(array $apiResponse): bool
  139.     {
  140.         return self::OP_ARCHIVED === $apiResponse['status'];
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public static function isOperation(array $apiResponse): bool
  146.     {
  147.         return self::OPRATIONS === $apiResponse['template'] && self::isOperationArchived($apiResponse);
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  151.      */
  152.     public static function isSearch(array $apiResponse): bool
  153.     {
  154.         return self::SEARCH === $apiResponse['template'];
  155.     }
  156.     /**
  157.      * {@inheritdoc}
  158.      */
  159.     public static function isProgramTv(array $apiResponse): bool
  160.     {
  161.         return self::PROGRAMTV === $apiResponse['template'];
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public static function isDeleteAccount(Session $session): bool
  167.     {
  168.         return ApiManager::ACCOUNT_DELETED === $session->get('notification_type');
  169.     }
  170.     /**
  171.      * {@inheritdoc}
  172.      */
  173.     public static function mergeApiResponse(array $apiResponse, array $staticResponse): array
  174.     {
  175.         return array_merge_recursive(
  176.             $apiResponse,
  177.             $staticResponse
  178.         );
  179.     }
  180.     /**
  181.      * {@inheritdoc}
  182.      */
  183.     protected function addMaxAge(Response $responseint $maxAge self::MAX_AGE): Response
  184.     {
  185.         $response->headers->set(
  186.             AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER,
  187.             'true'
  188.         );
  189.         $response->setMaxAge($maxAge)
  190.                  ->setPrivate()
  191.         ;
  192.         return $response;
  193.     }
  194. }