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

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. /**
  27.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  28.  */
  29. class FlowIndexer extends EntityIndexer implements EventSubscriberInterface
  30. {
  31.     private IteratorFactory $iteratorFactory;
  32.     private EntityRepositoryInterface $repository;
  33.     private FlowPayloadUpdater $payloadUpdater;
  34.     private EventDispatcherInterface $eventDispatcher;
  35.     private MessageBusInterface $messageBus;
  36.     /**
  37.      * @internal
  38.      */
  39.     public function __construct(
  40.         IteratorFactory $iteratorFactory,
  41.         EntityRepositoryInterface $repository,
  42.         FlowPayloadUpdater $payloadUpdater,
  43.         EventDispatcherInterface $eventDispatcher,
  44.         MessageBusInterface $messageBus
  45.     ) {
  46.         $this->iteratorFactory $iteratorFactory;
  47.         $this->repository $repository;
  48.         $this->payloadUpdater $payloadUpdater;
  49.         $this->eventDispatcher $eventDispatcher;
  50.         $this->messageBus $messageBus;
  51.     }
  52.     public function getName(): string
  53.     {
  54.         return 'flow.indexer';
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             PluginPostInstallEvent::class => 'refreshPlugin',
  60.             PluginPostActivateEvent::class => 'refreshPlugin',
  61.             PluginPostUpdateEvent::class => 'refreshPlugin',
  62.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  63.             PluginPostUninstallEvent::class => 'refreshPlugin',
  64.             AppInstalledEvent::class => 'refreshPlugin',
  65.             AppUpdatedEvent::class => 'refreshPlugin',
  66.             AppActivatedEvent::class => 'refreshPlugin',
  67.             AppDeletedEvent::class => 'refreshPlugin',
  68.             AppDeactivatedEvent::class => 'refreshPlugin',
  69.         ];
  70.     }
  71.     public function refreshPlugin(): void
  72.     {
  73.         // Schedule indexer to update flows
  74.         $this->messageBus->dispatch(new IterateEntityIndexerMessage($this->getName(), null));
  75.     }
  76.     /**
  77.      * @param array{offset: string}|null $offset
  78.      *
  79.      * @deprecated tag:v6.5.0 The parameter $offset will be native typed
  80.      */
  81.     public function iterate(/*?array */$offset): ?EntityIndexingMessage
  82.     {
  83.         if ($offset !== null && !\is_array($offset)) {
  84.             Feature::triggerDeprecationOrThrow(
  85.                 'v6.5.0.0',
  86.                 'Parameter `$offset` of method "iterate()" in class "FlowIndexer" will be natively typed to `?array` in v6.5.0.0.'
  87.             );
  88.         }
  89.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  90.         $ids $iterator->fetch();
  91.         if (empty($ids)) {
  92.             return null;
  93.         }
  94.         return new FlowIndexingMessage(array_values($ids), $iterator->getOffset());
  95.     }
  96.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  97.     {
  98.         $updates $event->getPrimaryKeys(FlowDefinition::ENTITY_NAME);
  99.         if (empty($updates)) {
  100.             return null;
  101.         }
  102.         $this->handle(new FlowIndexingMessage(array_values($updates), null$event->getContext()));
  103.         return null;
  104.     }
  105.     public function handle(EntityIndexingMessage $message): void
  106.     {
  107.         $ids array_unique(array_filter($message->getData()));
  108.         if (empty($ids)) {
  109.             return;
  110.         }
  111.         $this->payloadUpdater->update($ids);
  112.         $this->eventDispatcher->dispatch(new FlowIndexerEvent($ids$message->getContext()));
  113.     }
  114.     public function getTotal(): int
  115.     {
  116.         return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
  117.     }
  118.     public function getDecorated(): EntityIndexer
  119.     {
  120.         throw new DecorationPatternException(static::class);
  121.     }
  122. }