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

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\Feature;
  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 Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  24. /**
  25.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  26.  */
  27. class RuleIndexer extends EntityIndexer implements EventSubscriberInterface
  28. {
  29.     public const PAYLOAD_UPDATER 'rule.payload';
  30.     private IteratorFactory $iteratorFactory;
  31.     private Connection $connection;
  32.     private EntityRepositoryInterface $repository;
  33.     private RulePayloadUpdater $payloadUpdater;
  34.     private EventDispatcherInterface $eventDispatcher;
  35.     private CartRuleLoader $cartRuleLoader;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         Connection $connection,
  41.         IteratorFactory $iteratorFactory,
  42.         EntityRepositoryInterface $repository,
  43.         RulePayloadUpdater $payloadUpdater,
  44.         CartRuleLoader $cartRuleLoader,
  45.         EventDispatcherInterface $eventDispatcher
  46.     ) {
  47.         $this->iteratorFactory $iteratorFactory;
  48.         $this->repository $repository;
  49.         $this->connection $connection;
  50.         $this->payloadUpdater $payloadUpdater;
  51.         $this->eventDispatcher $eventDispatcher;
  52.         $this->cartRuleLoader $cartRuleLoader;
  53.     }
  54.     public function getName(): string
  55.     {
  56.         return 'rule.indexer';
  57.     }
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             PluginPostInstallEvent::class => 'refreshPlugin',
  62.             PluginPostActivateEvent::class => 'refreshPlugin',
  63.             PluginPostUpdateEvent::class => 'refreshPlugin',
  64.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  65.             PluginPostUninstallEvent::class => 'refreshPlugin',
  66.             RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
  67.         ];
  68.     }
  69.     public function refreshPlugin(): void
  70.     {
  71.         // Delete the payload and invalid flag of all rules
  72.         $update = new RetryableQuery(
  73.             $this->connection,
  74.             $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
  75.         );
  76.         $update->execute();
  77.     }
  78.     /**
  79.      * @param array{offset: string}|null $offset
  80.      *
  81.      * @deprecated tag:v6.5.0 The parameter $offset will be native typed
  82.      */
  83.     public function iterate(/*?array */$offset): ?EntityIndexingMessage
  84.     {
  85.         if ($offset !== null && !\is_array($offset)) {
  86.             Feature::triggerDeprecationOrThrow(
  87.                 'v6.5.0.0',
  88.                 'Parameter `$offset` of method "iterate()" in class "RuleIndexer" will be natively typed to `?array` in v6.5.0.0.'
  89.             );
  90.         }
  91.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  92.         $ids $iterator->fetch();
  93.         if (empty($ids)) {
  94.             return null;
  95.         }
  96.         return new RuleIndexingMessage(array_values($ids), $iterator->getOffset());
  97.     }
  98.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  99.     {
  100.         $updates $event->getPrimaryKeys(RuleDefinition::ENTITY_NAME);
  101.         if (empty($updates)) {
  102.             return null;
  103.         }
  104.         $this->handle(new RuleIndexingMessage(array_values($updates), null$event->getContext()));
  105.         return null;
  106.     }
  107.     public function handle(EntityIndexingMessage $message): void
  108.     {
  109.         $ids $message->getData();
  110.         $ids array_unique(array_filter($ids));
  111.         if (empty($ids)) {
  112.             return;
  113.         }
  114.         if ($message->allow(self::PAYLOAD_UPDATER)) {
  115.             $this->payloadUpdater->update($ids);
  116.         }
  117.         $this->eventDispatcher->dispatch(new RuleIndexerEvent($ids$message->getContext(), $message->getSkip()));
  118.     }
  119.     public function getTotal(): int
  120.     {
  121.         return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
  122.     }
  123.     public function getDecorated(): EntityIndexer
  124.     {
  125.         throw new DecorationPatternException(static::class);
  126.     }
  127.     public function onRuleWritten(EntityWrittenEvent $event): void
  128.     {
  129.         $this->cartRuleLoader->invalidate();
  130.     }
  131. }