Skip to content

Commit

Permalink
Merge pull request #482 from adobe-commerce-tier-4/T4-PR-05-23-2024
Browse files Browse the repository at this point in the history
[Support Tier-4 chittima] ACP2E-767: Remove Magento Catalog dependency on Magento Inventory
  • Loading branch information
okolesnyk authored Jun 11, 2024
2 parents 5dd278d + c667e08 commit a032928
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 5 deletions.
103 changes: 103 additions & 0 deletions InventoryCatalog/Model/SortingAdjustment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;

use Magento\Framework\Indexer\Config\Converter\SortingAdjustmentInterface;
use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexer;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockIndexer;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;

class SortingAdjustment implements SortingAdjustmentInterface
{
/**
* @inheritDoc
*/
public function adjust(array $indexersList) : array
{
$indexersListAdjusted = $indexersList;

$order = array_keys($indexersListAdjusted);
$inventoryPos = array_search(InventoryIndexer::INDEXER_ID, $order);
$stockPos = array_search(StockIndexer::INDEXER_ID, $order);
if ($stockPos !== false && $inventoryPos !== false) {
foreach ($indexersListAdjusted as $id => $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;
}
}
67 changes: 67 additions & 0 deletions InventoryCatalog/Test/Integration/IndexersOrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Test\Integration;

use Magento\Framework\Indexer\Config\Converter\SortingAdjustmentInterface;
use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexer;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as StockIndexer;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;

class IndexersOrderTest extends TestCase
{
/**
* @var SortingAdjustmentInterface
*/
private $sortingAdjustment;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
1 change: 1 addition & 0 deletions InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,5 @@
<argument name="sourceItemsSave" xsi:type="object">Magento\Inventory\Model\SourceItem\Command\SourceItemsSaveWithoutLegacySynchronization</argument>
</arguments>
</type>
<preference for="Magento\Framework\Indexer\Config\Converter\SortingAdjustmentInterface" type="Magento\InventoryCatalog\Model\SortingAdjustment" />
</config>
44 changes: 44 additions & 0 deletions InventoryIndexer/Test/Integration/NoPriceIndexerDependencyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\InventoryIndexer\Test\Integration;

use Magento\Framework\Indexer\Config\DependencyInfoProviderInterface;
use Magento\Catalog\Model\Indexer\Product\Price\Processor as PriceIndexer;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class NoPriceIndexerDependencyTest extends TestCase
{
/**
* @var DependencyInfoProviderInterface
*/
private $dependencyInfoProvider;

protected function setUp(): void
{
$this->dependencyInfoProvider = Bootstrap::getObjectManager()->get(DependencyInfoProviderInterface::class);
}

public function testPriceDependency()
{
$output = $this->dependencyInfoProvider->getIndexerIdsToRunBefore(PriceIndexer::INDEXER_ID);
$this->assertArrayNotHasKey(InventoryIndexer::INDEXER_ID, array_values($output));
}
}
5 changes: 0 additions & 5 deletions InventoryIndexer/etc/indexer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@
<title translate="true">Inventory</title>
<description translate="true">Inventory index (MSI)</description>
</indexer>
<indexer id="catalog_product_price">
<dependencies>
<indexer id="inventory"/>
</dependencies>
</indexer>
</config>

0 comments on commit a032928

Please sign in to comment.