<?php
namespace App\Controller;
use App\Application\Service\Customer\CustomerStatsService;
use App\Application\Service\Helper\LinkGenerator;
use App\Application\Service\Session\SessionService;
use App\Service\EnvironmentService;
use App\ViewManager\Landing\CmsService;
use App\ViewManager\Landing\LandingService;
use Psr\Log\LoggerInterface;
use Request as LegacyRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class LegacyController extends AbstractController
{
private CustomerStatsService $customerStatsService;
private LoggerInterface $logger;
private SessionService $sessionService;
private LinkGenerator $linkGenerator;
private LandingService $landingService;
private CmsService $cmsService;
private EnvironmentService $environmentService;
public function __construct(
CustomerStatsService $customerStatsService,
LoggerInterface $logger,
SessionService $sessionService,
LinkGenerator $linkGenerator,
LandingService $landingService,
CmsService $cmsService,
EnvironmentService $environmentService
) {
$this->customerStatsService = $customerStatsService;
$this->logger = $logger;
$this->sessionService = $sessionService;
$this->linkGenerator = $linkGenerator;
$this->landingService = $landingService;
$this->cmsService = $cmsService;
$this->environmentService = $environmentService;
}
/**
* @Route("/service{anything}", name="legacy_service_endpoints", priority="-3", requirements={"anything"=".*"})
* @Route("/admin{anything}", name="legacy_admin_endpoints", priority="-3", requirements={"anything"=".*"})
*/
public function legacyNotFrontEndpoints(Request $request): Response
{
$uri = $request->getPathInfo();
if ($this->environmentService->isApiSandbox() && !\str_contains($uri, 'order/payment')) {
return new JsonResponse(['code' => 'Bad request', 'message' => Response::HTTP_BAD_REQUEST, ], Response::HTTP_BAD_REQUEST);
}
$this->sessionService->set('cms_new', false);
$controllerData = $request->request->get('legacy_controller');
if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
throw $this->createNotFoundException();
}
if (LegacyRequest::MAINTENANCE_CONTROLLER === $controllerData['controller']) {
throw new ServiceUnavailableHttpException();
}
$legacyController = new $controllerData['controller']();
if (!method_exists($legacyController, $controllerData['method'])) {
throw $this->createNotFoundException();
}
call_user_func_array([$legacyController, $controllerData['method']], $controllerData['args']);
if (empty($legacyController->getView())) {
$this->sessionService->deleteFlashData();
return new Response('');
// throw $this->createNotFoundException();
}
$html = $legacyController->display();
$this->sessionService->deleteFlashData();
return new Response($html);
}
/**
* @Route("/{anything}", name="legacy_front_endpoints", priority=-5, requirements={"anything"="^((?!rest/).)*$"})
*/
public function legacyFrontEndpoints(Request $request, string $anything): Response
{
$uri = $request->getPathInfo();
if ($this->environmentService->isApiSandbox() && !\str_contains($uri, 'order/payment')) {
return new JsonResponse(['code' => 'Bad request', 'message' => Response::HTTP_BAD_REQUEST, ], Response::HTTP_BAD_REQUEST);
}
$cmsLanguage = $this->cmsService->getCmsInformation($anything);
$this->sessionService->set('cms_new', false);
if ($cmsLanguage && !$cmsLanguage->getCms()->getLegacy()) {
$this->sessionService->set('id_model', $cmsLanguage->getCms()->getId());
$this->sessionService->set('cms_new', true);
return $this->render(
$this->landingService->getLandingBaseTemplate($cmsLanguage),
$this->landingService->getParametersForBaseCms($cmsLanguage, (bool)$request->get('update', false))
);
}
$controllerData = $request->request->get('legacy_controller');
if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
throw $this->createNotFoundException();
}
if (LegacyRequest::MAINTENANCE_CONTROLLER === $controllerData['controller']) {
throw new ServiceUnavailableHttpException();
}
$legacyController = null;
try {
if ($this->sessionService->get('id_model')) {
$legacyController = new $controllerData['controller']($this->sessionService->get('id_model'));
} elseif (isset($controllerData['args'][0])) {
$legacyController = new $controllerData['controller']($controllerData['args'][0]);
} else {
$legacyController = new $controllerData['controller']();
}
} catch (\Throwable $t) {
$this->logger->critical('Error loading legacy controller: '.$anything.' '.$t->getMessage());
}
if (!method_exists($legacyController, $controllerData['method'])) {
throw $this->createNotFoundException();
}
call_user_func_array([$legacyController, $controllerData['method']], $controllerData['args']);
/** @var Environment */
$twig = $this->container->get('twig');
$headData = $legacyController->head();
if (null === $headData || $legacyController->getDisplay_only_view()) {
$html = $legacyController->display();
return new Response($html);
}
$customerStats = $this->customerStatsService->getCustomerStatsId();
$userId = $customerStats->getId();
$currentUrl = $this->getCurrentUrl($headData);
$renderedTemplateData = [
'bodyHtml' => $legacyController->display(true),
'headHtml' => $twig->render('front/base/legacy_head.html.twig', [
'data' => $headData,
'current_url' => $currentUrl,
'dataLayerUserData' => [
'userId' => $userId,
'pageName' => $request->getSession()->get('url'),
],
]),
'shopMenuHtml' => $legacyController->isShopMenu() ? $legacyController->mainNav() : null,
'footerJs' => $legacyController->prepareJs(true),
];
$renderedTemplate = $this->renderview(
'front/base/base_'.($legacyController->isShopMenu() ? 'shop' : 'corporate').'_legacy.html.twig',
$renderedTemplateData
);
$this->sessionService->deleteFlashData();
return new Response($renderedTemplate);
}
protected function getCurrentUrl(\Partial $headData): string
{
$currentUrl = '';
if (!empty($headData->current_url_fb)) {
return $headData->current_url_fb;
}
if (!empty($headData->current_url)) {
return $this->linkGenerator->getCurrentLink(
$headData->current_url,
$this->sessionService->get('id_lang')
);
}
return $currentUrl;
}
}