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

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