<?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()]),
];
$this->machineLocaleHandler->getRequest()->getSession()->set('showLocale', $showLocale);
$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();
}
}