<?php
namespace App\EventListener;
use App\Entity\Gos\PortalSettings;
use App\Utils\Uniqskills\DomainListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
class LocaleListener implements EventSubscriberInterface
{
private UrlMatcherInterface $urlMatcher;
private EntityManagerInterface $em;
private DomainListener $domainListener;
private $newUrl;
private $supportedLocales;
private $defaultLocale;
private $uniqskillsUrl;
private $current;
public function __construct(
EntityManagerInterface $_em,
UrlMatcherInterface $urlMatcher,
DomainListener $domainListener
) {
$this->em = $_em;
$this->urlMatcher = $urlMatcher;
$this->defaultLocale = $_ENV['DEFAULT_LOCALE'];
$this->supportedLocales = explode("|", substr($_ENV['US_SUPPORTED_LANGUAGES'], 1, -1));
$this->uniqskillsUrl = $_ENV['US_URL'];
$this->domainListener = $domainListener;
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
$this->current = $request->getUri();
$this->newUrl = $request->getPathInfo();
$fmDev = $this->checkForFmDev();
if (strpos($this->current, 'fm-admin'))
{
$this->setLocaleForAdminPanel($request);
return;
}
if (strpos($this->newUrl, '_wdt') !== false) return;
if (strpos($this->newUrl, '_profiler') !== false) return;
if (strpos($this->newUrl, '_fragment') !== false) return;
$locale = $request->query->get('userLocale')
? $request->query->get('userLocale')
: $request->request->get('userLocale');
if ($locale) {
$request->getSession()->set('userLocale', $locale);
$request->setLocale($locale);
}
if (strpos($this->current, $this->uniqskillsUrl) === false) return;
$locale = $this->checkLocale();
$this->domainListener->checkCountryCode($request);
$request->getSession()->set('portalSettingsHash', $_ENV['US_PORTAL_SETTINGS']);
$domain = $this->em->getRepository(PortalSettings::class)->findOneBy(['hash'=>$_ENV['US_PORTAL_SETTINGS']]);
if ($domain)
{
// task UUK-18 - if US UK portal set GB geolocalization
if ($domain->getHash() === $_ENV['US_PORTAL_SETTINGS'] && $domain->getHash() !== $_ENV['PL_US_PORTAL_SETTINGS'])
{
$request->getSession()->set('myCountryCode', 'GB');
}
$request->getSession()->set('domain', $domain->getId());
}
else
{
$request->getSession()->set('domain', '113'); //left it for now, just in case
}
if ($locale != false)
{
$request->getSession()->set('userLocale', $locale);
$request->setLocale($locale);
}
else
{
$locale = $domain->getFmCompanySettings()->getLanguage()
? $domain->getFmCompanySettings()->getLanguage()->first()->getCode()
: null;
if ($locale)
{
$request->getSession()->set('userLocale', $locale);
$request->setLocale($locale);
}
}
$pathLocale = Request::create($this->current)->getPathInfo();
try
{
$this->urlMatcher->match($pathLocale);
return;
}
catch (\Throwable $e)
{
if (!$request->isXmlHttpRequest() && !strpos($this->current, 'cart/add'))
throw new NotFoundHttpException("uniqskills");
}
$locale = $request->getSession()->get('userLocale');
if (is_null($locale) || !$this->isSupportedLocale($locale))
{
$locale = $this->defaultLocale;
}
$request->setLocale($locale);
$request->getSession()->set('userLocale', $locale);
$pathLocale = $this->createNewPath($locale, $this->current);
try
{
$this->urlMatcher->match($pathLocale);
if ($fmDev !== false) $pathLocale = $fmDev . $pathLocale;
$event->setResponse(new RedirectResponse($pathLocale));
}
catch (\Throwable $e)
{
throw new NotFoundHttpException("uniqskills");
}
}
private function setLocaleForAdminPanel(Request $request): void
{
if ($request->query->get('_locale'))
{
$request->setLocale($request->query->get('_locale'));
$request->getSession()->set('_locale', $request->query->get('_locale'));
}
else
{
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
private function checkForFmDev()
{
if (strpos($this->current, 'afm_dev.php'))
{
$this->current = str_replace('/afm_dev.php', '', $this->current);
return '/afm_dev.php';
}
elseif (strpos($this->current, 'fm_dev.php'))
{
$this->current = str_replace('/fm_dev.php', '', $this->current);
return '/fm_dev.php';
}
return false;
}
private function createNewPath($locale, $url): string
{
$newPath = Request::create(str_replace($this->uniqskillsUrl, $this->uniqskillsUrl . '/' . $locale, $url));
return $newPath->getPathInfo();
}
private function checkLocale()
{
$urlParts = explode('/', $this->newUrl);
if (isset($urlParts[1]) && $this->isSupportedLocale($urlParts[1]))
{
return $urlParts[1];
}
return false;
}
private function isSupportedLocale($locale): bool
{
return in_array($locale, $this->supportedLocales);
}
public static function getSubscribedEvents(): array
{
return array(
KernelEvents::REQUEST => [['onKernelRequest', 20]]
);
}
}