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

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 ($this->machineLocaleHandler->getCountryByIpCookie()
  14.             && $this->machineLocaleHandler->getLangByIpCookie()
  15.             && $this->machineLocaleHandler->getLangToUrl() === $this->machineLocaleHandler->getLangByIpCookie()
  16.             && $this->machineLocaleHandler->getCountryToUrl() === $this->machineLocaleHandler->getCountryByIpCookie()
  17.         ) {
  18.             // поправим все куки
  19.             $this->machineLocaleHandler->setCountryCookie($this->machineLocaleHandler->getCountryByIpCookie());
  20.             $this->machineLocaleHandler->setLangCookie($this->machineLocaleHandler->getLangByIpCookie());
  21.             $this->machineLocaleHandler->setUserApprovedCookie(true);
  22.             $this->machineLocaleHandler->setShowPopup(false);
  23.             $this->machineLocaleHandler->getRequest()->getSession()->remove('showLocale');
  24.             $this->machineLocaleHandler->getRequest()->getSession()->remove('showLocaleReact');
  25.             $this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLocaleByMainCookie());
  26.             $this->machineLocaleHandler->updateCookieSet();
  27.             return null;
  28.         }
  29.         $this->machineLocaleHandler->setShowPopup(true);
  30.         $showLocale = [
  31.             'lang'     => $this->machineLocaleHandler->getLangByIpCookie(),
  32.             'country'  => $this->machineLocaleHandler->getCountryByIpCookie(),
  33.             'currency' => LocaleHelper::getCurrencyByCountry($this->machineLocaleHandler->getCountryByIpCookie()),
  34.             'measure' => LocaleHelper::getUserMeasure(['country' => $this->machineLocaleHandler->getCountryByIpCookie()]),
  35.         ];
  36.         $this->machineLocaleHandler->getRequest()->getSession()->set('showLocale'$showLocale);
  37.         $this->machineLocaleHandler->getRequest()->getSession()->set('showLocaleReact'$showLocale);
  38.         $this->machineLocaleHandler->getRequest()->setLocale($this->machineLocaleHandler->getLangByIpCookie() . '-' $this->machineLocaleHandler->getCountryByIpCookie());
  39.         // пока нет выбора - приоритет ставим у локали в урле
  40.         $localeFromUri LocaleHelper::getLocaleFromUri($this->machineLocaleHandler->getRequest()->getRequestUri());
  41.         if (null !== $localeFromUri->getLocaleOrNullIfUndefined()) {
  42.             $this->machineLocaleHandler->getRequest()->setLocale($localeFromUri->getLocaleOrNullIfUndefined());
  43.         }
  44.         $this->nextStep(CheckExistsCountryByIpAndLangByIpCookieRuleHandler::getRule($this->machineLocaleHandler));
  45.         return parent::execute();
  46.     }
  47. }