custom/plugins/VioCustomerPrice/src/Subscriber/CacheKeySubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioCustomerPrice\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  6. use Shopware\Core\Content\Product\Events\CrossSellingRouteCacheKeyEvent;
  7. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheKeyEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheKeyEvent;
  10. use Shopware\Core\Content\Product\Events\ProductSuggestRouteCacheKeyEvent;
  11. use Shopware\Core\Framework\Adapter\Cache\StoreApiRouteCacheKeyEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use VioCustomerPrice\Struct\HasCustomerPriceStruct;
  17. class CacheKeySubscriber implements EventSubscriberInterface
  18. {
  19.     private EntityRepositoryInterface $customerPriceRepository;
  20.     public function __construct(
  21.         EntityRepositoryInterface $customerPriceRepository
  22.     )
  23.     {
  24.         $this->customerPriceRepository $customerPriceRepository;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ProductDetailRouteCacheKeyEvent::class => 'addCustomerPart',
  30.             ProductListingRouteCacheKeyEvent::class => 'addCustomerPart',
  31.             ProductSearchRouteCacheKeyEvent::class => 'addCustomerPart',
  32.             ProductSuggestRouteCacheKeyEvent::class => 'addCustomerPart',
  33.             CrossSellingRouteCacheKeyEvent::class => 'addCustomerPart',
  34.             CategoryRouteCacheKeyEvent::class => 'addCustomerPart'
  35.         ];
  36.     }
  37.     public function addCustomerPart(StoreApiRouteCacheKeyEvent $event): void
  38.     {
  39.         $context $event->getContext();
  40.         if($context->getCustomer() instanceof CustomerEntity) {
  41.             // check if customer has any customer price
  42.             if(!$context->hasExtensionOfType('hasCustomerPrice'HasCustomerPriceStruct::class)) {
  43.                 $hasCustomerPrice $this->customerPriceRepository->searchIds(
  44.                         (new Criteria())
  45.                             ->addFilter(
  46.                                 new EqualsFilter('customerId'$context->getCustomer()->getId())
  47.                             )
  48.                             ->setLimit(1)
  49.                         , $context->getContext()
  50.                     )->firstId() !== null;
  51.                 $context->addExtension('hasCustomerPrice', new HasCustomerPriceStruct($hasCustomerPrice));
  52.             }
  53.             else {
  54.                 /** @var HasCustomerPriceStruct $extension */
  55.                 $extension $context->getExtensionOfType('hasCustomerPrice'HasCustomerPriceStruct::class);
  56.                 $hasCustomerPrice $extension->getHasCustomerPrice();
  57.             }
  58.             if ($hasCustomerPrice) {
  59.                 $event->addPart($context->getCustomer()->getId());
  60.             }
  61.         }
  62.     }
  63. }