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

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