Skip to content

Commit

Permalink
Revert "Inventory performance fix."
Browse files Browse the repository at this point in the history
This reverts commit a6c9d2c.
  • Loading branch information
p-bystritsky committed Sep 1, 2023
1 parent 8c6155d commit 9a659c0
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions Model/Quote/GetQuoteInventoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
use Bold\Checkout\Api\Data\Quote\Inventory\Result\InventoryDataInterfaceFactory;
use Bold\Checkout\Api\Data\Quote\Inventory\ResultInterface;
use Bold\Checkout\Api\Data\Quote\Inventory\ResultInterfaceFactory;
use Magento\Bundle\Model\Product\Type as Bundle;
use Bold\Checkout\Api\Quote\GetQuoteInventoryDataInterface;
use Bold\Checkout\Model\Http\Client\Request\Validator\ShopIdValidator;
use Exception;
use Magento\Bundle\Model\Product\Type as Bundle;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Module\Manager;
use Magento\Framework\ObjectManagerInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface;
use Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterfaceFactory;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -63,11 +58,6 @@ class GetQuoteInventoryData implements GetQuoteInventoryDataInterface
*/
private $storeManager;

/**
* @var Manager
*/
private $moduleManager;

/**
* @param CartRepositoryInterface $cartRepository
* @param ShopIdValidator $shopIdValidator
Expand All @@ -76,7 +66,6 @@ class GetQuoteInventoryData implements GetQuoteInventoryDataInterface
* @param ErrorInterfaceFactory $errorFactory
* @param InventoryDataInterfaceFactory $inventoryDataFactory
* @param StoreManagerInterface $storeManager
* @param Manager $moduleManager
*/
public function __construct(
CartRepositoryInterface $cartRepository,
Expand All @@ -85,8 +74,7 @@ public function __construct(
ResultInterfaceFactory $resultFactory,
ErrorInterfaceFactory $errorFactory,
InventoryDataInterfaceFactory $inventoryDataFactory,
StoreManagerInterface $storeManager,
Manager $moduleManager
StoreManagerInterface $storeManager
) {
$this->cartRepository = $cartRepository;
$this->shopIdValidator = $shopIdValidator;
Expand All @@ -95,7 +83,6 @@ public function __construct(
$this->inventoryDataFactory = $inventoryDataFactory;
$this->objectManager = $objectManager;
$this->storeManager = $storeManager;
$this->moduleManager = $moduleManager;
}

/**
Expand Down Expand Up @@ -171,9 +158,6 @@ private function isProductSalable(CartItemInterface $item): bool
}

try {
if (!$this->moduleManager->isEnabled('Magento_InventorySalesApi')) {
return (bool)$item->getProduct()->isSalable();
}
$stockResolver = $this->getStockResolverService();
$productSalableForRequestedQtyService = $this->getProductSalableForRequestedQtyService();
$sku = $item['product']['sku'];
Expand All @@ -182,6 +166,9 @@ private function isProductSalable(CartItemInterface $item): bool
(string)$sku,
$requestedQty
);
if (!$request || !$stockResolver || !$productSalableForRequestedQtyService) {
return (bool)$item->getProduct()->isSalable();
}
$websiteId = (int)$this->storeManager->getStore($item->getStoreId())->getWebsiteId();
$websiteCode = $this->storeManager->getWebsite($websiteId)->getCode();
$stockId = $stockResolver->execute('website', $websiteCode)->getStockId();
Expand All @@ -195,12 +182,14 @@ private function isProductSalable(CartItemInterface $item): bool
/**
* Try to build \Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface. If it's not possible, return null.
*
* @return AreProductsSalableForRequestedQtyInterface|null
* @return \Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface|null
*/
private function getProductSalableForRequestedQtyService(): ?AreProductsSalableForRequestedQtyInterface
private function getProductSalableForRequestedQtyService(): ?\Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface
{
try {
$areProductsSalable = $this->objectManager->get(AreProductsSalableForRequestedQtyInterface::class);
$areProductsSalable = $this->objectManager->get(
\Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface::class
);
} catch (Throwable $e) {
$areProductsSalable = null;
}
Expand All @@ -210,12 +199,12 @@ private function getProductSalableForRequestedQtyService(): ?AreProductsSalableF
/**
* Try to build \Magento\InventorySalesApi\Api\StockResolverInterface. If it's not possible, return null.
*
* @return StockResolverInterface|null
* @return \Magento\InventorySalesApi\Api\StockResolverInterface|null
*/
private function getStockResolverService(): ?StockResolverInterface
private function getStockResolverService(): ?\Magento\InventorySalesApi\Api\StockResolverInterface
{
try {
$stockResolver = $this->objectManager->get(StockResolverInterface::class);
$stockResolver = $this->objectManager->get(\Magento\InventorySalesApi\Api\StockResolverInterface::class);
} catch (Throwable $e) {
$stockResolver = null;
}
Expand All @@ -228,14 +217,16 @@ private function getStockResolverService(): ?StockResolverInterface
*
* @param string $sku
* @param float $qty
* @return IsProductSalableForRequestedQtyRequestInterface|null
* @return \Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface|null
*/
private function getRequest(
string $sku,
float $qty
): ?IsProductSalableForRequestedQtyRequestInterface {
): ?\Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterface {
try {
$requestFactory = $this->objectManager->get(IsProductSalableForRequestedQtyRequestInterfaceFactory::class);
$requestFactory = $this->objectManager->get(
\Magento\InventorySalesApi\Api\Data\IsProductSalableForRequestedQtyRequestInterfaceFactory::class
);
$request = $requestFactory->create(
[
'sku' => $sku,
Expand Down

0 comments on commit 9a659c0

Please sign in to comment.