vendor/shopware/core/Content/Rule/DataAbstractionLayer/RuleIndexer.php line 98

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Rule\DataAbstractionLayer;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Cart\CartRuleLoader;
  5. use Shopware\Core\Content\Rule\Event\RuleIndexerEvent;
  6. use Shopware\Core\Content\Rule\RuleDefinition;
  7. use Shopware\Core\Content\Rule\RuleEvents;
  8. use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
  16. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  17. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  18. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  19. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  20. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. class RuleIndexer extends EntityIndexer implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var IteratorFactory
  27.      */
  28.     private $iteratorFactory;
  29.     /**
  30.      * @var Connection
  31.      */
  32.     private $connection;
  33.     /**
  34.      * @var EntityRepositoryInterface
  35.      */
  36.     private $repository;
  37.     /**
  38.      * @var CacheClearer
  39.      */
  40.     private $cacheClearer;
  41.     /**
  42.      * @var RulePayloadUpdater
  43.      */
  44.     private $payloadUpdater;
  45.     /**
  46.      * @var EventDispatcherInterface
  47.      */
  48.     private $eventDispatcher;
  49.     /**
  50.      * @var CartRuleLoader
  51.      */
  52.     private $cartRuleLoader;
  53.     public function __construct(
  54.         Connection $connection,
  55.         IteratorFactory $iteratorFactory,
  56.         EntityRepositoryInterface $repository,
  57.         CacheClearer $cacheClearer,
  58.         RulePayloadUpdater $payloadUpdater,
  59.         CartRuleLoader $cartRuleLoader,
  60.         EventDispatcherInterface $eventDispatcher
  61.     ) {
  62.         $this->iteratorFactory $iteratorFactory;
  63.         $this->repository $repository;
  64.         $this->connection $connection;
  65.         $this->cacheClearer $cacheClearer;
  66.         $this->payloadUpdater $payloadUpdater;
  67.         $this->eventDispatcher $eventDispatcher;
  68.         $this->cartRuleLoader $cartRuleLoader;
  69.     }
  70.     public function getName(): string
  71.     {
  72.         return 'rule.indexer';
  73.     }
  74.     public static function getSubscribedEvents(): array
  75.     {
  76.         return [
  77.             PluginPostInstallEvent::class => 'refreshPlugin',
  78.             PluginPostActivateEvent::class => 'refreshPlugin',
  79.             PluginPostUpdateEvent::class => 'refreshPlugin',
  80.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  81.             PluginPostUninstallEvent::class => 'refreshPlugin',
  82.             RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
  83.         ];
  84.     }
  85.     public function refreshPlugin(): void
  86.     {
  87.         // Delete the payload and invalid flag of all rules
  88.         $update = new RetryableQuery(
  89.             $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
  90.         );
  91.         $update->execute();
  92.         // invalidates all cached queries to the `rule` table
  93.         $this->cacheClearer->invalidateTags(['entity_' RuleDefinition::ENTITY_NAME]);
  94.     }
  95.     public function iterate($offset): ?EntityIndexingMessage
  96.     {
  97.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  98.         $ids $iterator->fetch();
  99.         if (empty($ids)) {
  100.             return null;
  101.         }
  102.         return new RuleIndexingMessage(array_values($ids), $iterator->getOffset());
  103.     }
  104.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  105.     {
  106.         $updates $event->getPrimaryKeys(RuleDefinition::ENTITY_NAME);
  107.         if (empty($updates)) {
  108.             return null;
  109.         }
  110.         $this->handle(new RuleIndexingMessage(array_values($updates), null$event->getContext()));
  111.         return null;
  112.     }
  113.     public function handle(EntityIndexingMessage $message): void
  114.     {
  115.         $ids $message->getData();
  116.         $ids array_unique(array_filter($ids));
  117.         if (empty($ids)) {
  118.             return;
  119.         }
  120.         $this->payloadUpdater->update($ids);
  121.         $this->eventDispatcher->dispatch(new RuleIndexerEvent($ids$message->getContext()));
  122.         $this->cacheClearer->invalidateIds($idsRuleDefinition::ENTITY_NAME);
  123.     }
  124.     public function onRuleWritten(EntityWrittenEvent $event): void
  125.     {
  126.         $this->cartRuleLoader->reset();
  127.     }
  128. }