<?php
namespace App\Controller;
use App\Application\Service\Helper\LinkGenerator;
use App\Application\Service\Session\SessionService;
use App\Service\LegacyControllerService;
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\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\Routing\Annotation\Route;
class LegacyController extends AbstractController
{
private LoggerInterface $logger;
private SessionService $sessionService;
private LinkGenerator $linkGenerator;
private LandingService $landingService;
private CmsService $cmsService;
private LegacyControllerService $legacyControllerService;
public function __construct(
LoggerInterface $logger,
SessionService $sessionService,
LinkGenerator $linkGenerator,
LandingService $landingService,
CmsService $cmsService,
LegacyControllerService $legacyControllerService
) {
$this->logger = $logger;
$this->sessionService = $sessionService;
$this->linkGenerator = $linkGenerator;
$this->landingService = $landingService;
$this->cmsService = $cmsService;
$this->legacyControllerService = $legacyControllerService;
}
/**
* @Route("/admin{anything}", name="legacy_admin_endpoints", priority="-3", requirements={"anything"=".*"})
*/
public function legacyNotFrontEndpoints(Request $request): Response
{
$this->sessionService->set('cms_new', false);
/** @var array $controllerData */
$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
{
$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)
);
}
/** @var array $controllerData */
$controllerData = $request->request->get('legacy_controller');
if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
throw $this->createNotFoundException();
}
if ($newRoute = $this->redirectToNewRoutes(
$controllerData['controller'],
reset($controllerData['args'])
)) {
return $this->redirect($newRoute, Response::HTTP_MOVED_PERMANENTLY);
}
$legacyController = null;
$parameter = $this->sessionService->get('id_model') ?? reset($controllerData['args']) ?? null;
try {
if ($parameter) {
$legacyController = new $controllerData['controller']($parameter);
} 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']);
return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
}
/**
* @param int|string $parameter
*/
protected function redirectToNewRoutes(string $controller, $parameter): ?string
{
switch ($controller) {
case \taxonomyController::class:
return $this->linkGenerator->getTaxonomyLink($parameter);
case \manufacturerController::class:
return $this->linkGenerator->getManufacturerLink($parameter);
case \productController::class:
return $this->linkGenerator->getProductLink($parameter);
case \tagController::class:
return $this->generateUrl('shop_tag', ['url' => $parameter, 'lang' => $this->sessionService->getLocale()]);
}
return null;
}
}