diff --git a/InventoryCatalog/Model/SortingAdjustment.php b/InventoryCatalog/Model/SortingAdjustment.php new file mode 100644 index 00000000000..0a4894433ec --- /dev/null +++ b/InventoryCatalog/Model/SortingAdjustment.php @@ -0,0 +1,103 @@ + $data) { + if ($id === StockIndexer::INDEXER_ID) { + $indexersListAdjusted = [$id => $data] + $indexersListAdjusted; + break; + } + } + } + + $order = array_keys($indexersListAdjusted); + $pricePos = array_search(PriceIndexer::INDEXER_ID, $order); + $inventoryPos = array_search(InventoryIndexer::INDEXER_ID, $order); + if ($pricePos !== false && $inventoryPos !== false) { + $indexersListAdjusted = $this->switchPositions($indexersListAdjusted, $inventoryPos, $pricePos); + } + + return $indexersListAdjusted; + } + + /** + * Switch position for two indexers if necessary + * + * @param array $list + * @param int $posShouldBeUpper + * @param int $posShouldBeLower + * @return array + */ + private function switchPositions(array $list, int $posShouldBeUpper, int $posShouldBeLower) : array + { + if ($posShouldBeUpper > $posShouldBeLower) { + $newOrder = $this->reArrange($list, $posShouldBeUpper, $posShouldBeLower); + $tmpList = []; + $c = count($newOrder); + for ($i = 0; $i < $c; $i++) { + $tmpList[$newOrder[$i]] = $list[$newOrder[$i]]; + } + $list = $tmpList; + } + return $list; + } + + /** + * Perform adjustments in the sorting order + * + * @param array $list + * @param int $posShouldBeUpper + * @param int $posShouldBeLower + * @return array + */ + private function reArrange(array $list, int $posShouldBeUpper, int $posShouldBeLower) : array + { + $newOrder = []; + $order = array_keys($list); + foreach ($order as $pos => $indexerId) { + if ($pos < $posShouldBeLower || $pos > $posShouldBeUpper) { + $newOrder[$pos] = $indexerId; + } elseif ($pos === $posShouldBeLower) { + $newOrder[$pos] = $order[$posShouldBeUpper]; + $newOrder[$pos+1] = $indexerId; + } elseif ($pos > $posShouldBeLower && $pos < $posShouldBeUpper) { + $newOrder[$pos+1] = $indexerId; + } + } + return $newOrder; + } +} diff --git a/InventoryCatalog/Test/Integration/IndexersOrderTest.php b/InventoryCatalog/Test/Integration/IndexersOrderTest.php new file mode 100644 index 00000000000..001ac0b5aa1 --- /dev/null +++ b/InventoryCatalog/Test/Integration/IndexersOrderTest.php @@ -0,0 +1,67 @@ +sortingAdjustment = Bootstrap::getObjectManager()->create(SortingAdjustmentInterface::class); + } + + /** + * @return void + */ + public function testIndexersOrder() + { + $unAdjusted = [ + 'indexer1' => [], + PriceIndexer::INDEXER_ID => [], + 'indexer2' => [], + InventoryIndexer::INDEXER_ID => [], + 'indexer3' => [], + StockIndexer::INDEXER_ID => [], + 'indexer4' => [] + ]; + $output = $this->sortingAdjustment->adjust($unAdjusted); + $this->assertArrayHasKey(PriceIndexer::INDEXER_ID, $output); + $this->assertArrayHasKey(InventoryIndexer::INDEXER_ID, $output); + $this->assertArrayHasKey(StockIndexer::INDEXER_ID, $output); + $order = array_keys($output); + $inventoryPos = array_search(InventoryIndexer::INDEXER_ID, $order); + $stockPos = array_search(StockIndexer::INDEXER_ID, $order); + $pricePos = array_search(PriceIndexer::INDEXER_ID, $order); + $this->assertTrue($stockPos < $inventoryPos); + $this->assertTrue($inventoryPos < $pricePos); + } +} diff --git a/InventoryCatalog/etc/di.xml b/InventoryCatalog/etc/di.xml index 863ce424eb9..016871a1150 100644 --- a/InventoryCatalog/etc/di.xml +++ b/InventoryCatalog/etc/di.xml @@ -222,4 +222,5 @@ Magento\Inventory\Model\SourceItem\Command\SourceItemsSaveWithoutLegacySynchronization + diff --git a/InventoryIndexer/Test/Integration/NoPriceIndexerDependencyTest.php b/InventoryIndexer/Test/Integration/NoPriceIndexerDependencyTest.php new file mode 100644 index 00000000000..5d9727c48ba --- /dev/null +++ b/InventoryIndexer/Test/Integration/NoPriceIndexerDependencyTest.php @@ -0,0 +1,44 @@ +dependencyInfoProvider = Bootstrap::getObjectManager()->get(DependencyInfoProviderInterface::class); + } + + public function testPriceDependency() + { + $output = $this->dependencyInfoProvider->getIndexerIdsToRunBefore(PriceIndexer::INDEXER_ID); + $this->assertArrayNotHasKey(InventoryIndexer::INDEXER_ID, array_values($output)); + } +} diff --git a/InventoryIndexer/etc/indexer.xml b/InventoryIndexer/etc/indexer.xml index cced728ea5d..c269fca7c98 100644 --- a/InventoryIndexer/etc/indexer.xml +++ b/InventoryIndexer/etc/indexer.xml @@ -10,9 +10,4 @@ Inventory Inventory index (MSI) - - - - -