src/Controller/LegacyController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Application\Service\Helper\LinkGenerator;
  4. use App\Application\Service\Session\SessionService;
  5. use App\Service\LegacyControllerService;
  6. use Psr\Log\LoggerInterface;
  7. use Request as LegacyRequest;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class LegacyController extends AbstractController
  13. {
  14. public const TAXONOMY_CONTROLLER = 'taxonomyController';
  15. public const MANUFACTURER_CONTROLLER = 'manufacturerController';
  16. public const PRODUCT_CONTROLLER = 'productController';
  17. public const TAG_CONTROLLER = 'tagController';
  18. public function __construct(
  19. private readonly LoggerInterface $logger,
  20. private readonly SessionService $sessionService,
  21. private readonly LegacyControllerService $legacyControllerService,
  22. private readonly LinkGenerator $linkGenerator,
  23. ) {
  24. }
  25. /**
  26. * @Route("/{anything}", name="legacy_front_endpoints", priority=-5, requirements={"anything"="^((?!rest/).)*$"})
  27. */
  28. public function legacyFrontEndpoints(Request $request, string $anything): Response
  29. {
  30. $controllerData = $request->request->all('legacy_controller');
  31. if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  32. throw $this->createNotFoundException();
  33. }
  34. if ($newRoute = $this->redirectToNewRoutes(
  35. $controllerData['controller'],
  36. reset($controllerData['args'])
  37. )) {
  38. return $this->redirect($newRoute, Response::HTTP_MOVED_PERMANENTLY);
  39. }
  40. $legacyController = null;
  41. $parameter = $this->sessionService->get('id_model') ?? reset($controllerData['args']) ?? null;
  42. try {
  43. if ($parameter) {
  44. $legacyController = new $controllerData['controller']($parameter);
  45. } else {
  46. $legacyController = new $controllerData['controller']();
  47. }
  48. } catch (\Throwable $t) {
  49. $this->logger->critical('Error loading legacy controller: '.$anything.' '.$t->getMessage());
  50. }
  51. $controllerMethod = $controllerData['method'] ?? null;
  52. if (!$legacyController || !\is_string($controllerMethod) || !method_exists($legacyController, $controllerMethod)) {
  53. throw $this->createNotFoundException();
  54. }
  55. call_user_func_array([$legacyController, $controllerData['method']], $controllerData['args']);
  56. return new Response($this->legacyControllerService->getHtmlFromLegacyController($legacyController));
  57. }
  58. /**
  59. * @param int|string $parameter
  60. */
  61. protected function redirectToNewRoutes(string $controller, $parameter): ?string
  62. {
  63. switch ($controller) {
  64. case self::TAXONOMY_CONTROLLER:
  65. return $this->linkGenerator->getTaxonomyLink((int)$parameter);
  66. case self::MANUFACTURER_CONTROLLER:
  67. return $this->linkGenerator->getManufacturerLink((int)$parameter);
  68. case self::PRODUCT_CONTROLLER:
  69. return $this->linkGenerator->getProductLink((int)$parameter);
  70. case self::TAG_CONTROLLER:
  71. return $this->generateUrl('shop_tag', ['url' => $parameter, 'lang' => $this->sessionService->getLocale()]);
  72. }
  73. return null;
  74. }
  75. }