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

  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\EntityRepository;
  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\Log\Package;
  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.  * @internal
  28.  */
  29. #[Package('business-ops')]
  30. class FlowIndexer extends EntityIndexer implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @internal
  34.      */
  35.     public function __construct(private readonly IteratorFactory $iteratorFactory, private readonly EntityRepository $repository, private readonly FlowPayloadUpdater $payloadUpdater, private readonly EventDispatcherInterface $eventDispatcher, private readonly MessageBusInterface $messageBus)
  36.     {
  37.     }
  38.     public function getName(): string
  39.     {
  40.         return 'flow.indexer';
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             PluginPostInstallEvent::class => 'refreshPlugin',
  46.             PluginPostActivateEvent::class => 'refreshPlugin',
  47.             PluginPostUpdateEvent::class => 'refreshPlugin',
  48.             PluginPostDeactivateEvent::class => 'refreshPlugin',
  49.             PluginPostUninstallEvent::class => 'refreshPlugin',
  50.             AppInstalledEvent::class => 'refreshPlugin',
  51.             AppUpdatedEvent::class => 'refreshPlugin',
  52.             AppActivatedEvent::class => 'refreshPlugin',
  53.             AppDeletedEvent::class => 'refreshPlugin',
  54.             AppDeactivatedEvent::class => 'refreshPlugin',
  55.         ];
  56.     }
  57.     public function refreshPlugin(): void
  58.     {
  59.         // Schedule indexer to update flows
  60.         $this->messageBus->dispatch(new IterateEntityIndexerMessage($this->getName(), null));
  61.     }
  62.     public function iterate(?array $offset): ?EntityIndexingMessage
  63.     {
  64.         $iterator $this->iteratorFactory->createIterator($this->repository->getDefinition(), $offset);
  65.         $ids $iterator->fetch();
  66.         if (empty($ids)) {
  67.             return null;
  68.         }
  69.         return new FlowIndexingMessage(array_values($ids), $iterator->getOffset());
  70.     }
  71.     public function update(EntityWrittenContainerEvent $event): ?EntityIndexingMessage
  72.     {
  73.         $updates $event->getPrimaryKeys(FlowDefinition::ENTITY_NAME);
  74.         if (empty($updates)) {
  75.             return null;
  76.         }
  77.         $this->handle(new FlowIndexingMessage(array_values($updates), null$event->getContext()));
  78.         return null;
  79.     }
  80.     public function handle(EntityIndexingMessage $message): void
  81.     {
  82.         $ids array_unique(array_filter($message->getData()));
  83.         if (empty($ids)) {
  84.             return;
  85.         }
  86.         $this->payloadUpdater->update($ids);
  87.         $this->eventDispatcher->dispatch(new FlowIndexerEvent($ids$message->getContext()));
  88.     }
  89.     public function getTotal(): int
  90.     {
  91.         return $this->iteratorFactory->createIterator($this->repository->getDefinition())->fetchCount();
  92.     }
  93.     public function getDecorated(): EntityIndexer
  94.     {
  95.         throw new DecorationPatternException(static::class);
  96.     }
  97. }