|
15 | 15 | */ |
16 | 16 | class Processor |
17 | 17 | { |
| 18 | + /** |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + private $sharedIndexesComplete = []; |
| 22 | + |
18 | 23 | /** |
19 | 24 | * @var ConfigInterface |
20 | 25 | */ |
@@ -60,32 +65,83 @@ public function __construct( |
60 | 65 | */ |
61 | 66 | public function reindexAllInvalid() |
62 | 67 | { |
63 | | - $sharedIndexesComplete = []; |
64 | 68 | foreach (array_keys($this->config->getIndexers()) as $indexerId) { |
65 | 69 | /** @var Indexer $indexer */ |
66 | 70 | $indexer = $this->indexerFactory->create(); |
67 | 71 | $indexer->load($indexerId); |
68 | 72 | $indexerConfig = $this->config->getIndexer($indexerId); |
| 73 | + $sharedIndex = $indexerConfig['shared_index']; |
| 74 | + |
69 | 75 | if ($indexer->isInvalid()) { |
70 | 76 | // Skip indexers having shared index that was already complete |
71 | 77 | $sharedIndex = $indexerConfig['shared_index'] ?? null; |
72 | | - if (!in_array($sharedIndex, $sharedIndexesComplete)) { |
| 78 | + if (!in_array($sharedIndex, $this->sharedIndexesComplete)) { |
73 | 79 | $indexer->reindexAll(); |
74 | | - } else { |
75 | | - /** @var \Magento\Indexer\Model\Indexer\State $state */ |
76 | | - $state = $indexer->getState(); |
77 | | - $state->setStatus(StateInterface::STATUS_WORKING); |
78 | | - $state->save(); |
79 | | - $state->setStatus(StateInterface::STATUS_VALID); |
80 | | - $state->save(); |
81 | | - } |
82 | | - if ($sharedIndex) { |
83 | | - $sharedIndexesComplete[] = $sharedIndex; |
| 80 | + |
| 81 | + if ($sharedIndex) { |
| 82 | + $this->validateSharedIndex($sharedIndex); |
| 83 | + } |
84 | 84 | } |
85 | 85 | } |
86 | 86 | } |
87 | 87 | } |
88 | 88 |
|
| 89 | + /** |
| 90 | + * Get indexer ids that have common shared index |
| 91 | + * |
| 92 | + * @param string $sharedIndex |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + private function getIndexerIdsBySharedIndex(string $sharedIndex): array |
| 96 | + { |
| 97 | + $indexers = $this->config->getIndexers(); |
| 98 | + |
| 99 | + $result = []; |
| 100 | + foreach ($indexers as $indexerConfig) { |
| 101 | + if ($indexerConfig['shared_index'] == $sharedIndex) { |
| 102 | + $result[] = $indexerConfig['indexer_id']; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Validate indexers by shared index ID |
| 111 | + * |
| 112 | + * @param string $sharedIndex |
| 113 | + * @return $this |
| 114 | + */ |
| 115 | + private function validateSharedIndex(string $sharedIndex): self |
| 116 | + { |
| 117 | + if (empty($sharedIndex)) { |
| 118 | + throw new \InvalidArgumentException( |
| 119 | + 'The sharedIndex is an invalid shared index identifier. Verify the identifier and try again.' |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + $indexerIds = $this->getIndexerIdsBySharedIndex($sharedIndex); |
| 124 | + if (empty($indexerIds)) { |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + foreach ($indexerIds as $indexerId) { |
| 129 | + /** @var \Magento\Indexer\Model\Indexer $indexer */ |
| 130 | + $indexer = $this->indexerFactory->create(); |
| 131 | + $indexer->load($indexerId); |
| 132 | + /** @var \Magento\Indexer\Model\Indexer\State $state */ |
| 133 | + $state = $indexer->getState(); |
| 134 | + $state->setStatus(StateInterface::STATUS_WORKING); |
| 135 | + $state->save(); |
| 136 | + $state->setStatus(StateInterface::STATUS_VALID); |
| 137 | + $state->save(); |
| 138 | + } |
| 139 | + |
| 140 | + $this->sharedIndexesComplete[] = $sharedIndex; |
| 141 | + |
| 142 | + return $this; |
| 143 | + } |
| 144 | + |
89 | 145 | /** |
90 | 146 | * Regenerate indexes for all indexers |
91 | 147 | * |
|
0 commit comments