src/EventListener/LegacyBootstrapListener.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Application\Service\Api\Service\FileService;
  4. use App\Application\Service\Session\SessionService;
  5. use App\Controller\Front\Account\AccountCreationController;
  6. use App\Entity\System\Catalog;
  7. use App\Entity\System\Customer;
  8. use App\Entity\System\Employee;
  9. use App\Service\EnvironmentService;
  10. use Request as LegacyRequest;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\HttpKernel\Event\RequestEvent;
  16. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. class LegacyBootstrapListener
  21. {
  22.     public const CONTOLLERS_CHECKOUT = ['cartController''subscriptionController'];
  23.     public const METHOD_CHECKOUT = ['checkout''mobilecart'];
  24.     public const LINKS_CHECKOUTS = ['/cart/checkout''subscription/checkout''cart/mobileCart'];
  25.     private RequestStack $requestStack;
  26.     private UrlGeneratorInterface $urlGenerator;
  27.     private Security $security;
  28.     private RouterInterface $router;
  29.     private EnvironmentService $environmentService;
  30.     private string $legacyDir;
  31.     private SessionService $sessionService;
  32.     public function __construct(
  33.         RequestStack $requestStack,
  34.         UrlGeneratorInterface $urlGenerator,
  35.         Security $security,
  36.         RouterInterface $router,
  37.         EnvironmentService $environmentService,
  38.         FileService $fileService,
  39.         SessionService $sessionService
  40.     ) {
  41.         $this->requestStack $requestStack;
  42.         $this->urlGenerator $urlGenerator;
  43.         $this->security $security;
  44.         $this->router $router;
  45.         $this->legacyDir $fileService->getLegacyDir();
  46.         $this->environmentService $environmentService;
  47.         $this->sessionService $sessionService;
  48.     }
  49.     public function onKernelRequest(RequestEvent $event): void
  50.     {
  51.         if ($this->environmentService->isApi()) {
  52.             return;
  53.         }
  54.         $symfonyRequest $event->getRequest();
  55.         $refTapfiliate $symfonyRequest->query->get('ref');
  56.         $this->requestStack->getSession()->set('isCheckout'false);
  57.         try {
  58.             $pathInfo $this->router->match($event->getRequest()->getPathInfo());
  59.         } catch (ResourceNotFoundException $exception) {
  60.             return;
  61.         }
  62.         $this->defineLayoutsPath($event->getRequest()->getPathInfo());
  63.         if (
  64.             \array_key_exists('_route'$pathInfo)
  65.             && ($pathInfo['_route'] === 'legacy_front_endpoints'
  66.             || $pathInfo['_route'] === 'legacy_service_endpoints'
  67.             || $pathInfo['_route'] === 'legacy_admin_endpoints')
  68.         ) {
  69.             $legacyRequest = new LegacyRequest(); // TODO move logic inside LegacyRequest here
  70.             $controller $legacyRequest->getControlador().LegacyRequest::DEFAULT_NAME_CONTROLLER;
  71.             $method $legacyRequest->getMetodo();
  72.             $args $legacyRequest->getArgumentos();
  73.             $this->sessionService->set('controller'$controller);
  74.             $this->sessionService->set('metodo'$method);
  75.             $this->sessionService->set('argumentos'$args);
  76.             $this->sessionService->set('url'$legacyRequest->getUrl());
  77.             if (!$this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY) || empty($this->sessionService->get(SessionService::ALLOWED_CATALOGS_KEY))) {
  78.                 $this->sessionService->set(SessionService::ALLOWED_CATALOGS_KEY, [Catalog::DEFAULT_CATALOG_BIGBUY_REFERENCE]);
  79.             }
  80.             $resolvedController $this->resolveLegacyController($legacyRequest$controller$method$args);
  81.             $this->saveGoogleAnalyticsCookie($legacyRequest$resolvedController['class']);
  82.             $symfonyRequestData $symfonyRequest->request->get('legacy_controller');
  83.             $symfonyRequest->request->set('legacy_controller', [
  84.                 'url' => $legacyRequest->getUrl(),
  85.                 'controller' => $resolvedController['class'],
  86.                 'method' => $resolvedController['method'],
  87.                 'args' => $resolvedController['args'],
  88.             ]);
  89.             if (
  90.                 $symfonyRequestData !== null
  91.                 && !\array_key_exists('ref'$symfonyRequestData)
  92.                 && empty($symfonyRequestData['ref'])
  93.                 && $refTapfiliate !== null
  94.             ) {
  95.                 $symfonyRequest->request->set('legacy_controller', [
  96.                     'url' => $legacyRequest->getUrl(),
  97.                     'controller' => $resolvedController['class'],
  98.                     'method' => $resolvedController['method'],
  99.                     'args' => $resolvedController['args'],
  100.                     'ref' => $refTapfiliate,
  101.                 ]);
  102.             }
  103.             $this->populateSfSessionFromSessionGlobal($symfonyRequest->getSession());
  104.             if (in_array($controllerself::CONTOLLERS_CHECKOUT) && in_array($methodself::METHOD_CHECKOUT)) {
  105.                 $this->requestStack->getSession()->set('isCheckout'true);
  106.             }
  107.         }
  108.         $this->checkCustomerPartialAccountAndRedirect($event);
  109.     }
  110.     /**
  111.      * @param string $path
  112.      * @param string $controller
  113.      *
  114.      * @return string
  115.      */
  116.     private function getLegacyControllerPath(string $pathstring $controller): string
  117.     {
  118.         return $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$controller.'.php';
  119.     }
  120.     /**
  121.      * @param LegacyRequest $legacyRequest
  122.      * @param string $controller
  123.      * @param string $method
  124.      * @param array $args
  125.      *
  126.      * @return array
  127.      */
  128.     private function resolveLegacyController(LegacyRequest $legacyRequeststring $controllerstring $method, array $args): array
  129.     {
  130.         if ($legacyRequest->getAdmin()) {
  131.             $path ADMIN_DIR;
  132.         } elseif ($legacyRequest->getService()) {
  133.             $path SERVICE_DIR;
  134.         } else {
  135.             $path FRONT_DIR;
  136.         }
  137.         $rutaControlador $this->getLegacyControllerPath($path$controller);
  138.         $maintenanceips explode(';', \Configuration::get('MAINTENANCE_IPS'true));
  139.         $realIP '';
  140.         if (isset($_SERVER['HTTP_X_REAL_IP'])) {
  141.             $realIP $_SERVER['HTTP_X_REAL_IP'];
  142.         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  143.             $realIP $_SERVER['REMOTE_ADDR'];
  144.         }
  145.         if (!empty($args) && $legacyRequest->getService()) {
  146.             $args $this->sessionService->get('argumentos');
  147.         }
  148.         if (\Configuration::get('MAINTENANCE') !== '0' && !in_array($realIP$maintenanceips)) {
  149.             return [
  150.                 'class' => LegacyRequest::MAINTENANCE_CONTROLLER,
  151.                 'method' => LegacyRequest::DEFAULT_METHOD,
  152.                 'args' => $args,
  153.             ];
  154.             //            throw new ServiceUnavailableHttpException();
  155.         }
  156.         if (!is_callable([$controller$method]) || !is_readable($rutaControlador)) {
  157.             return [
  158.                 'class' => LegacyRequest::NOT_FOUND_CONTROLLER,
  159.                 'method' => LegacyRequest::DEFAULT_METHOD,
  160.                 'args' => $args,
  161.             ];
  162.             //            throw new NotFoundHttpException('Not Found');
  163.         }
  164.         // Require requested controller and all the rest from the same folder (service, admin, front..) because in certain cases a controller calls another...
  165.         // Sort descending because of a single case (admin/sliderController must be loaded before admin/bannerController). So dirty, I know.
  166.         foreach (scandir($this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$pathSCANDIR_SORT_DESCENDING) as $filename) {
  167.             $controllerPath $this->legacyDir.'/controllers'.DIRECTORY_SEPARATOR.$path.'/'.$filename;
  168.             if (is_file($controllerPath) && strpos($filename'Controller')) {
  169.                 require_once $controllerPath;
  170.             }
  171.         }
  172.         return [
  173.             'class' => $controller,
  174.             'method' => $method,
  175.             'args' => $args,
  176.         ];
  177.     }
  178.     private function saveGoogleAnalyticsCookie(LegacyRequest $legacyRequeststring $class): void
  179.     {
  180.         if (
  181.             LegacyRequest::NOT_FOUND_CONTROLLER !== $class && LegacyRequest::MAINTENANCE_CONTROLLER !== $class
  182.             && $legacyRequest->getFront() && !\Tools::getValue('ajax')
  183.         ) {
  184.             \GACookie::SaveCookie();
  185.         }
  186.     }
  187.     private function populateSfSessionFromSessionGlobal(SessionInterface $session): void
  188.     {
  189.         foreach ($_SESSION as $key => $value) {
  190.             if ($key !== '_sf2_attributes') {
  191.                 $session->set($key$value);
  192.             }
  193.         }
  194.     }
  195.     /**
  196.      * @param RequestEvent $requestEvent
  197.      *
  198.      * @return void
  199.      */
  200.     private function checkCustomerPartialAccountAndRedirect(RequestEvent $requestEvent): void
  201.     {
  202.         if (Request::METHOD_OPTIONS === $requestEvent->getRequest()->getMethod()) {
  203.             return;
  204.         }
  205.         $pathInfo $requestEvent->getRequest()->getPathInfo();
  206.         /** @var Customer|null $customer */
  207.         $customer $this->security->getUser();
  208.         if ($customer instanceof Employee) {
  209.             return;
  210.         }
  211.         $this->checkLinksCheckouts($pathInfo$customer$requestEvent);
  212.     }
  213.     private function checkLinksCheckouts(string $pathInfo, ?Customer $customerRequestEvent $requestEvent): void
  214.     {
  215.         foreach (self::LINKS_CHECKOUTS as $path) {
  216.             if (str_contains($pathInfo$path)) {
  217.                 $this->checkPartialCreation($customer$requestEvent$pathInfo);
  218.             }
  219.         }
  220.     }
  221.     private function checkPartialCreation(?Customer $customerRequestEvent $requestEventstring $pathInfo): void
  222.     {
  223.         if (!$customer) {
  224.             return;
  225.         }
  226.         switch ($customer->getPartialCreation()) {
  227.             case Customer::PARTIAL_CREATION_STEP_0:
  228.                 $requestEvent->setResponse(new RedirectResponse($this->urlGenerator->generate('account_creation_step1', [], UrlGeneratorInterface::ABSOLUTE_URL)));
  229.                 $this->requestStack->getSession()->set(AccountCreationController::ACCOUNT_CREATION_REDIRECT$pathInfo);
  230.                 return;
  231.             case Customer::PARTIAL_CREATION:
  232.                 $requestEvent->setResponse(new RedirectResponse($this->urlGenerator->generate('account_creation_step2', [], UrlGeneratorInterface::ABSOLUTE_URL)));
  233.                 $this->requestStack->getSession()->set(AccountCreationController::ACCOUNT_CREATION_REDIRECT$pathInfo);
  234.                 break;
  235.         }
  236.     }
  237.     private function defineLayoutsPath(string $route): void
  238.     {
  239.         $templateDir FRONT_DIR;
  240.         if (preg_match('/^\/'.ADMIN_DIR.'\//'$route$m)) {
  241.             $templateDir ADMIN_DIR;
  242.             if (!defined('IS_ADMIN')) {
  243.                 define('IS_ADMIN''1');
  244.             }
  245.         } // SI ESTAMOS EN SERVICE
  246.         elseif (preg_match('/^\/'.SERVICE_DIR.'\//'$route$m)) {
  247.             $templateDir SERVICE_DIR;
  248.             if (!defined('IS_SERVICE')) {
  249.                 define('IS_SERVICE''1');
  250.             }
  251.         }
  252.         if (!defined('LAYOUTS_PATH')) {
  253.             /** @phpstan-ignore-next-line  */
  254.             define('LAYOUTS_PATH', (IS_MOBILE MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'layout'.DIRECTORY_SEPARATOR);
  255.         }
  256.         if (!defined('PARTIALS_PATH')) {
  257.             /** @phpstan-ignore-next-line  */
  258.             define('PARTIALS_PATH', (IS_MOBILE MOBILE_VIEWS.DIRECTORY_SEPARATOR '').$templateDir.DIRECTORY_SEPARATOR.'partial'.DIRECTORY_SEPARATOR);
  259.         }
  260.     }
  261. }