vendor/shopware/platform/src/Core/Content/Rule/DataAbstractionLayer/RuleIndexer.php line 90

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.     /**
  25.      * @var IteratorFactory
  26.      */
  27.     private $iteratorFactory;
  28.     /**
  29.      * @var Connection
  30.      */
  31.     private $connection;
  32.     /**
  33.      * @var EntityRepositoryInterface
  34.      */
  35.     private $repository;
  36.     /**
  37.      * @var RulePayloadUpdater
  38.      */
  39.     private $payloadUpdater;
  40.     /**
  41.      * @var EventDispatcherInterface
  42.      */
  43.     private $eventDispatcher;
  44.     /**
  45.      * @var CartRuleLoader
  46.      */
  47.     private $cartRuleLoader;
  48.     public function __construct(
  49.         Connection $connection,
  50.         IteratorFactory $iteratorFactory,
  51.         EntityRepositoryInterface $repository,
  52.         RulePayloadUpdater $payloadUpdater,
  53.         CartRuleLoader $cartRuleLoader,
  54.         EventDispatcherInterface $eventDispatcher
  55.     ) {
  56.         $this->iteratorFactory $iteratorFactory;
  57.         $this->repository $repository;
  58.         $this->connection $connection;
  59.         $this->payloadUpdater $payloadUpdater;
  60.         $this->eventDispatcher $eventDispatcher;
  61.         $this->cartRuleLoader $cartRuleLoader;
  62.     }
  63.     public function getName(): string
  64.     {
  65.         return 'rule.indexer';
  66.     }
  67.     public static function getSubscribedEvents(): array
  68.     {
  69.         return [
  70.             PluginPostInstallEvent::class => 'refreshPlugin',
  71.             PluginPostActivateEvent::class => 'refreshPlugin',
  72.             PluginPostUpdateEvent::class => 'refreshPlugin',
  73.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  74.             PluginPostUninstallEvent::class => 'refreshPlugin',
  75.             RuleEvents::RULE_WRITTEN_EVENT => 'onRuleWritten',
  76.         ];
  77.     }
  78.     public function refreshPlugin(): void
  79.     {
  80.         // Delete the payload and invalid flag of all rules
  81.         $update = new RetryableQuery(
  82.             $this->connection->prepare('UPDATE `rule` SET `payload` = null, `invalid` = 0')
  83.         );
  84.         $update->execute();
  85.     }
  86.     public function iterate($offset): ?EntityIndexingMessage
  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.         $this->payloadUpdater->update($ids);
  112.         $this->eventDispatcher->dispatch(new RuleIndexerEvent($ids$message->getContext()));
  113.     }
  114.     public function onRuleWritten(EntityWrittenEvent $event): void
  115.     {
  116.         $this->cartRuleLoader->reset();
  117.     }
  118. }