platform/src/Core/Content/Flow/Indexing/FlowIndexer.php line 64

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\DataAbstractionLayer\Dbal\Common\IteratorFactory;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexer;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexingMessage;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\MessageQueue\IterateEntityIndexerMessage;
  11. use Shopware\Core\Framework\Plugin\Event\PluginPostActivateEvent;
  12. use Shopware\Core\Framework\Plugin\Event\PluginPostDeactivateEvent;
  13. use Shopware\Core\Framework\Plugin\Event\PluginPostInstallEvent;
  14. use Shopware\Core\Framework\Plugin\Event\PluginPostUninstallEvent;
  15. use Shopware\Core\Framework\Plugin\Event\PluginPostUpdateEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Messenger\MessageBusInterface;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. class FlowIndexer extends EntityIndexer implements EventSubscriberInterface
  20. {
  21.     private IteratorFactory $iteratorFactory;
  22.     private EntityRepositoryInterface $repository;
  23.     private FlowPayloadUpdater $payloadUpdater;
  24.     private EventDispatcherInterface $eventDispatcher;
  25.     private MessageBusInterface $messageBus;
  26.     public function __construct(
  27.         IteratorFactory $iteratorFactory,
  28.         EntityRepositoryInterface $repository,
  29.         FlowPayloadUpdater $payloadUpdater,
  30.         EventDispatcherInterface $eventDispatcher,
  31.         MessageBusInterface $messageBus
  32.     ) {
  33.         $this->iteratorFactory $iteratorFactory;
  34.         $this->repository $repository;
  35.         $this->payloadUpdater $payloadUpdater;
  36.         $this->eventDispatcher $eventDispatcher;
  37.         $this->messageBus $messageBus;
  38.     }
  39.     public function getName(): string
  40.     {
  41.         return 'flow.indexer';
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             PluginPostInstallEvent::class => 'refreshPlugin',
  47.             PluginPostActivateEvent::class => 'refreshPlugin',
  48.             PluginPostUpdateEvent::class => 'refreshPlugin',
  49.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  50.             PluginPostUninstallEvent::class => 'refreshPlugin',
  51.         ];
  52.     }
  53.     public function refreshPlugin(): void
  54.     {
  55.         // Schedule indexer to update flows
  56.         $this->messageBus->dispatch(new IterateEntityIndexerMessage($this->getName(), null));
  57.     }
  58.     /**
  59.      * @param array|null $offset
  60.      *
  61.      * @deprecated tag:v6.5.0 The parameter $offset will be native typed
  62.      */
  63.     public function iterate(/*?array */$offset): ?EntityIndexingMessage
  64.     {
  65.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  66.         $ids $iterator->fetch();
  67.         if (empty($ids)) {
  68.             return null;
  69.         }
  70.         return new FlowIndexingMessage(array_values($ids), $iterator->getOffset());
  71.     }
  72.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  73.     {
  74.         $updates $event->getPrimaryKeys(FlowDefinition::ENTITY_NAME);
  75.         if (empty($updates)) {
  76.             return null;
  77.         }
  78.         $this->handle(new FlowIndexingMessage(array_values($updates), null$event->getContext()));
  79.         return null;
  80.     }
  81.     public function handle(EntityIndexingMessage $message): void
  82.     {
  83.         $ids array_unique(array_filter($message->getData()));
  84.         if (empty($ids)) {
  85.             return;
  86.         }
  87.         $this->payloadUpdater->update($ids);
  88.         $this->eventDispatcher->dispatch(new FlowIndexerEvent($ids$message->getContext()));
  89.     }
  90. }