Skip to content

Commit fb505fb

Browse files
authored
Merge pull request magento#2913 from nmalevanec/AreProductsSalableForRequestedQty
AreProductsSalableForRequestedQtyInterface Rework
2 parents b31fe2b + b7f13e4 commit fb505fb

12 files changed

+419
-15
lines changed

InventorySales/Model/AreProductsSalableForRequestedQty.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface;
12+
use Magento\InventorySalesApi\Api\Data\IsProductSalableResultInterfaceFactory;
1213
use Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterface;
1314
use Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterfaceFactory;
1415
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
@@ -25,43 +26,61 @@ class AreProductsSalableForRequestedQty implements AreProductsSalableForRequeste
2526
private $isProductSalableForRequestedQtyInterface;
2627

2728
/**
28-
* @var LoggerInterface
29+
* @var IsProductSalableResultInterfaceFactory
2930
*/
30-
private $logger;
31+
private $isProductSalableResultFactory;
3132

3233
/**
3334
* @var ProductsSalableResultInterfaceFactory
3435
*/
3536
private $productsSalableResultFactory;
3637

38+
/**
39+
* @var LoggerInterface
40+
*/
41+
private $logger;
42+
3743
/**
3844
* @param IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQtyInterface
45+
* @param IsProductSalableResultInterfaceFactory $isProductSalableResultFactory
3946
* @param ProductsSalableResultInterfaceFactory $productsSalableResultFactory
4047
* @param LoggerInterface $logger
4148
*/
4249
public function __construct(
4350
IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQtyInterface,
51+
IsProductSalableResultInterfaceFactory $isProductSalableResultFactory,
4452
ProductsSalableResultInterfaceFactory $productsSalableResultFactory,
4553
LoggerInterface $logger
4654
) {
4755
$this->isProductSalableForRequestedQtyInterface = $isProductSalableForRequestedQtyInterface;
48-
$this->logger = $logger;
4956
$this->productsSalableResultFactory = $productsSalableResultFactory;
57+
$this->isProductSalableResultFactory = $isProductSalableResultFactory;
58+
$this->logger = $logger;
5059
}
5160

