vendor/shopware/core/Content/Flow/Indexing/FlowIndexer.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Indexing;
  3. use Shopware\Core\Content\Flow\Events\FlowIndexerEvent;
  4. use Shopware\Core\Content\Flow\FlowDefinition;
  5. use Shopware\Core\Framework\App\Event\AppActivatedEvent;
  6. use Shopware\Core\Framework\App\Event\AppDeactivatedEvent;
  7. use Shopware\Core\Framework\App\Event\AppDeletedEvent;
  8. use Shopware\Core\Framework\App\Event\AppInstalledEvent;
  9. use Shopware\Core\Framework\App\Event\AppUpdatedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\IteratorFactory;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\MessageQueue\IterateEntityIndexerMessage;
  16. use Shopware\Core\Framework\Feature;
  17. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  18. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  19. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  20. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  21. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  22. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\Messenger\MessageBusInterface;
  25. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  26. class FlowIndexer extends EntityIndexer implements EventSubscriberInterface
  27. {
  28.     private IteratorFactory $iteratorFactory;
  29.     private EntityRepositoryInterface $repository;
  30.     private FlowPayloadUpdater $payloadUpdater;
  31.     private EventDispatcherInterface $eventDispatcher;
  32.     private MessageBusInterface $messageBus;
  33.     /**
  34.      * @internal
  35.      */
  36.     public function __construct(
  37.         IteratorFactory $iteratorFactory,
  38.         EntityRepositoryInterface $repository,
  39.         FlowPayloadUpdater $payloadUpdater,
  40.         EventDispatcherInterface $eventDispatcher,
  41.         MessageBusInterface $messageBus
  42.     ) {
  43.         $this->iteratorFactory $iteratorFactory;
  44.         $this->repository $repository;
  45.         $this->payloadUpdater $payloadUpdater;
  46.         $this->eventDispatcher $eventDispatcher;
  47.         $this->messageBus $messageBus;
  48.     }
  49.     public function getName(): string
  50.     {
  51.         return 'flow.indexer';
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             PluginPostInstallEvent::class => 'refreshPlugin',
  57.             PluginPostActivateEvent::class => 'refreshPlugin',
  58.             PluginPostUpdateEvent::class => 'refreshPlugin',
  59.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  60.             PluginPostUninstallEvent::class => 'refreshPlugin',
  61.             AppInstalledEvent::class => 'refreshPlugin',
  62.             AppUpdatedEvent::class => 'refreshPlugin',
  63.             AppActivatedEvent::class => 'refreshPlugin',
  64.             AppDeletedEvent::class => 'refreshPlugin',
  65.             AppDeactivatedEvent::class => 'refreshPlugin',
  66.         ];
  67.     }
  68.     public function refreshPlugin(): void
  69.     {
  70.         // Schedule indexer to update flows
  71.         $this->messageBus->dispatch(new IterateEntityIndexerMessage($this->getName(), null));
  72.     }
  73.     /**
  74.      * @param array|null $offset
  75.      *
  76.      * @deprecated tag:v6.5.0 The parameter $offset will be native typed
  77.      */
  78.     public function iterate(/*?array */$offset): ?EntityIndexingMessage
  79.     {
  80.         if ($offset !== null && !\is_array($offset)) {
  81.             Feature::triggerDeprecationOrThrow(
  82.                 'v6.5.0.0',
  83.                 'Parameter `$offset` of method "iterate()" in class "FlowIndexer" will be natively typed to `?array` in v6.5.0.0.'
  84.             );
  85.         }
  86.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  87.         $ids $iterator->fetch();
  88.         if (empty($ids)) {
  89.             return null;
  90.         }
  91.         return new FlowIndexingMessage(array_values($ids), $iterator->getOffset());
  92.     }
  93.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  94.     {
  95.         $updates $event->getPrimaryKeys(FlowDefinition::ENTITY_NAME);
  96.         if (empty($updates)) {
  97.             return null;
  98.         }
  99.         $this->handle(new FlowIndexingMessage(array_values($updates), null$event->getContext()));
  100.         return null;
  101.     }
  102.     public function handle(EntityIndexingMessage $message): void
  103.     {
  104.         $ids array_unique(array_filter($message->getData()));
  105.         if (empty($ids)) {
  106.             return;
  107.         }
  108.         $this->payloadUpdater->update($ids);
  109.         $this->eventDispatcher->dispatch(new FlowIndexerEvent($ids$message->getContext()));
  110.     }
  111.     public function getTotal(): int
  112.     {
  113.         return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
  114.     }
  115.     public function getDecorated(): EntityIndexer
  116.     {
  117.         throw new DecorationPatternException(static::class);
  118.     }
  119. }