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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Components\ShopSwitch;
  3. use Acris\ShopSwitch\Components\Exclusion\ExclusionService;
  4. use Acris\ShopSwitch\Components\GeoTargeting\BotService;
  5. use Acris\ShopSwitch\Components\ContextLanguageService;
  6. use Acris\ShopSwitch\Components\GeoTargeting\IpService;
  7. use Acris\ShopSwitch\Components\ShopSwitchHelperService;
  8. use Acris\ShopSwitch\Components\ShopSwitchRedirectUrlService;
  9. use Acris\ShopSwitch\Components\ShopSwitchRuleGateway;
  10. use Acris\ShopSwitch\Components\ShopSwitchRuleService;
  11. use Acris\ShopSwitch\Components\Struct\PossibleCountryCollection;
  12. use Acris\ShopSwitch\Components\Struct\PossibleLanguageCollection;
  13. use Acris\ShopSwitch\Custom\ShopSwitchRuleEntity;
  14. use Acris\ShopSwitch\Components\Exception\ActiveDomainNotFoundException;
  15. use OpenApi\Annotations as OA;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  18. use Shopware\Core\Framework\Routing\Annotation\Entity;
  19. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  20. use Shopware\Core\Framework\Routing\Annotation\Since;
  21. use Shopware\Core\System\SystemConfig\SystemConfigService;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. /**
  25.  * @RouteScope(scopes={"store-api"})
  26.  */
  27. class ShopSwitchRoute extends AbstractShopSwitchRoute
  28. {
  29.     public const INTEGRATION_TYPE_DIRECT 'direct';
  30.     public const INTEGRATION_TYPE_AJAX 'ajax';
  31.     /**
  32.      * @var ShopSwitchRuleGateway
  33.      */
  34.     private $shopSwitchRuleGateway;
  35.     /**
  36.      * @var ShopSwitchRuleService
  37.      */
  38.     private $shopSwitchRuleService;
  39.     /**
  40.      * @var IpService
  41.      */
  42.     private $ipService;
  43.     /**
  44.      * @var ShopSwitchRedirectUrlService
  45.      */
  46.     private $redirectUrlService;
  47.     /**
  48.      * @var BotService
  49.      */
  50.     private BotService $botService;
  51.     /**
  52.      * @var ContextLanguageService
  53.      */
  54.     private ContextLanguageService $contextLanguageService;
  55.     /**
  56.      * @var SystemConfigService
  57.      */
  58.     private SystemConfigService $configService;
  59.     private ExclusionService $exclusionService;
  60.     private ShopSwitchHelperService $helperService;
  61.     public function __construct(
  62.         ShopSwitchRuleGateway $shopSwitchRuleGateway,
  63.         ShopSwitchRuleService $shopSwitchRuleService,
  64.         IpService $ipService,
  65.         ShopSwitchRedirectUrlService $redirectUrlService,
  66.         BotService $botService,
  67.         ContextLanguageService $contextLanguageService,
  68.         SystemConfigService $configService,
  69.         ExclusionService $exclusionService,
  70.         ShopSwitchHelperService $helperService
  71.     ) {
  72.         $this->shopSwitchRuleGateway $shopSwitchRuleGateway;
  73.         $this->shopSwitchRuleService $shopSwitchRuleService;
  74.         $this->ipService $ipService;
  75.         $this->redirectUrlService $redirectUrlService;
  76.         $this->botService $botService;
  77.         $this->contextLanguageService $contextLanguageService;
  78.         $this->configService $configService;
  79.         $this->exclusionService $exclusionService;
  80.         $this->helperService $helperService;
  81.     }
  82.     public function getDecorated(): AbstractShopSwitchRoute
  83.     {
  84.         throw new DecorationPatternException(self::class);
  85.     }
  86.     /**
  87.      * @Since("6.3.4.0")
  88.      * @Route("/store-api/acris-shop-switch-rules/{activeDomainId}", name="store-api.acris-shop-switch.rules", methods={"GET", "POST"})
  89.      */
  90.     public function load(string $activeDomainIdRequest $requestContext $context, ?string $languageId, ?string $fixedLanguageId null, ?string $fixedCountryIso null): ShopSwitchRouteResponse
  91.     {
  92.         $activeDomainEntity $this->helperService->getDomainById($activeDomainId$context);
  93.         if(empty($languageId)) {
  94.             $languageId $activeDomainEntity->getLanguageId();
  95.         }
  96.         $activeSalesChannel $activeDomainEntity->getSalesChannel();
  97.         $salesChannelId $activeSalesChannel->getId();
  98.         if(empty($fixedLanguageId) === true) {
  99.             $browserLanguageIso $this->getBrowserLanguageIso($request);
  100.             $context $this->contextLanguageService->getContextByLanguageId($context$languageId);
  101.             $languageIds $this->getLanguageIdsByLanguageIso($browserLanguageIso$context$salesChannelId);
  102.             $languageId reset($languageIds);
  103.             if($this->configService->get('AcrisShopSwitchCS.config.modalWindowLanguage'$salesChannelId) === 'browserLanguage' && !empty($languageId)) {
  104.                 $context $this->switchContextByBrowserLanguage($languageId$context);
  105.             }
  106.         } else {
  107.             $languageIds = [$fixedLanguageId];
  108.         }
  109.         $activeRule $this->shopSwitchRuleGateway->getActiveRuleByDomain($activeDomainId$context);
  110.         $activeDomainUrl $this->redirectUrlService->getUrlForActiveDomain($request$activeDomainEntity);
  111.         if(!$activeRule instanceof ShopSwitchRuleEntity) {
  112.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  113.         }
  114.         $activeRuleDomain $this->shopSwitchRuleService->getActiveRuleDomain($activeDomainId$activeRule);
  115.         if(empty($activeRuleDomain)) {
  116.             throw new ActiveDomainNotFoundException();
  117.         }
  118.         if($this->botService->isBot($request) === true) {
  119.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  120.         }
  121.         if($this->exclusionService->isExcluded($this->ipService->getClientIp($request$salesChannelId), $request$context) === true) {
  122.             return new ShopSwitchRouteResponse(new ShopSwitchResult($activeDomainId, new PossibleCountryCollection(), new PossibleLanguageCollection(), null$activeDomainUrltrue));
  123.         }
  124.         return $this->helperService->getValidResponse($activeDomainId$activeRule$activeDomainUrl$request$activeSalesChannel$context$activeRuleDomain$languageIds$fixedCountryIso);
  125.     }
  126.     private function getBrowserLanguageIso(Request $request): ?string
  127.     {
  128.         $browserAcceptLanguage $request->server->get('HTTP_ACCEPT_LANGUAGE');
  129.         if(empty($browserAcceptLanguage) === true) {
  130.             return $browserAcceptLanguage;
  131.         }
  132.         return strtolower(substr($browserAcceptLanguage05));
  133.     }
  134.     private function getLanguageIdsByLanguageIso(?string $languageIsoContext $contextstring $salesChannelId): array
  135.     {
  136.         if(empty($languageIso) === true) {
  137.             return [];
  138.         }
  139.         $languageIds $this->contextLanguageService->getLanguageIdByIso($languageIso$context);
  140.         if(empty($languageIds) === true) {
  141.             $languageIdFromConfig $this->configService->get('AcrisShopSwitchCS.config.fallbackModalWindowLanguage'$salesChannelId);
  142.             if(!empty($languageIdFromConfig)) {
  143.                 $languageIds = [$languageIdFromConfig];
  144.             }
  145.         }
  146.         return $languageIds;
  147.     }
  148.     private function switchContextByBrowserLanguage(?string $languageIdContext $context): Context
  149.     {
  150.         if(empty($languageId) === true) {
  151.             return $context;
  152.         }
  153.         return $this->contextLanguageService->getContextByLanguageId($context$languageId);
  154.     }
  155. }