custom/plugins/AcrisShopSwitchCS/src/Components/ShopSwitch/ShopSwitchRoute.php line 187

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Components\ShopSwitch;
  3. use Acris\ShopSwitch\Components\Exception\IpNotFoundException;
  4. use Acris\ShopSwitch\Components\Exclusion\ExclusionService;
  5. use Acris\ShopSwitch\Components\GeoTargeting\BotService;
  6. use Acris\ShopSwitch\Components\ContextLanguageService;
  7. use Acris\ShopSwitch\Components\GeoTargeting\Ip2CountryService;
  8. use Acris\ShopSwitch\Components\GeoTargeting\IpService;
  9. use Acris\ShopSwitch\Components\ShopSwitchHelperService;
  10. use Acris\ShopSwitch\Components\ShopSwitchRedirectUrlService;
  11. use Acris\ShopSwitch\Components\ShopSwitchRuleGateway;
  12. use Acris\ShopSwitch\Components\ShopSwitchRuleService;
  13. use Acris\ShopSwitch\Components\Struct\PossibleCountryCollection;
  14. use Acris\ShopSwitch\Components\Struct\PossibleLanguageCollection;
  15. use Acris\ShopSwitch\Custom\ShopSwitchRuleEntity;
  16. use Acris\ShopSwitch\Components\Exception\ActiveDomainNotFoundException;
  17. use Shopware\Core\Framework\Context;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  20. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  21. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  22. use Shopware\Core\Framework\Routing\Annotation\Since;
  23. use Shopware\Core\System\Country\CountryEntity;
  24. use Shopware\Core\System\SystemConfig\SystemConfigService;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\Routing\Annotation\Route;
  27. use Acris\ShopSwitch\Components\PathInfoChecker;
  28. /**
  29.  * @RouteScope(scopes={"store-api"})
  30.  */
  31. class ShopSwitchRoute extends AbstractShopSwitchRoute
  32. {
  33.     public const INTEGRATION_TYPE_DIRECT 'direct';
  34.     public const INTEGRATION_TYPE_AJAX 'ajax';
  35.     /**
  36.      * @var ShopSwitchRuleGateway
  37.      */
  38.     private $shopSwitchRuleGateway;
  39.     /**
  40.      * @var ShopSwitchRuleService
  41.      */
  42.     private $shopSwitchRuleService;
  43.     /**
  44.      * @var IpService
  45.      */
  46.     private $ipService;
  47.     /**
  48.      * @var ShopSwitchRedirectUrlService
  49.      */
  50.     private $redirectUrlService;
  51.     /**
  52.      * @var BotService
  53.      */
  54.     private BotService $botService;
  55.     /**
  56.      * @var ContextLanguageService
  57.      */
  58.     private ContextLanguageService $contextLanguageService;
  59.     /**
  60.      * @var SystemConfigService
  61.      */
  62.     private SystemConfigService $configService;
  63.     private ExclusionService $exclusionService;
  64.     private ShopSwitchHelperService $helperService;
  65.     private Ip2CountryService $ip2CountryService;
  66.     private PathInfoChecker $infoChecker;
  67.     private EntityRepositoryInterface $countryRepository;
  68.     public function __construct(
  69.         ShopSwitchRuleGateway $shopSwitchRuleGateway,
  70.         ShopSwitchRuleService $shopSwitchRuleService,
  71.         IpService $ipService,
  72.         ShopSwitchRedirectUrlService $redirectUrlService,
  73.         BotService $botService,
  74.         ContextLanguageService $contextLanguageService,
  75.         SystemConfigService $configService,
  76.         ExclusionService $exclusionService,
  77.         ShopSwitchHelperService $helperService,
  78.         Ip2CountryService $ip2CountryService,
  79.         PathInfoChecker $infoChecker,
  80.         EntityRepositoryInterface $countryRepository
  81.     ) {
  82.         $this->shopSwitchRuleGateway $shopSwitchRuleGateway;
  83.         $this->shopSwitchRuleService $shopSwitchRuleService;
  84.         $this->ipService $ipService;
  85.         $this->redirectUrlService $redirectUrlService;
  86.         $this->botService $botService;
  87.         $this->contextLanguageService $contextLanguageService;
  88.         $this->configService $configService;
  89.         $this->exclusionService $exclusionService;
  90.         $this->helperService $helperService;
  91.         $this->ip2CountryService $ip2CountryService;
  92.         $this->infoChecker $infoChecker;
  93.         $this->countryRepository $countryRepository;
  94.     }
  95.     public function getDecorated(): AbstractShopSwitchRoute
  96.     {
  97.         throw new DecorationPatternException(self::class);
  98.     }
  99.     /**
  100.      * @Since("6.3.4.0")
  101.      * @Route("/store-api/acris-shop-switch-rules/{activeDomainId}", name="store-api.acris-shop-switch.rules", methods={"GET", "POST"})
  102.      */
  103.     public function load(string $activeDomainIdRequest $requestContext $context, ?string $languageId, ?string $fixedLanguageId null, ?string $fixedCountryIso null): ShopSwitchRouteResponse
  104.     {
  105.         $activeDomainEntity $this->helperService->getDomainById($activeDomainId$context);
  106.         if(empty($languageId)) {
  107.             $languageId $activeDomainEntity->getLanguageId();
  108.         }
  109.         $activeSalesChannel $activeDomainEntity->getSalesChannel();
  110.         $salesChannelId $activeSalesChannel->getId();
  111.         if(empty($fixedLanguageId) === true) {
  112.             $browserLanguageIso $this->getBrowserLanguageIso($request);
  113.             $context $this->contextLanguageService->getContextByLanguageId($context$languageId);
  114.             $languageIds $this->getLanguageIdsByLanguageIso($browserLanguageIso$context$salesChannelId);
  115.             $languageId reset($languageIds);
  116.             if($this->configService->get('AcrisShopSwitchCS.config.modalWindowLanguage'$salesChannelId) === 'browserLanguage' && !empty($languageId)) {
  117.                 $context $this->switchContextByBrowserLanguage($languageId$context);
  118.             }
  119.         } else {
  120.             $languageIds = [$fixedLanguageId];
  121.         }
  122.         $activeRule $this->shopSwitchRuleGateway->getActiveRuleByDomain($activeDomainId$context);
  123.         if($activeRule instanceof ShopSwitchRuleEntity) {
  124.             $domainCollections $activeRule->getDomains();
  125.             if($domainCollections) {
  126.                 $this->infoChecker->collectPathInfoExistencesForDomains($request,$domainCollections);
  127.             }
  128.         }
  129.         $activeDomainUrl $this->redirectUrlService->getUrlForActiveDomain($request$activeDomainEntity);
  130.         if(!$activeRule instanceof ShopSwitchRuleEntity) {
  131.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  132.         }
  133.         $activeRuleDomain $this->shopSwitchRuleService->getActiveRuleDomain($activeDomainId$activeRule);
  134.         if(empty($activeRuleDomain)) {
  135.             throw new ActiveDomainNotFoundException();
  136.         }
  137.         if($this->botService->isBot($request) === true) {
  138.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  139.         }
  140.         if($this->exclusionService->isExcluded($this->ipService->getClientIp($request$salesChannelId), $request$context) === true) {
  141.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  142.         }
  143.         // always try to get the country from the ip lookup (also when just language selection is used to set the shipping country - could be change via new config value if wanted)
  144.         if(empty($fixedCountryIso) === true) {
  145.             $ip $this->ipService->getClientIp($request$salesChannelId);
  146.             if(empty($ip)) {
  147.                 throw new IpNotFoundException();
  148.             }
  149.             $fixedCountryIso $this->ip2CountryService->ip2country($ip$salesChannelId);
  150.             if(empty($fixedCountryIso) === true) {
  151.                 $defaultCountryId $this->configService->get('AcrisShopSwitchCS.config.fallbackCountry'$salesChannelId);
  152.                 if(!empty($defaultCountryId)) {
  153.                     $defaultCountry $this->countryRepository->search(new Criteria([$defaultCountryId]), $context)->first();
  154.                     if($defaultCountry instanceof CountryEntity) {
  155.                         $fixedCountryIso $defaultCountry->getIso();
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         return $this->helperService->getValidResponse($activeDomainId$activeRule$activeDomainUrl$request$activeSalesChannel$context$activeRuleDomain$languageIds$fixedCountryIso);
  161.     }
  162.     private function getBrowserLanguageIso(Request $request): ?string
  163.     {
  164.         $browserAcceptLanguage $request->server->get('HTTP_ACCEPT_LANGUAGE');
  165.         if(empty($browserAcceptLanguage) === true) {
  166.             return $browserAcceptLanguage;
  167.         }
  168.         if(strpos($browserAcceptLanguage,",") == 2){
  169.             return strtolower(substr($browserAcceptLanguage02));
  170.         }else {
  171.             return strtolower(substr($browserAcceptLanguage05));
  172.         }
  173.     }
  174.     private function getLanguageIdsByLanguageIso(?string $languageIsoContext $contextstring $salesChannelId): array
  175.     {
  176.         if(empty($languageIso) === true) {
  177.             return [];
  178.         }
  179.         $languageIds $this->contextLanguageService->getLanguageIdByIso($languageIso$context);
  180.         if(empty($languageIds) === true) {
  181.             $languageIdFromConfig $this->configService->get('AcrisShopSwitchCS.config.fallbackModalWindowLanguage'$salesChannelId);
  182.             if(!empty($languageIdFromConfig)) {
  183.                 $languageIds = [$languageIdFromConfig];
  184.             }
  185.         }
  186.         return $languageIds;
  187.     }
  188.     private function switchContextByBrowserLanguage(?string $languageIdContext $context): Context
  189.     {
  190.         if(empty($languageId) === true) {
  191.             return $context;
  192.         }
  193.         return $this->contextLanguageService->getContextByLanguageId($context$languageId);
  194.     }
  195. }