-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from adobe-commerce-tier-4/T4-PR-05-23-2024
[Support Tier-4 chittima] ACP2E-767: Remove Magento Catalog dependency on Magento Inventory
- Loading branch information
Showing
5 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
InventoryIndexer/Test/Integration/NoPriceIndexerDependencyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters