custom/plugins/MbdusArticleSort/src/Subscriber/SortWritten.php line 44

Open in your IDE?
  1. <?php
  2. namespace MbdusArticleSort\Subscriber;
  3. use MbdusArticleSort\Product\ProductCategoryiesSortDefinition;
  4. use Shopware\Core\Content\Category\CategoryDefinition;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class SortWritten implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var ProductCategoryDefinition
  14.      */
  15.     private $categoryProductSortRepository;
  16.     /**
  17.      * @var CacheClearer
  18.      */
  19.     private $cacheClearer;
  20.     /**
  21.      * @var EntityCacheKeyGenerator
  22.      */
  23.     private $cacheKeyGenerator;
  24.     public function __construct(
  25.         EntityRepositoryInterface $categoryProductSort
  26.     )
  27.     {
  28.         $this->categoryProductSortRepository $categoryProductSort;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             'mbdus_productcategoriessort.written' => 'entityWritten'
  34.         ];
  35.     }
  36.     public function entityWritten(EntityWrittenEvent $entityWrittenEvent)
  37.     {
  38.         if (empty($entityWrittenEvent->getWriteResults())) {
  39.             return;
  40.         }
  41.         $entityWriteResult $entityWrittenEvent->getWriteResults()[0];
  42.       
  43.         if ($entityWriteResult->getOperation() !== 'update') {
  44.     //        return;
  45.         }
  46.         $sortId $entityWriteResult->getPayload()['id'];
  47.         $criteria = new Criteria([$sortId]);
  48.         $criteria->addAssociation('category');
  49.         $criteria->addAssociation('product');
  50.         $categoryProductSort $this->categoryProductSortRepository->search($criteria$entityWrittenEvent->getContext())->first();
  51.         $categoryId $categoryProductSort->get('category')->getId();
  52.         $productId $categoryProductSort->get('product')->getId();
  53.  //       $this->invalidateCategoryEntityCache($categoryId);
  54.  //       $this->invalidateProductEntityCache($productId);
  55.     }
  56.     private function invalidateCategoryEntityCache($categoryId)
  57.     {
  58.         $tags = [CategoryDefinition::ENTITY_NAME '.id'];
  59.         $tags[] = $this->cacheKeyGenerator->getEntityTag($categoryIdCategoryDefinition::ENTITY_NAME);
  60.         $this->cacheClearer->invalidateTags($tags);
  61.     }
  62.     private function invalidateProductEntityCache($productId)
  63.     {
  64.         $tags[] = $this->cacheKeyGenerator->getEntityTag($productIdProductDefinition::ENTITY_NAME);
  65.         $this->cacheClearer->invalidateTags($tags);
  66.     }
  67. }