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

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