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

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