custom/plugins/b2bsellerscore/src/Components/CustomerPrice/Subscriber/CacheKeyEventSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\CustomerPrice\Subscriber;
  3. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\CrossSellingRouteCacheKeyEvent;
  5. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheKeyEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheKeyEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSuggestRouteCacheKeyEvent;
  9. use Shopware\Core\Framework\Adapter\Cache\StoreApiRouteCacheKeyEvent;
  10. use Shopware\Core\PlatformRequest;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CacheKeyEventSubscriber implements EventSubscriberInterface
  15. {
  16.     private SystemConfigService $configService;
  17.     public function __construct(
  18.         SystemConfigService $configService
  19.     )
  20.     {
  21.         $this->configService $configService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CategoryRouteCacheKeyEvent::class => ['disableCache'100],
  27.             ProductDetailRouteCacheKeyEvent::class => ['disableCache'100],
  28.             ProductSuggestRouteCacheKeyEvent::class => ['disableCache'100],
  29.             ProductListingRouteCacheKeyEvent::class => ['disableCache'100],
  30.             ProductSearchRouteCacheKeyEvent::class => ['disableCache'100],
  31.             CrossSellingRouteCacheKeyEvent::class => ['disableCache'100]
  32.         ];
  33.     }
  34.     public function disableCache(StoreApiRouteCacheKeyEvent $event): void
  35.     {
  36.         $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  37.         if (!$context instanceof SalesChannelContext) {
  38.             return;
  39.         }
  40.         if($this->configService->getBool('B2bSellersCore.config.enableCustomerSpecificCaching'$event->getSalesChannelId())) {
  41.             $event->addPart($this->getCustomerHash($context));
  42.             return;
  43.         }
  44.         $event->disableCaching();
  45.     }
  46.     public function getCustomerHash(SalesChannelContext $context): string
  47.     {
  48.         return md5((string)json_encode([
  49.             $context->getCustomerId(),
  50.             $context->getTaxState()
  51.         ], JSON_THROW_ON_ERROR));
  52.     }
  53. }