Skip to content

Commit

Permalink
Merge pull request magento#1157 from magento-engcom/1132-Refactor-Get…
Browse files Browse the repository at this point in the history
…ProductIdsBySourceItemIds-class-use-nventoryApi

Removed InventoryCache dependency on Inventory
  • Loading branch information
maghamed authored May 21, 2018
2 parents 26ee672 + ab39b56 commit 993d789
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 18 deletions.
13 changes: 10 additions & 3 deletions app/code/Magento/InventoryCache/Model/FlushCacheByProductIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\InventoryCache\Model;

use Magento\Catalog\Model\Product;
use Magento\Framework\EntityManager\EventManager;
use Magento\Framework\Indexer\CacheContext;

Expand All @@ -26,16 +25,24 @@ class FlushCacheByProductIds
*/
private $eventManager;

/**
* @var string
*/
private $productCacheTag;

/**
* @param CacheContext $cacheContext
* @param EventManager $eventManager
* @param string $productCacheTag
*/
public function __construct(
CacheContext $cacheContext,
EventManager $eventManager
EventManager $eventManager,
string $productCacheTag
) {
$this->cacheContext = $cacheContext;
$this->eventManager = $eventManager;
$this->productCacheTag = $productCacheTag;
}

/**
Expand All @@ -47,7 +54,7 @@ public function __construct(
public function execute(array $productIds)
{
if ($productIds) {
$this->cacheContext->registerEntities(Product::CACHE_TAG, $productIds);
$this->cacheContext->registerEntities($this->productCacheTag, $productIds);
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\InventoryCache\Model\ResourceModel;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
Expand Down Expand Up @@ -44,25 +43,41 @@ class GetProductIdsByStockIds
*/
private $indexStructure;

/**
* @var string
*/
private $productSkuColumn;

/**
* @var string
*/
private $productInterfaceClassName;

/**
* @param ResourceConnection $resource
* @param MetadataPool $metadataPool
* @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
* @param DefaultStockProviderInterface $defaultStockProvider
* @param IndexStructure $indexStructure
* @param string $productSkuColumn
* @param string $productInterfaceClassName
*/
public function __construct(
ResourceConnection $resource,
MetadataPool $metadataPool,
StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
DefaultStockProviderInterface $defaultStockProvider,
IndexStructure $indexStructure
IndexStructure $indexStructure,
string $productSkuColumn,
string $productInterfaceClassName
) {
$this->resource = $resource;
$this->metadataPool = $metadataPool;
$this->defaultStockProvider = $defaultStockProvider;
$this->indexStructure = $indexStructure;
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
$this->productSkuColumn = $productSkuColumn;
$this->productInterfaceClassName = $productInterfaceClassName;
}

