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

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