custom/plugins/AcrisShopSwitchCS/src/Subscriber/ShopSwitchModalDataSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Subscriber;
  3. use Acris\ShopSwitch\Components\ShopSwitch\AbstractShopSwitchRoute;
  4. use Acris\ShopSwitch\Components\ShopSwitch\ShopSwitchResult;
  5. use Acris\ShopSwitch\Components\ShopSwitchCookieService;
  6. use Acris\ShopSwitch\Custom\ShopSwitchRuleDefinition;
  7. use Acris\ShopSwitch\Custom\ShopSwitchRuleEntity;
  8. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  9. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  10. use Shopware\Core\Framework\Struct\ArrayEntity;
  11. use Shopware\Core\SalesChannelRequest;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  14. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Contracts\Cache\ItemInterface;
  17. use Shopware\Core\Framework\Context;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class ShopSwitchModalDataSubscriber implements EventSubscriberInterface
  20. {
  21.     public const SHOP_SWITCH_MODAL_LOADED_AFTER 'shopSwitchModalLoadedAfter';
  22.     private AbstractShopSwitchRoute $shopSwitchRoute;
  23.     private TagAwareAdapterInterface $cache;
  24.     private AbstractCacheTracer $tracer;
  25.     private SystemConfigService $configService;
  26.     private ShopSwitchCookieService $shopSwitchCookieService;
  27.     public function __construct(AbstractShopSwitchRoute $shopSwitchRouteTagAwareAdapterInterface $cacheAbstractCacheTracer $tracerSystemConfigService $configServiceShopSwitchCookieService $shopSwitchCookieService)
  28.     {
  29.         $this->shopSwitchRoute $shopSwitchRoute;
  30.         $this->cache $cache;
  31.         $this->tracer $tracer;
  32.         $this->configService $configService;
  33.         $this->shopSwitchCookieService $shopSwitchCookieService;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded',
  39.         ];
  40.     }
  41.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
  42.     {
  43.         if($this->configService->get('AcrisShopSwitchCS.config.loadShopSwitchModal'$event->getSalesChannelContext()->getSalesChannelId()) !== 'enableJs') {
  44.             return;
  45.         }
  46.         $salesChannelContext $event->getSalesChannelContext();
  47.         if($salesChannelContext->hasExtension(ShopSwitchResult::MODAL_EXTENSION_KEY)) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         $context $event->getContext();
  52.         $activeDomainId $request->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_ID);
  53.         // do only if at the page the modal window is not loaded anymore
  54.         if($this->shopSwitchCookieService->isAlreadyConfigured($request$activeDomainId) === false) {
  55.             return;
  56.         }
  57.         $currentShippingCountryIso $salesChannelContext->getShippingLocation()->getCountry()->getIso();
  58.         $currentLanguageId $salesChannelContext->getLanguageId();
  59.         $key $this->generateKey($activeDomainId$currentShippingCountryIso);
  60.         $shopSwitchResult $this->getFromCache($key$activeDomainId$currentShippingCountryIso$currentLanguageId$request$context);
  61.         if ($shopSwitchResult instanceof ShopSwitchResult) {
  62.             $shopSwitchResult->setLoadImmediately(false);
  63.             if($shopSwitchResult->getShopSwitchRuleEntity() instanceof ShopSwitchRuleEntity) {
  64.                 $salesChannelContext->addExtension(ShopSwitchResult::MODAL_EXTENSION_KEY$shopSwitchResult);
  65.                 $salesChannelContext->addExtension(self::SHOP_SWITCH_MODAL_LOADED_AFTER, new ArrayEntity([]));
  66.             }
  67.         }
  68.     }
  69.     private function getFromCache($keystring $activeDomainIdstring $currentShippingCountryIsostring $currentLanguageIdRequest $requestContext $context): ?ShopSwitchResult
  70.     {
  71.         $value $this->cache->get($key, function (ItemInterface $item) use ($activeDomainId$currentShippingCountryIso$currentLanguageId$request$context) {
  72.             $name self::buildName($activeDomainId$currentShippingCountryIso);
  73.             /** @var ShopSwitchResult $shopSwitchResult */
  74.             $shopSwitchResult $this->tracer->trace($name, function () use ($activeDomainId$currentShippingCountryIso$currentLanguageId$request$context) {
  75.                 return $this->shopSwitchRoute->load($activeDomainId$request$context$currentLanguageId$currentLanguageId$currentShippingCountryIso)->getResult();
  76.             });
  77.             // only load configurations with switch type is modal
  78.             if($shopSwitchResult->getShopSwitchRuleEntity() instanceof ShopSwitchRuleEntity) {
  79.                 if($shopSwitchResult->getShopSwitchRuleEntity()->getSwitchType() !== ShopSwitchRuleDefinition::SWITCH_TYPE_MODAL) {
  80.                     $shopSwitchResult null;
  81.                 }
  82.             }
  83.             $item->tag($this->generateTags($activeDomainId$currentShippingCountryIso));
  84.             return CacheValueCompressor::compress($shopSwitchResult);
  85.         });
  86.         return CacheValueCompressor::uncompress($value);
  87.     }
  88.     public static function buildName(string $activeDomainIdstring $shippingCountryIso): string
  89.     {
  90.         return 'shop-switch-modal-data-' $activeDomainId '-' $shippingCountryIso;
  91.     }
  92.     private function generateKey(string $activeDomainIdstring $shippingCountryIso) {
  93.         return 'shop-switch-modal-data-' $activeDomainId '-' $shippingCountryIso;
  94.     }
  95.     private function generateTags(string $activeDomainIdstring $shippingCountryIso): array
  96.     {
  97.         $tags array_merge(
  98.             $this->tracer->get(self::buildName($activeDomainId$shippingCountryIso)),
  99.             [self::buildName($activeDomainId$shippingCountryIso)],
  100.             [ShopSwitchCacheSubscriber::HEADER_CACHE_TAG],
  101.             ['sales-channel'],
  102.             ['sales-channel-context'],
  103.             ['navigation']
  104.         );
  105.         return array_unique($tags);
  106.     }
  107. }