/**
Expand All @@ -78,14 +93,14 @@ public function execute(array $stockIds): array
continue;
}
$stockIndexTableName = $this->stockIndexTableNameResolver->execute($stockId);
$entityMetadata = $this->metadataPool->getMetadata(ProductInterface::class);
$entityMetadata = $this->metadataPool->getMetadata($this->productInterfaceClassName);
$linkField = $entityMetadata->getLinkField();
$connection = $this->resource->getConnection();
$sql = $connection->select()
->from(['main' => $stockIndexTableName], [])
->join(
['product' => $this->resource->getTableName('catalog_product_entity')],
'product.' . ProductInterface::SKU . '=main.' . ProductInterface::SKU,
'product.' . $this->productSkuColumn . '=main.' . $this->productSkuColumn,
[$linkField]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Magento\InventoryCache\Plugin\InventoryIndexer\Indexer\Source\SourceItemIndexer;

use Magento\InventoryCache\Model\FlushCacheByProductIds;
use Magento\InventoryCache\Model\ResourceModel\GetProductIdsBySourceItemIds;
use Magento\InventoryIndexer\Model\ResourceModel\GetProductIdsBySourceItemIds;
use Magento\InventoryIndexer\Indexer\SourceItem\SourceItemIndexer;

/**
Expand Down
3 changes: 1 addition & 2 deletions app/code/Magento/InventoryCache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-inventory-catalog-api": "*",
"magento/module-inventory-indexer": "*"
},
"suggest": {
"magento/module-inventory": "*"
"magento/module-catalog": "*"
},
"type": "magento2-module",
"license": [
Expand Down
11 changes: 8 additions & 3 deletions app/code/Magento/InventoryCache/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
<type name="Magento\InventoryIndexer\Indexer\SourceItem\SourceItemIndexer">
<plugin name="invalidate_products_cache" type="Magento\InventoryCache\Plugin\InventoryIndexer\Indexer\Source\SourceItemIndexer\CacheFlush"/>
</type>
<type name="Magento\InventoryCache\Model\ResourceModel\GetProductIdsBySourceItemIds">
<type name="Magento\InventoryCache\Model\ResourceModel\GetProductIdsByStockIds">
<arguments>
<argument name="tableNameSourceItem" xsi:type="const">Magento\Inventory\Model\ResourceModel\SourceItem::TABLE_NAME_SOURCE_ITEM</argument>
<argument name="sourceItemIdFieldName" xsi:type="const">Magento\Inventory\Model\ResourceModel\SourceItem::ID_FIELD_NAME</argument>
<argument name="productSkuColumn" xsi:type="const">Magento\Catalog\Api\Data\ProductInterface::SKU</argument>
<argument name="productInterfaceClassName" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</argument>
</arguments>
</type>
<type name="Magento\InventoryCache\Model\FlushCacheByProductIds">
<arguments>
<argument name="productCacheTag" xsi:type="const">Magento\Catalog\Model\Product::CACHE_TAG</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
*/
declare(strict_types=1);

namespace Magento\InventoryCache\Model\ResourceModel;
namespace Magento\InventoryIndexer\Model\ResourceModel;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;

Expand Down Expand Up @@ -36,24 +35,32 @@ class GetProductIdsBySourceItemIds
*/
private $sourceItemIdFieldName;

/**
* @var string
*/
private $productInterfaceClassName;

/**
* GetProductIdsByStockIds constructor.
*
* @param ResourceConnection $resource
* @param MetadataPool $metadataPool
* @param string $tableNameSourceItem
* @param string $sourceItemIdFieldName
* @param string $productInterfaceClassName
*/
public function __construct(
ResourceConnection $resource,
MetadataPool $metadataPool,
$tableNameSourceItem,
$sourceItemIdFieldName
string $tableNameSourceItem,
string $sourceItemIdFieldName,
string $productInterfaceClassName
) {
$this->resource = $resource;
$this->metadataPool = $metadataPool;
$this->tableNameSourceItem = $tableNameSourceItem;
$this->sourceItemIdFieldName = $sourceItemIdFieldName;
$this->productInterfaceClassName = $productInterfaceClassName;
}

/**
Expand All @@ -65,7 +72,7 @@ public function __construct(
*/
public function execute(array $sourceItemIds): array
{
$productLinkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productLinkField = $this->metadataPool->getMetadata($this->productInterfaceClassName)->getLinkField();
$connection = $this->resource->getConnection();
$sourceItemTable = $this->resource->getTableName($this->tableNameSourceItem);
$select = $connection->select()
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/InventoryIndexer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"magento/module-inventory-sales-api": "*",
"magento/module-inventory-sales": "*"
},
"suggest": {
"magento/module-catalog": "*"
},
"type": "magento2-module",
"license": [
"OSL-3.0",
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/InventoryIndexer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@
<argument name="dimensionName" xsi:type="string">stock_</argument>
</arguments>
</type>
<type name="Magento\InventoryIndexer\Model\ResourceModel\GetProductIdsBySourceItemIds">
<arguments>
<argument name="tableNameSourceItem" xsi:type="const">Magento\Inventory\Model\ResourceModel\SourceItem::TABLE_NAME_SOURCE_ITEM</argument>
<argument name="sourceItemIdFieldName" xsi:type="const">Magento\Inventory\Model\ResourceModel\SourceItem::ID_FIELD_NAME</argument>
<argument name="productInterfaceClassName" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</argument>
</arguments>
</type>
</config>

0 comments on commit 993d789

Please sign in to comment.