src/WebBundle/Handler/Locale/Rule/CheckUrlLocaleEqualsCountryByIpAndLangByIpCookieRuleHandler.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WebBundle\Handler\Locale\Rule;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use WebBundle\Helper\LocaleHelper;
  6. /**
  7.  * Проверяет: равенство локали в урле и кук, определённых по ip
  8.  */
  9. class CheckUrlLocaleEqualsCountryByIpAndLangByIpCookieRuleHandler extends AbstractRuleHandler
  10. {
  11.     public function execute(): ?Response
  12.     {
  13.         if (
  14.             $this->machineLocaleHandler->getCountryByIpCookie()
  15.             && $this->machineLocaleHandler->getLangByIpCookie()
  16.             && $this->machineLocaleHandler->getLangToUrl() === $this->machineLocaleHandler->getLangByIpCookie()
  17.             && $this->machineLocaleHandler->getCountryToUrl() === $this->machineLocaleHandler->getCountryByIpCookie()
  18.         ) {
  19.             // поправим все куки
  20.             $this->machineLocaleHandler->setCountryCookie($this->machineLocaleHandler->getCountryByIpCookie());
  21.             $this->machineLocaleHandler->setLangCookie($this->machineLocaleHandler->getLangByIpCookie());
  22.             $this->machineLocaleHandler->setUserApprovedCookie(true);
  23.             $this->machineLocaleHandler->setShowPopup(false);
  24.             $this->machineLocaleHandler->getRequest()->getSession()->remove('showLocale');
  25.             $this->machineLocaleHandler->getRequest()->getSession()->remove('showLocaleReact');
  26.             $this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLocaleByMainCookie());
  27.             $this->machineLocaleHandler->updateCookieSet();
  28.             return null;
  29.         }
  30.         $this->machineLocaleHandler->setShowPopup(true);
  31.         $showLocale = [
  32.             'lang'     => $this->machineLocaleHandler->getLangByIpCookie(),
  33.             'country'  => $this->machineLocaleHandler->getCountryByIpCookie(),
  34.             'currency' => LocaleHelper::getCurrencyByCountry($this->machineLocaleHandler->getCountryByIpCookie()),
  35.             'measure' => LocaleHelper::getUserMeasure(['country' => $this->machineLocaleHandler->getCountryByIpCookie()]),
  36.         ];
  37.         // если ip у меня чили, или вообще нет, то берем по урл или по умолчанию так как весь сайн на русском, а предлагает язык en
  38.         if ($this->machineLocaleHandler->getLangByIpCookie() != LocaleHelper::getCurCountry()) {
  39.             $showLocale = [
  40.                 'lang'     => LocaleHelper::getCurLocale(),
  41.                 'country'  => LocaleHelper::getCurCountry(),
  42.                 'currency' => LocaleHelper::getCur(),
  43.                 'measure' => LocaleHelper::getUserMeasure(['country' => LocaleHelper::getCurCountry()]),
  44.             ];
  45.         }
  46.             $this->machineLocaleHandler->getRequest()->getSession()->set('showLocale'$showLocale);
  47.            //Закомментировал потому-что вызывает повторную прогонку и меняет локаль проверить не берет ли редактирование профиля showLocaleReact''
  48.            // $this->machineLocaleHandler->getRequest()->getSession()->set('showLocaleReact', $showLocale);
  49.         $this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLangByIpCookie() . '-' $this->machineLocaleHandler->getCountryByIpCookie());
  50.         // пока нет выбора - приоритет ставим у локали в урле
  51.         $localeFromUri LocaleHelper::getLocaleFromUri($this->machineLocaleHandler->getRequest()->getRequestUri());
  52.         if (null !== $localeFromUri->getLocaleOrNullIfUndefined()) {
  53.             $this->machineLocaleHandler->getRequest()->setLocale($localeFromUri->getLocaleOrNullIfUndefined());
  54.         }
  55.         $this->nextStep(CheckExistsCountryByIpAndLangByIpCookieRuleHandler::getRule($this->machineLocaleHandler));
  56.         return parent::execute();
  57.     }
  58. }