5261
/**
5362
* @inheritDoc
5463
*/
55-
public function execute(array $skuRequests, int $stockId): ProductsSalableResultInterface
56-
{
64+
public function execute(
65+
array $skuRequests,
66+
int $stockId
67+
): ProductsSalableResultInterface {
5768
$results = [];
58-
foreach ($skuRequests as $sku => $quantity) {
69+
foreach ($skuRequests as $request) {
5970
try {
60-
$results[] = $this->isProductSalableForRequestedQtyInterface->execute(
61-
(string)$sku,
71+
$result = $this->isProductSalableForRequestedQtyInterface->execute(
72+
$request->getSku(),
6273
$stockId,
63-
(float)$quantity
74+
$request->getQty()
75+
);
76+
$result = $this->isProductSalableResultFactory->create(
77+
[
78+
'sku' => $request->getSku(),
79+
'isSalable' => $result->isSalable(),
80+
'errors' => $result->getErrors(),
81+
]
6482
);
83+
$results[] = $result;
6584
} catch (LocalizedException $e) {
6685
$this->logger->error($e->getLogMessage());
6786
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventorySales\Model\AreProductsSalableForRequestedQty;
9+
10+
use Magento\InventorySalesApi\Api\Data\ProductSalableForRequestedQtyInfoExtensionInterface;
11+
use Magento\InventorySalesApi\Api\Data\ProductSalableForRequestedQtyInfoInterface;
12+
13+
/**
14+
* @inheritDoc
15+
*/
16+
class ProductsSalableInfo implements ProductSalableForRequestedQtyInfoInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $sku;
22+
23+
/**
24+
* @var float
25+
*/
26+
private $qty;
27+
28+
/**
29+
* @var ProductSalableForRequestedQtyInfoExtensionInterface|null
30+
*/
31+
private $extensionAttributes;
32+
33+
/**
34+
* @param string $sku
35+
* @param float $qty
36+
* @param ProductSalableForRequestedQtyInfoExtensionInterface|null $extensionAttributes
37+
*/
38+
public function __construct(string $sku, float $qty, $extensionAttributes = null)
39+
{
40+
$this->sku = $sku;
41+
$this->qty = $qty;
42+
$this->extensionAttributes = $extensionAttributes;
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function getSku(): string
49+
{
50+
return $this->sku;
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
public function getQty(): float
57+
{
58+
return $this->qty;
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function setExtensionAttributes(
65+
ProductSalableForRequestedQtyInfoExtensionInterface $extAttributes
66+
): void {
67+
$this->extensionAttributes = $extAttributes;
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
public function getExtensionAttributes(): ?ProductSalableForRequestedQtyInfoExtensionInterface
74+
{
75+
return $this->extensionAttributes;
76+
}
77+
}

InventorySales/Model/CheckItemsQuantity.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\InventorySalesApi\Api\AreProductsSalableForRequestedQtyInterface;
1313
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterface;
14+
use Magento\InventorySalesApi\Api\Data\ProductSalableForRequestedQtyInfoInterfaceFactory;
1415
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
1516

1617
/**
@@ -23,16 +24,26 @@ class CheckItemsQuantity
2324
*/
2425
private $areProductsSalableForRequestedQty;
2526

27+
/**
28+
* @var ProductSalableForRequestedQtyInfoInterfaceFactory
29+
*/
30+
private $salableForRequestedQtyInfoFactory;
31+
2632
/**
2733
* @param IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty @deprecated
34+
* @param ProductSalableForRequestedQtyInfoInterfaceFactory|null $salableForRequestedQtyInfoFactory
2835
* @param AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2937
*/
3038
public function __construct(
3139
IsProductSalableForRequestedQtyInterface $isProductSalableForRequestedQty,
40+
ProductSalableForRequestedQtyInfoInterfaceFactory $salableForRequestedQtyInfoFactory = null,
3241
AreProductsSalableForRequestedQtyInterface $areProductsSalableForRequestedQty = null
3342
) {
3443
$this->areProductsSalableForRequestedQty = $areProductsSalableForRequestedQty ?: ObjectManager::getInstance()
3544
->get(AreProductsSalableForRequestedQtyInterface::class);
45+
$this->salableForRequestedQtyInfoFactory = $salableForRequestedQtyInfoFactory ?: ObjectManager::getInstance()
46+
->get(ProductSalableForRequestedQtyInfoInterfaceFactory::class);
3647
}
3748

3849
/**
@@ -45,7 +56,11 @@ public function __construct(
4556
*/
4657
public function execute(array $items, int $stockId): void
4758
{
48-
$result = $this->areProductsSalableForRequestedQty->execute($items, $stockId);
59+
$skuRequests = [];
60+
foreach ($items as $sku => $qty) {
61+
$skuRequests[] = $this->salableForRequestedQtyInfoFactory->create(['sku' => $sku, 'qty' => $qty]);
62+
}
63+
$result = $this->areProductsSalableForRequestedQty->execute($skuRequests, $stockId);
4964
foreach ($result->getSalable() as $isSalable) {
5065
if (false === $isSalable->isSalable()) {
5166
$errors = $isSalable->getErrors();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventorySales\Model;
9+
10+
use Magento\InventorySalesApi\Api\Data\IsProductSalableResultExtensionInterface;
11+
use Magento\InventorySalesApi\Api\Data\IsProductSalableResultInterface;
12+
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterface;
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
class ProductSalableResult implements IsProductSalableResultInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $sku;
23+
24+
/**
25+
* @var bool
26+
*/
27+
private $isSalable;
28+
29+
/**
30+
* @var IsProductSalableResultExtensionInterface|null
31+
*/
32+
private $extensionAttributes;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $errors;
38+
39+
/**
40+
* @param string $sku
41+
* @param bool $isSalable
42+
* @param ProductSalabilityErrorInterface[] $errors
43+
* @param IsProductSalableResultExtensionInterface|null $extensionAttributes
44+
*/
45+
public function __construct(
46+
string $sku,
47+
bool $isSalable,
48+
array $errors = [],
49+
IsProductSalableResultExtensionInterface $extensionAttributes = null
50+
) {
51+
$this->sku = $sku;
52+
$this->isSalable = $isSalable;
53+
$this->extensionAttributes = $extensionAttributes;
54+
$this->errors = $errors;
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function getSku(): string
61+
{
62+
return $this->sku;
63+
}
64+
65+
/**
66+
* @inheritDoc
67+
*/
68+
public function isSalable(): bool
69+
{
70+
return $this->isSalable;
71+
}
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function getErrors(): array
77+
{
78+
return $this->errors;
79+
}
80+
81+
/**
82+
* @inheritDoc
83+
*/
84+
public function setExtensionAttributes(IsProductSalableResultExtensionInterface $extensionAttributes): void
85+
{
86+
$this->extensionAttributes = $extensionAttributes;
87+
}
88+
89+
/**
90+
* @inheritDoc
91+
*/
92+
public function getExtensionAttributes(): ?IsProductSalableResultExtensionInterface
93+
{
94+
return $this->extensionAttributes;
95+
}
96+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\InventorySales\Model\AreProductsSalableForRequestedQty;
8+
namespace Magento\InventorySales\Model;
99

1010
use Magento\InventorySalesApi\Api\Data\ProductsSalableResultExtensionInterface;
1111
use Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterface;

InventorySales/etc/di.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,7 @@
184184
<plugin name="process_reservations_after_product_save" type="Magento\InventorySales\Plugin\Catalog\Model\ResourceModel\Product\UpdateReservationsPlugin"/>
185185
<plugin name="delete_reservations" type="Magento\InventorySales\Plugin\Catalog\Model\ResourceModel\Product\DeleteReservationsPlugin"/>
186186
</type>
187-
<preference for="Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterface" type="Magento\InventorySales\Model\AreProductsSalableForRequestedQty\ProductsSalableResult"/>
187+
<preference for="Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterface" type="Magento\InventorySales\Model\ProductsSalableResult"/>
188+
<preference for="Magento\InventorySalesApi\Api\Data\IsProductSalableResultInterface" type="Magento\InventorySales\Model\ProductSalableResult"/>
189+
<preference for="Magento\InventorySalesApi\Api\Data\ProductSalableForRequestedQtyInfoInterface" type="Magento\InventorySales\Model\AreProductsSalableForRequestedQty\ProductsSalableInfo"/>
188190
</config>

InventorySalesApi/Api/AreProductsSalableForRequestedQtyInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface AreProductsSalableForRequestedQtyInterface
1919
/**
2020
* Get whether products are salable in requested Qty for given set of SKUs in specified stock.
2121
*
22-
* @param array $skuRequests array('sku' => 'quantity', ..., ...)
22+
* @param \Magento\InventorySalesApi\Api\Data\ProductSalableForRequestedQtyInfoInterface[] $skuRequests
2323
* @param int $stockId
2424
* @return \Magento\InventorySalesApi\Api\Data\ProductsSalableResultInterface
2525
*/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventorySalesApi\Api\Data;
9+
10+
use Magento\Framework\Api\ExtensibleDataInterface;
11+
12+
/**
13+
* Represents "is product salable" result interface.
14+
*
15+
* @api
16+
*/
17+
interface IsProductSalableResultInterface extends ExtensibleDataInterface
18+
{
19+
/**
20+
* Retrieve product sku from result.
21+
*
22+
* @return string
23+
*/
24+
public function getSku(): string;
25+
26+
/**
27+
* Retrieve is salable result.
28+
*
29+
* @return bool
30+
*/
31+
public function isSalable(): bool;
32+
33+
/**
34+
* Retrieve errors from result.
35+
*
36+
* @return \Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterface[]
37+
*/
38+
public function getErrors(): array;
39+
40+
/**
41+
* Set extension attributes to result.
42+
*
43+
* @param \Magento\InventorySalesApi\Api\Data\IsProductSalableResultExtensionInterface $extensionAttributes
44+
* @return void
45+
*/
46+
public function setExtensionAttributes(
47+
IsProductSalableResultExtensionInterface $extensionAttributes
48+
): void;
49+
50+
/**
51+
* Retrieve existing extension attributes object.
52+
*
53+
* @return \Magento\InventorySalesApi\Api\Data\IsProductSalableResultExtensionInterface|null
54+
*/
55+
public function getExtensionAttributes(): ?IsProductSalableResultExtensionInterface;
56+
}

0 commit comments

Comments
 (0)