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

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