<?php
declare(strict_types=1);
namespace App\Manager\Api;
use App\Client\Api\ApiDynamicClientInterface;
use App\Manager\Search\SearchManagerInterface;
use App\Service\ResponseGeneratorInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
/**
* Class ApiManager
*/
final class ApiManager implements ApiManagerInterface
{
private const TEMPLATE_PROGRAM = '/static/et-vous/programme-tv.html.twig';
private const MAX_AGE = 300;
public const OP_ARCHIVED = 'Archivee';
public const OPRATIONS = 'operation';
public const SEARCH = 'recherche';
public const PROGRAMTV = 'programme-tv';
public const ACCOUNT_DELETED = 'account_deleted';
/**
* ApiManager constructor.
*/
public function __construct(
public array $config,
private readonly SearchManagerInterface $searchManager,
private readonly ApiDynamicClientInterface $apiDynamicClient,
private readonly ResponseGeneratorInterface $responseGenerator,
private readonly Environment $twig
) {
}
/**
* {@inheritdoc}
*/
public function getApi(?string $endpoint, ?string $queryString): string
{
return "{$this->config["url_api"]}ftvev_api{$endpoint}{$queryString}";
}
/**
* {@inheritdoc}
*/
public function getAssetsPath(string $mode, string $endPoint): string
{
return "{$this->config["assets_path"]}/mockups/{$mode}/{$endPoint}.json";
}
/**
* {@inheritdoc}
*/
public function getResponseDynamic(string $url): array
{
return $this->apiDynamicClient->findDynamicPages($url);
}
/**
* {@inheritdoc}
*/
public function getResponseProgram(array $apiResponse, array $staticResponse = []): Response
{
$apiResponse = ApiManager::mergeApiResponse($apiResponse, $staticResponse);
$response = $this->responseGenerator->create((int) $apiResponse['code'], []);
$template = $this->twig->render(self::TEMPLATE_PROGRAM, $apiResponse);
$response->setContent($template);
$this->addMaxAge($response);
return $response;
}
/**
* {@inheritdoc}
*/
public function getApiSearchResponse(RequestStack $request, array $querySearch, array $apiResponse): array
{
[$searchResult, $searchQuery] = $this->searchManager->getSearchApi($request, $querySearch);
$apiResponse['search_result'] = $searchResult;
$apiResponse['search_query'] = $searchQuery;
return $apiResponse;
}
/**
* {@inheritdoc}
*/
public function renderTemplate(array $apiResponse, string $mode, array $headers = []): Response
{
$apiResponse = array_merge(
['code' => Response::HTTP_OK, 'universe' => '', 'layout' => '', 'template' => ''],
$apiResponse
);
$template = self::buildTemplatePath($mode, $apiResponse['universe'], $apiResponse['template']);
$response = $this->responseGenerator->create((int) $apiResponse['code'], $headers);
$render = $this->twig->render($template, $apiResponse);
$response->setContent($render);
$this->addMaxAge($response);
return $response;
}
/**
* {@inheritdoc}
*/
public function getPlayer(): array
{
return [
$this->getResponseDynamic($this->getApi(sprintf("/et-vous/%s", self::PROGRAMTV), null)),
$this->config['authorized_url_player'],
];
}
/**
* {@inheritdoc}
*/
public static function buildTemplatePath(string $mode, string $universe, string $template): string
{
if (empty($template)) {
throw new NotFoundHttpException();
}
$universe = empty($universe) ? '' : sprintf('%s%s', $universe, DIRECTORY_SEPARATOR);
return sprintf('%s%s%s%s.html.twig', $mode, DIRECTORY_SEPARATOR, $universe, $template);
}
/**
* {@inheritdoc}
*/
public static function redirectResponse(array $apiResponse): RedirectResponse
{
return new RedirectResponse(self::getPath($apiResponse), Response::HTTP_MOVED_PERMANENTLY);
}
/**
* {@inheritdoc}
*/
public static function getPath(array $apiResponse): string
{
$path = $apiResponse['subrubric']['uri'] ?? $apiResponse['rubric']['uri'];
if (empty($path)) {
$path = '/';
}
return $path;
}
/**
* {@inheritdoc}
*/
public static function isOperationArchived(array $apiResponse): bool
{
return self::OP_ARCHIVED === $apiResponse['status'];
}
/**
* {@inheritdoc}
*/
public static function isOperation(array $apiResponse): bool
{
return self::OPRATIONS === $apiResponse['template'] && self::isOperationArchived($apiResponse);
}
/**
* {@inheritdoc}
*/
public static function isSearch(array $apiResponse): bool
{
return self::SEARCH === $apiResponse['template'];
}
/**
* {@inheritdoc}
*/
public static function isProgramTv(array $apiResponse): bool
{
return self::PROGRAMTV === $apiResponse['template'];
}
/**
* {@inheritdoc}
*/
public static function isDeleteAccount(Session $session): bool
{
return ApiManager::ACCOUNT_DELETED === $session->get('notification_type');
}
/**
* {@inheritdoc}
*/
public static function mergeApiResponse(array $apiResponse, array $staticResponse): array
{
return array_merge_recursive(
$apiResponse,
$staticResponse
);
}
/**
* {@inheritdoc}
*/
protected function addMaxAge(Response $response, int $maxAge = self::MAX_AGE): Response
{
$response->headers->set(
AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER,
'true'
);
$response->setMaxAge($maxAge)
->setPrivate()
;
return $response;
}
}