<?php
namespace App\Controller;
use App\Application\Service\Helper\LinkGenerator;
use App\Application\Service\Session\SessionService;
use App\Service\LegacyControllerService;
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
{
public const TAXONOMY_CONTROLLER = 'taxonomyController';
public const MANUFACTURER_CONTROLLER = 'manufacturerController';
public const PRODUCT_CONTROLLER = 'productController';
public const TAG_CONTROLLER = 'tagController';
private LoggerInterface $logger;
private SessionService $sessionService;
private LegacyControllerService $legacyControllerService;
private LinkGenerator $linkGenerator;
public function __construct(
LoggerInterface $logger,
SessionService $sessionService,
LegacyControllerService $legacyControllerService,
LinkGenerator $linkGenerator
) {
$this->logger = $logger;
$this->sessionService = $sessionService;
$this->legacyControllerService = $legacyControllerService;
$this->linkGenerator = $linkGenerator;
}
/**
* @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
{
/** @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 self::TAXONOMY_CONTROLLER:
return $this->linkGenerator->getTaxonomyLink($parameter);
case self::MANUFACTURER_CONTROLLER:
return $this->linkGenerator->getManufacturerLink($parameter);
case self::PRODUCT_CONTROLLER:
return $this->linkGenerator->getProductLink($parameter);
case self::TAG_CONTROLLER:
return $this->generateUrl('shop_tag', ['url' => $parameter, 'lang' => $this->sessionService->getLocale()]);
}
return null;
}
}