Skip to content

Commit

Permalink
MDVA-27939: Backport inventor patches down to 2.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
robertorubino committed May 21, 2020
1 parent 26a9fe9 commit 6c9c7b8
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Model\Source\Command;

use Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface;

/**
* @inheritdoc
*/
class GetSourcesAssignedToStockOrderedByPriorityCache implements GetSourcesAssignedToStockOrderedByPriorityInterface
{
/**
* @var GetSourcesAssignedToStockOrderedByPriority
*/
private $getSourcesAssignedToStock;

/**
* @var array
*/
private $sourcesAssignedToStock = [];

/**
* @param GetSourcesAssignedToStockOrderedByPriority $getSourcesAssignedToStockOrderedByPriority
*/
public function __construct(
GetSourcesAssignedToStockOrderedByPriority $getSourcesAssignedToStockOrderedByPriority
) {
$this->getSourcesAssignedToStock = $getSourcesAssignedToStockOrderedByPriority;
}

/**
* @inheritdoc
*/
public function execute(int $stockId): array
{
if (!isset($this->sourcesAssignedToStock[$stockId])) {
$this->sourcesAssignedToStock[$stockId] = $this->getSourcesAssignedToStock->execute($stockId);
}

return $this->sourcesAssignedToStock[$stockId];
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<preference for="Magento\InventoryApi\Api\Data\SourceInterface" type="Magento\Inventory\Model\Source"/>
<preference for="Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface" type="Magento\Inventory\Model\SourceCarrierLink"/>
<preference for="Magento\InventoryApi\Api\Data\SourceSearchResultsInterface" type="Magento\Inventory\Model\SourceSearchResults"/>
<preference for="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" type="Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriority"/>
<preference for="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" type="Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriorityCache"/>
<preference for="Magento\InventoryApi\Api\GetSourceItemsBySkuInterface" type="Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku"/>
<preference for="Magento\InventoryApi\Model\GetSourceCodesBySkusInterface" type="Magento\Inventory\Model\GetSourceCodesBySkus"/>
<preference for="Magento\InventoryApi\Model\SourceCarrierLinkManagementInterface" type="Magento\Inventory\Model\SourceCarrierLinkManagement"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model;

use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
use Magento\Framework\Serialize\Serializer\Json;

/**
* @inheritdoc
*/
class GetProductIdsBySkusCache implements GetProductIdsBySkusInterface
{
/**
* @var GetProductIdsBySkus
*/
private $getProductIdsBySkus;

/**
* @var Json
*/
private $jsonSerializer;

/**
* @var array
*/
private $productIdsBySkus = [];

/**
* @param GetProductIdsBySkus $getProductIdsBySkus
* @param Json $jsonSerializer
*/
public function __construct(
GetProductIdsBySkus $getProductIdsBySkus,
Json $jsonSerializer
) {
$this->getProductIdsBySkus = $getProductIdsBySkus;
$this->jsonSerializer = $jsonSerializer;
}

/**
* @inheritdoc
*/
public function execute(array $skus): array
{
$cacheKey = $this->jsonSerializer->serialize($skus);
if (!isset($this->productIdsBySkus[$cacheKey])) {
$this->productIdsBySkus[$cacheKey] = $this->getProductIdsBySkus->execute($skus);
}

return $this->productIdsBySkus[$cacheKey];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Model\ResourceModel;

use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
use Magento\Framework\Serialize\Serializer\Json;

/**
* @inheritdoc
*/
class GetProductTypesBySkusCache implements GetProductTypesBySkusInterface
{
/**
* @var GetProductTypesBySkus
*/
private $getProductTypesBySkus;

/**
* @var Json
*/
private $jsonSerializer;

/**
* @var array
*/
private $productTypesBySkus = [];

/**
* @param GetProductTypesBySkus $getProductTypesBySkus
* @param Json $jsonSerializer
*/
public function __construct(
GetProductTypesBySkus $getProductTypesBySkus,
Json $jsonSerializer
) {
$this->getProductTypesBySkus = $getProductTypesBySkus;
$this->jsonSerializer = $jsonSerializer;
}

/**
* @inheritdoc
*/
public function execute(array $skus): array
{
$cacheKey = $this->jsonSerializer->serialize($skus);
if (!isset($this->productTypesBySkus[$cacheKey])) {
$this->productTypesBySkus[$cacheKey] = $this->getProductTypesBySkus->execute($skus);
}

return $this->productTypesBySkus[$cacheKey];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\Indexer\ProductPriceIndexFilter;
use Magento\Framework\App\ResourceConnection;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;

Expand Down Expand Up @@ -39,22 +41,30 @@ class ModifySelectInProductPriceIndexFilter
*/
private $stockByWebsiteIdResolver;

/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
* @param StockConfigurationInterface $stockConfiguration
* @param ResourceConnection $resourceConnection
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
* @param DefaultStockProviderInterface $defaultStockProvider
*/
public function __construct(
StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
StockConfigurationInterface $stockConfiguration,
ResourceConnection $resourceConnection,
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
DefaultStockProviderInterface $defaultStockProvider
) {
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
$this->stockConfiguration = $stockConfiguration;
$this->resourceConnection = $resourceConnection;
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
$this->defaultStockProvider = $defaultStockProvider;
}

/**
Expand Down Expand Up @@ -84,7 +94,7 @@ public function aroundModifyPrice(
$select->from(['price_index' => $priceTable->getTableName()], []);
$priceEntityField = $priceTable->getEntityField();

if ($this->resourceConnection->getConnection()->isTableExists($stockTable)) {
if (!$this->isDefaultStock($stock) && $this->resourceConnection->getConnection()->isTableExists($stockTable)) {
$select->joinInner(
['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')],
"product_entity.entity_id = price_index.{$priceEntityField}",
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface" type="Magento\InventoryCatalog\Model\DefaultStockProvider"/>
<preference for="Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface" type="Magento\InventoryCatalog\Model\DefaultSourceProvider"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkus"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface" type="Magento\InventoryCatalog\Model\ResourceModel\GetProductTypesBySkus" />
<preference for="Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkusCache"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface" type="Magento\InventoryCatalog\Model\ResourceModel\GetProductTypesBySkusCache" />
<preference for="Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface" type="Magento\InventoryCatalog\Model\GetSkusByProductIds"/>
<preference for="Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface" type="Magento\InventoryCatalog\Model\IsSingleSourceMode"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryConfiguration\Plugin\InventoryConfiguration\Model;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\InventoryConfiguration\Model\GetLegacyStockItem;

/**
* Caching plugin for GetLegacyStockItem service.
*/
class GetLegacyStockItemCache
{
/**
* @var array
*/
private $legacyStockItemsBySku = [];

/**
* Cache the result of service to avoid duplicate queries to the database.
*
* @param GetLegacyStockItem $subject
* @param callable $proceed
* @param string $sku
* @return StockItemInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(GetLegacyStockItem $subject, callable $proceed, string $sku): StockItemInterface
{
if (!isset($this->legacyStockItemsBySku[$sku])) {
$this->legacyStockItemsBySku[$sku] = $proceed($sku);
}

return $this->legacyStockItemsBySku[$sku];
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/InventoryConfiguration/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
<plugin name="allow_negative_min_qty_in_config"
type="Magento\InventoryConfiguration\Plugin\CatalogInventory\Model\System\Config\Backend\Minqty\AllowNegativeMinQtyInConfigPlugin"/>
</type>
<type name="Magento\InventoryConfiguration\Model\GetLegacyStockItem">
<plugin name="cache_legacy_stock_item"
type="Magento\InventoryConfiguration\Plugin\InventoryConfiguration\Model\GetLegacyStockItemCache"/>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\InventoryIndexer\Indexer\IndexStructure;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;

/**
* @inheritdoc
Expand All @@ -28,46 +30,73 @@ class GetStockItemData implements GetStockItemDataInterface
*/
private $stockIndexTableNameResolver;

/**
* @var DefaultStockProviderInterface
*/
private $defaultStockProvider;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

/**
* @param ResourceConnection $resource
* @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
* @param DefaultStockProviderInterface $defaultStockProvider
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
*/
public function __construct(
ResourceConnection $resource,
StockIndexTableNameResolverInterface $stockIndexTableNameResolver
StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
DefaultStockProviderInterface $defaultStockProvider,
GetProductIdsBySkusInterface $getProductIdsBySkus
) {
$this->resource = $resource;
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
$this->defaultStockProvider = $defaultStockProvider;
$this->getProductIdsBySkus = $getProductIdsBySkus;
}

/**
* @inheritdoc
*/
public function execute(string $sku, int $stockId): ?array
{
$stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId);

$connection = $this->resource->getConnection();
$select = $connection->select()
->from(
$select = $connection->select();

if ($this->defaultStockProvider->getId() === $stockId) {
$productId = current($this->getProductIdsBySkus->execute([$sku]));
$stockItemTableName = $this->resource->getTableName('cataloginventory_stock_status');
$select->from(
$stockItemTableName,
[
GetStockItemDataInterface::QUANTITY => 'qty',
GetStockItemDataInterface::IS_SALABLE => 'stock_status',
]
)->where('product_id = ?', $productId);

return $connection->fetchRow($select) ?: null;
} else {
$stockItemTableName = $this->stockIndexTableNameResolver->execute($stockId);
$select->from(
$stockItemTableName,
[
GetStockItemDataInterface::QUANTITY => IndexStructure::QUANTITY,
GetStockItemDataInterface::IS_SALABLE => IndexStructure::IS_SALABLE,
]
)
->where(IndexStructure::SKU . ' = ?', $sku);
)->where(IndexStructure::SKU . ' = ?', $sku);

try {
if ($connection->isTableExists($stockItemTableName)) {
return $connection->fetchRow($select) ?: null;
}
try {
if ($connection->isTableExists($stockItemTableName)) {
return $connection->fetchRow($select) ?: null;
}

return null;
} catch (\Exception $e) {
throw new LocalizedException(__(
'Could not receive Stock Item data'
), $e);
return null;
} catch (\Exception $e) {
throw new LocalizedException(__('Could not receive Stock Item data'), $e);
}
}
}
}
Loading

0 comments on commit 6c9c7b8

Please sign in to comment.