src/Controller/LegacyController.php line 120

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Application\Service\Customer\CustomerStatsService;
  4. use App\Application\Service\Helper\LinkGenerator;
  5. use App\Application\Service\Session\SessionService;
  6. use App\Service\EnvironmentService;
  7. use App\ViewManager\Landing\CmsService;
  8. use App\ViewManager\Landing\LandingService;
  9. use Psr\Log\LoggerInterface;
  10. use Request as LegacyRequest;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Twig\Environment;
  18. class LegacyController extends AbstractController
  19. {
  20.     private CustomerStatsService $customerStatsService;
  21.     private LoggerInterface $logger;
  22.     private SessionService $sessionService;
  23.     private LinkGenerator $linkGenerator;
  24.     private LandingService $landingService;
  25.     private CmsService $cmsService;
  26.     private EnvironmentService $environmentService;
  27.     public function __construct(
  28.         CustomerStatsService $customerStatsService,
  29.         LoggerInterface $logger,
  30.         SessionService $sessionService,
  31.         LinkGenerator $linkGenerator,
  32.         LandingService $landingService,
  33.         CmsService $cmsService,
  34.         EnvironmentService $environmentService
  35.     ) {
  36.         $this->customerStatsService $customerStatsService;
  37.         $this->logger $logger;
  38.         $this->sessionService $sessionService;
  39.         $this->linkGenerator $linkGenerator;
  40.         $this->landingService $landingService;
  41.         $this->cmsService $cmsService;
  42.         $this->environmentService $environmentService;
  43.     }
  44.     /**
  45.      * @Route("/service{anything}", name="legacy_service_endpoints", priority="-3",  requirements={"anything"=".*"})
  46.      * @Route("/admin{anything}", name="legacy_admin_endpoints", priority="-3", requirements={"anything"=".*"})
  47.      */
  48.     public function legacyNotFrontEndpoints(Request $request): Response
  49.     {
  50.         $uri $request->getPathInfo();
  51.         if ($this->environmentService->isApiSandbox() && !\str_contains($uri'order/payment')) {
  52.             return new JsonResponse(['code' => 'Bad request''message' => Response::HTTP_BAD_REQUEST, ], Response::HTTP_BAD_REQUEST);
  53.         }
  54.         $this->sessionService->set('cms_new'false);
  55.         $controllerData $request->request->get('legacy_controller');
  56.         if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  57.             throw $this->createNotFoundException();
  58.         }
  59.         if (LegacyRequest::MAINTENANCE_CONTROLLER === $controllerData['controller']) {
  60.             throw new ServiceUnavailableHttpException();
  61.         }
  62.         $legacyController = new $controllerData['controller']();
  63.         if (!method_exists($legacyController$controllerData['method'])) {
  64.             throw $this->createNotFoundException();
  65.         }
  66.         call_user_func_array([$legacyController$controllerData['method']], $controllerData['args']);
  67.         if (empty($legacyController->getView())) {
  68.             $this->sessionService->deleteFlashData();
  69.             return new Response('');
  70.             // throw $this->createNotFoundException();
  71.         }
  72.         $html $legacyController->display();
  73.         $this->sessionService->deleteFlashData();
  74.         return new Response($html);
  75.     }
  76.     /**
  77.      * @Route("/{anything}", name="legacy_front_endpoints", priority=-5, requirements={"anything"="^((?!rest/).)*$"})
  78.      */
  79.     public function legacyFrontEndpoints(Request $requeststring $anything): Response
  80.     {
  81.         $uri $request->getPathInfo();
  82.         if ($this->environmentService->isApiSandbox() && !\str_contains($uri'order/payment')) {
  83.             return new JsonResponse(['code' => 'Bad request''message' => Response::HTTP_BAD_REQUEST, ], Response::HTTP_BAD_REQUEST);
  84.         }
  85.         $cmsLanguage $this->cmsService->getCmsInformation($anything);
  86.         $this->sessionService->set('cms_new'false);
  87.         if ($cmsLanguage && !$cmsLanguage->getCms()->getLegacy()) {
  88.             $this->sessionService->set('id_model'$cmsLanguage->getCms()->getId());
  89.             $this->sessionService->set('cms_new'true);
  90.             return $this->render(
  91.                 $this->landingService->getLandingBaseTemplate($cmsLanguage),
  92.                 $this->landingService->getParametersForBaseCms($cmsLanguage, (bool)$request->get('update'false))
  93.             );
  94.         }
  95.         $controllerData $request->request->get('legacy_controller');
  96.         if (LegacyRequest::NOT_FOUND_CONTROLLER === $controllerData['controller']) {
  97.             throw $this->createNotFoundException();
  98.         }
  99.         if (LegacyRequest::MAINTENANCE_CONTROLLER === $controllerData['controller']) {
  100.             throw new ServiceUnavailableHttpException();
  101.         }
  102.         $legacyController null;
  103.         try {
  104.             if ($this->sessionService->get('id_model')) {
  105.                 $legacyController = new $controllerData['controller']($this->sessionService->get('id_model'));
  106.             } elseif (isset($controllerData['args'][0])) {
  107.                 $legacyController = new $controllerData['controller']($controllerData['args'][0]);
  108.             } else {
  109.                 $legacyController = new $controllerData['controller']();
  110.             }
  111.         } catch (\Throwable $t) {
  112.             $this->logger->critical('Error loading legacy controller: '.$anything.' '.$t->getMessage());
  113.         }
  114.         if (!method_exists($legacyController$controllerData['method'])) {
  115.             throw $this->createNotFoundException();
  116.         }
  117.         call_user_func_array([$legacyController$controllerData['method']], $controllerData['args']);
  118.         /** @var Environment */
  119.         $twig $this->container->get('twig');
  120.         $headData $legacyController->head();
  121.         if (null === $headData || $legacyController->getDisplay_only_view()) {
  122.             $html $legacyController->display();
  123.             return new Response($html);
  124.         }
  125.         $customerStats $this->customerStatsService->getCustomerStatsId();
  126.         $userId $customerStats->getId();
  127.         $currentUrl $this->getCurrentUrl($headData);
  128.         $renderedTemplateData = [
  129.             'bodyHtml' => $legacyController->display(true),
  130.             'headHtml' => $twig->render('front/base/legacy_head.html.twig', [
  131.                 'data' => $headData,
  132.                 'current_url' => $currentUrl,
  133.                 'dataLayerUserData' => [
  134.                     'userId' => $userId,
  135.                     'pageName' => $request->getSession()->get('url'),
  136.                 ],
  137.             ]),
  138.             'shopMenuHtml' => $legacyController->isShopMenu() ? $legacyController->mainNav() : null,
  139.             'footerJs' => $legacyController->prepareJs(true),
  140.         ];
  141.         $renderedTemplate $this->renderview(
  142.             'front/base/base_'.($legacyController->isShopMenu() ? 'shop' 'corporate').'_legacy.html.twig',
  143.             $renderedTemplateData
  144.         );
  145.         $this->sessionService->deleteFlashData();
  146.         return new Response($renderedTemplate);
  147.     }
  148.     protected function getCurrentUrl(\Partial $headData): string
  149.     {
  150.         $currentUrl '';
  151.         if (!empty($headData->current_url_fb)) {
  152.             return $headData->current_url_fb;
  153.         }
  154.         if (!empty($headData->current_url)) {
  155.             return $this->linkGenerator->getCurrentLink(
  156.                 $headData->current_url,
  157.                 $this->sessionService->get('id_lang')
  158.             );
  159.         }
  160.         return $currentUrl;
  161.     }
  162. }