<?php
declare(strict_types=1);
namespace WebBundle\Handler\Locale\Rule;
use Symfony\Component\HttpFoundation\Response;
use WebBundle\Helper\LocaleHelper;
/**
* Проверяет: равенство локали в урле и кук, определённых по ip
*/
class CheckUrlLocaleEqualsCountryByIpAndLangByIpCookieRuleHandler extends AbstractRuleHandler
{
public function execute(): ?Response
{
if (
$this->machineLocaleHandler->getCountryByIpCookie()
&& $this->machineLocaleHandler->getLangByIpCookie()
&& $this->machineLocaleHandler->getLangToUrl() === $this->machineLocaleHandler->getLangByIpCookie()
&& $this->machineLocaleHandler->getCountryToUrl() === $this->machineLocaleHandler->getCountryByIpCookie()
) {
// поправим все куки
$this->machineLocaleHandler->setCountryCookie($this->machineLocaleHandler->getCountryByIpCookie());
$this->machineLocaleHandler->setLangCookie($this->machineLocaleHandler->getLangByIpCookie());
$this->machineLocaleHandler->setUserApprovedCookie(true);
$this->machineLocaleHandler->setShowPopup(false);
$this->machineLocaleHandler->getRequest()->getSession()->remove('showLocale');
$this->machineLocaleHandler->getRequest()->getSession()->remove('showLocaleReact');
$this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLocaleByMainCookie());
$this->machineLocaleHandler->updateCookieSet();
return null;
}
$this->machineLocaleHandler->setShowPopup(true);
$showLocale = [
'lang' => $this->machineLocaleHandler->getLangByIpCookie(),
'country' => $this->machineLocaleHandler->getCountryByIpCookie(),
'currency' => LocaleHelper::getCurrencyByCountry($this->machineLocaleHandler->getCountryByIpCookie()),
'measure' => LocaleHelper::getUserMeasure(['country' => $this->machineLocaleHandler->getCountryByIpCookie()]),
];
// если ip у меня чили, или вообще нет, то берем по урл или по умолчанию так как весь сайн на русском, а предлагает язык en
if ($this->machineLocaleHandler->getLangByIpCookie() != LocaleHelper::getCurCountry()) {
$showLocale = [
'lang' => LocaleHelper::getCurLocale(),
'country' => LocaleHelper::getCurCountry(),
'currency' => LocaleHelper::getCur(),
'measure' => LocaleHelper::getUserMeasure(['country' => LocaleHelper::getCurCountry()]),
];
}
$this->machineLocaleHandler->getRequest()->getSession()->set('showLocale', $showLocale);
//Закомментировал потому-что вызывает повторную прогонку и меняет локаль проверить не берет ли редактирование профиля showLocaleReact''
// $this->machineLocaleHandler->getRequest()->getSession()->set('showLocaleReact', $showLocale);
$this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLangByIpCookie() . '-' . $this->machineLocaleHandler->getCountryByIpCookie());
// пока нет выбора - приоритет ставим у локали в урле
$localeFromUri = LocaleHelper::getLocaleFromUri($this->machineLocaleHandler->getRequest()->getRequestUri());
if (null !== $localeFromUri->getLocaleOrNullIfUndefined()) {
$this->machineLocaleHandler->getRequest()->setLocale($localeFromUri->getLocaleOrNullIfUndefined());
}
$this->nextStep(CheckExistsCountryByIpAndLangByIpCookieRuleHandler::getRule($this->machineLocaleHandler));
return parent::execute();
}
}