src/EventListener/LocaleListener.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Gos\PortalSettings;
  4. use App\Utils\Uniqskills\DomainListener;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  13. class LocaleListener implements EventSubscriberInterface
  14. {
  15.     private UrlMatcherInterface $urlMatcher;
  16.     private EntityManagerInterface $em;
  17.     private DomainListener $domainListener;
  18.     private $newUrl;
  19.     private $supportedLocales;
  20.     private $defaultLocale;
  21.     private $uniqskillsUrl;
  22.     private $current;
  23.     public function __construct(
  24.         EntityManagerInterface $_em,
  25.         UrlMatcherInterface $urlMatcher,
  26.         DomainListener $domainListener
  27.     ) {
  28.         $this->em               $_em;
  29.         $this->urlMatcher       $urlMatcher;
  30.         $this->defaultLocale    $_ENV['DEFAULT_LOCALE'];
  31.         $this->supportedLocales explode("|"substr($_ENV['US_SUPPORTED_LANGUAGES'], 1, -1));
  32.         $this->uniqskillsUrl    $_ENV['US_URL'];
  33.         $this->domainListener   $domainListener;
  34.     }
  35.     public function onKernelRequest(RequestEvent $event): void
  36.     {
  37.         $request        $event->getRequest();
  38.         $this->current  $request->getUri();
  39.         $this->newUrl   $request->getPathInfo();
  40.         $fmDev          $this->checkForFmDev();
  41.         if (strpos($this->current'fm-admin'))
  42.         {
  43.             $this->setLocaleForAdminPanel($request);
  44.             return;
  45.         }
  46.         if (strpos($this->newUrl'_wdt') !== false) return;
  47.         if (strpos($this->newUrl'_profiler') !== false) return;
  48.         if (strpos($this->newUrl'_fragment') !== false) return;
  49.         $locale $request->query->get('userLocale')
  50.             ? $request->query->get('userLocale')
  51.             : $request->request->get('userLocale');
  52.         if ($locale) {
  53.             $request->getSession()->set('userLocale'$locale);
  54.             $request->setLocale($locale);
  55.         }
  56.         if (strpos($this->current$this->uniqskillsUrl) === false) return;
  57.         $locale $this->checkLocale();
  58.         $this->domainListener->checkCountryCode($request);
  59.         $request->getSession()->set('portalSettingsHash'$_ENV['US_PORTAL_SETTINGS']);
  60.         $domain $this->em->getRepository(PortalSettings::class)->findOneBy(['hash'=>$_ENV['US_PORTAL_SETTINGS']]);
  61.         if ($domain)
  62.         {
  63.             // task UUK-18 - if US UK portal set GB geolocalization
  64.             if ($domain->getHash() === $_ENV['US_PORTAL_SETTINGS'] && $domain->getHash() !== $_ENV['PL_US_PORTAL_SETTINGS'])
  65.             {
  66.                 $request->getSession()->set('myCountryCode''GB');
  67.             }
  68.             $request->getSession()->set('domain'$domain->getId());
  69.         }
  70.         else
  71.         {
  72.             $request->getSession()->set('domain''113'); //left it for now, just in case
  73.         }
  74.         if ($locale != false)
  75.         {
  76.             $request->getSession()->set('userLocale'$locale);
  77.             $request->setLocale($locale);
  78.         }
  79.         else
  80.         {
  81.             $locale $domain->getFmCompanySettings()->getLanguage()
  82.                 ? $domain->getFmCompanySettings()->getLanguage()->first()->getCode()
  83.                 : null;
  84.             if ($locale)
  85.             {
  86.                 $request->getSession()->set('userLocale'$locale);
  87.                 $request->setLocale($locale);
  88.             }
  89.         }
  90.         $pathLocale Request::create($this->current)->getPathInfo();
  91.         try
  92.         {
  93.             $this->urlMatcher->match($pathLocale);
  94.             return;
  95.         }
  96.         catch (\Throwable $e)
  97.         {
  98.             if (!$request->isXmlHttpRequest() && !strpos($this->current'cart/add'))
  99.                 throw new NotFoundHttpException("uniqskills");
  100.         }
  101.         $locale $request->getSession()->get('userLocale');
  102.         if (is_null($locale) || !$this->isSupportedLocale($locale))
  103.         {
  104.             $locale $this->defaultLocale;
  105.         }
  106.         $request->setLocale($locale);
  107.         $request->getSession()->set('userLocale'$locale);
  108.         $pathLocale $this->createNewPath($locale$this->current);
  109.         try
  110.         {
  111.             $this->urlMatcher->match($pathLocale);
  112.             if ($fmDev !== false$pathLocale $fmDev $pathLocale;
  113.             $event->setResponse(new RedirectResponse($pathLocale));
  114.         }
  115.         catch (\Throwable $e)
  116.         {
  117.             throw new NotFoundHttpException("uniqskills");
  118.         }
  119.     }
  120.     private function setLocaleForAdminPanel(Request $request): void
  121.     {
  122.         if ($request->query->get('_locale'))
  123.         {
  124.             $request->setLocale($request->query->get('_locale'));
  125.             $request->getSession()->set('_locale'$request->query->get('_locale'));
  126.         }
  127.         else
  128.         {
  129.             $request->setLocale($request->getSession()->get('_locale'$this->defaultLocale));
  130.         }
  131.     }
  132.     private function checkForFmDev()
  133.     {
  134.         if (strpos($this->current'afm_dev.php'))
  135.         {
  136.             $this->current str_replace('/afm_dev.php'''$this->current);
  137.             return '/afm_dev.php';
  138.         }
  139.         elseif (strpos($this->current'fm_dev.php'))
  140.         {
  141.             $this->current str_replace('/fm_dev.php'''$this->current);
  142.             return '/fm_dev.php';
  143.         }
  144.         return false;
  145.     }
  146.     private function createNewPath($locale$url): string
  147.     {
  148.         $newPath Request::create(str_replace($this->uniqskillsUrl$this->uniqskillsUrl '/' $locale$url));
  149.         return $newPath->getPathInfo();
  150.     }
  151.     private function checkLocale()
  152.     {
  153.         $urlParts explode('/'$this->newUrl);
  154.         if (isset($urlParts[1]) && $this->isSupportedLocale($urlParts[1]))
  155.         {
  156.             return $urlParts[1];
  157.         }
  158.         return false;
  159.     }
  160.     private function isSupportedLocale($locale): bool
  161.     {
  162.         return in_array($locale$this->supportedLocales);
  163.     }
  164.     public static function getSubscribedEvents(): array
  165.     {
  166.         return array(
  167.             KernelEvents::REQUEST => [['onKernelRequest'20]]
  168.         );
  169.     }
  170. }