Skip to content

Commit a77be45

Browse files
author
Valeriy Nayda
authored
Merge pull request #64 from magento-engcom/msi-reservation
Introduce Reservation Api
2 parents 7388bae + dcba6ad commit a77be45

18 files changed

+1084
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Inventory\Model;
7+
8+
use Magento\InventoryApi\Api\Data\ReservationInterface;
9+
10+
/**
11+
* {@inheritdoc}
12+
*
13+
* @codeCoverageIgnore
14+
*/
15+
class Reservation implements ReservationInterface
16+
{
17+
/**
18+
* @var int|null
19+
*/
20+
private $reservationId;
21+
22+
/**
23+
* @var int
24+
*/
25+
private $stockId;
26+
27+
/**
28+
* @var string
29+
*/
30+
private $sku;
31+
32+
/**
33+
* @var float
34+
*/
35+
private $quantity;
36+
37+
/**
38+
* @var string|null
39+
*/
40+
private $metadata;
41+
42+
public function __construct(
43+
$reservationId,
44+
int $stockId,
45+
string $sku,
46+
float $quantity,
47+
$metadata = null
48+
) {
49+
$this->reservationId = $reservationId;
50+
$this->stockId = $stockId;
51+
$this->sku = $sku;
52+
$this->quantity = $quantity;
53+
$this->metadata = $metadata;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function getReservationId()
60+
{
61+
return $this->reservationId;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function getStockId(): int
68+
{
69+
return $this->stockId;
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
public function getSku(): string
76+
{
77+
return $this->sku;
78+
}
79+
80+
/**
81+
* @inheritdoc
82+
*/
83+
public function getQuantity(): float
84+
{
85+
return $this->quantity;
86+
}
87+
88+
/**
89+
* @inheritdoc
90+
*/
91+
public function getMetadata()
92+
{
93+
return $this->metadata;
94+
}
95+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Inventory\Model\Reservation\Command;
7+
8+
use Magento\Framework\Exception\CouldNotSaveException;
9+
use Magento\Framework\Exception\InputException;
10+
use Magento\Inventory\Model\ResourceModel\Reservation\SaveMultiple;
11+
use Magento\InventoryApi\Api\Data\ReservationInterface;
12+
use Magento\InventoryApi\Api\ReservationsAppendInterface;
13+
use Psr\Log\LoggerInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class ReservationsAppend implements ReservationsAppendInterface
19+
{
20+
/**
21+
* @var SaveMultiple
22+
*/
23+
private $saveMultiple;
24+
25+
/**
26+
* @var LoggerInterface
27+
*/
28+
private $logger;
29+
30+
/**
31+
* @param SaveMultiple $saveMultiple
32+
* @param LoggerInterface $logger
33+
*/
34+
public function __construct(
35+
SaveMultiple $saveMultiple,
36+
LoggerInterface $logger
37+
) {
38+
$this->saveMultiple = $saveMultiple;
39+
$this->logger = $logger;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function execute(array $reservations)
46+
{
47+
/** @var ReservationInterface $reservation */
48+
foreach ($reservations as $reservation) {
49+
if (null !== $reservation->getReservationId()) {
50+
$message = __(
51+
'Cannot update Reservation %reservation',
52+
['reservation' => $reservation->getReservationId()]
53+
);
54+
$this->logger->error($message);
55+
throw new InputException($message);
56+
}
57+
}
58+
try {
59+
$this->saveMultiple->execute($reservations);
60+
} catch (\Exception $e) {
61+
$this->logger->error($e->getMessage());
62+
throw new CouldNotSaveException(__('Could not append Reservation'), $e);
63+
}
64+
}
65+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Inventory\Model\Reservation;
7+
8+
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Framework\Validation\ValidationException;
10+
use Magento\Inventory\Model\Reservation\Validator\ReservationValidatorInterface;
11+
use Magento\Inventory\Model\SnakeToCamelCaseConverter;
12+
use Magento\InventoryApi\Api\Data\ReservationInterface;
13+
use Magento\InventoryApi\Api\ReservationBuilderInterface;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class ReservationBuilder implements ReservationBuilderInterface
19+
{
20+
/**
21+
* @var int
22+
*/
23+
private $stockId;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $sku;
29+
30+
/**
31+
* @var float
32+
*/
33+
private $quantity;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $metadata;
39+
40+
/**
41+
* @var ObjectManagerInterface
42+
*/
43+
private $objectManager;
44+
45+
/**
46+
* @var ReservationValidatorInterface
47+
*/
48+
private $reservationValidator;
49+
50+
/**
51+
* @var SnakeToCamelCaseConverter
52+
*/
53+
private $snakeToCamelCaseConverter;
54+
55+
/**
56+
* @param ObjectManagerInterface $objectManager
57+
* @param ReservationValidatorInterface $reservationValidator
58+
* @param SnakeToCamelCaseConverter $snakeToCamelCaseConverter
59+
*/
60+
public function __construct(
61+
ObjectManagerInterface $objectManager,
62+
ReservationValidatorInterface $reservationValidator,
63+
SnakeToCamelCaseConverter $snakeToCamelCaseConverter
64+
) {
65+
$this->objectManager = $objectManager;
66+
$this->reservationValidator = $reservationValidator;
67+
$this->snakeToCamelCaseConverter = $snakeToCamelCaseConverter;
68+
}
69+
70+
/**
71+
* @inheritdoc
72+
*/
73+
public function setStockId(int $stockId): ReservationBuilderInterface
74+
{
75+
$this->stockId = $stockId;
76+
return $this;
77+
}
78+
79+
/**
80+
* @inheritdoc
81+
*/
82+
public function setSku(string $sku): ReservationBuilderInterface
83+
{
84+
$this->sku = $sku;
85+
return $this;
86+
}
87+
88+
/**
89+
* @inheritdoc
90+
*/
91+
public function setQuantity(float $quantity): ReservationBuilderInterface
92+
{
93+
$this->quantity = $quantity;
94+
return $this;
95+
}
96+
97+
/**
98+
* @inheritdoc
99+
*/
100+
public function setMetadata(string $metadata = null): ReservationBuilderInterface
101+
{
102+
$this->metadata = $metadata;
103+
return $this;
104+
}
105+
106+
/**
107+
* @inheritdoc
108+
*/
109+
public function build(): ReservationInterface
110+
{
111+
$data = [
112+
ReservationInterface::STOCK_ID => $this->stockId,
113+
ReservationInterface::SKU => $this->sku,
114+
ReservationInterface::QUANTITY => $this->quantity,
115+
ReservationInterface::METADATA => $this->metadata,
116+
];
117+
118+
$arguments = $this->convertArrayKeysFromSnakeToCamelCase($data);
119+
$reservation = $this->objectManager->create(ReservationInterface::class, $arguments);
120+
$this->reset();
121+
122+
$validationResult = $this->reservationValidator->validate($reservation);
123+
if (!$validationResult->isValid()) {
124+
throw new ValidationException($validationResult);
125+
}
126+
return $reservation;
127+
}
128+
129+
/**
130+
* Used to clean state after object creation.
131+
*/
132+
private function reset()
133+
{
134+
$this->stockId = null;
135+
$this->sku = null;
136+
$this->quantity = null;
137+
$this->metadata = null;
138+
}
139+
140+
/**
141+
* Used to convert database field names (that use snake case) into constructor parameter names (that use camel case)
142+
* to avoid to define them twice in domain model interface.
143+
*
144+
* @param array $array
145+
* @return array
146+
*/
147+
private function convertArrayKeysFromSnakeToCamelCase(array $array)
148+
{
149+
$convertedArrayKeys = $this->snakeToCamelCaseConverter->convert(array_keys($array));
150+
return array_combine($convertedArrayKeys, array_values($array));
151+
}
152+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Inventory\Model\Reservation\Validator;
7+
8+
use Magento\Framework\Validation\ValidationResult;
9+
use Magento\Framework\Validation\ValidationResultFactory;
10+
use Magento\InventoryApi\Api\Data\ReservationInterface;
11+
12+
/**
13+
* Check that stock ID is valid
14+
*/
15+
class QuantityValidator implements ReservationValidatorInterface
16+
{
17+
/**
18+
* @var ValidationResultFactory
19+
*/
20+
private $validationResultFactory;
21+
22+
/**
23+
* @param ValidationResultFactory $validationResultFactory
24+
*/
25+
public function __construct(ValidationResultFactory $validationResultFactory)
26+
{
27+
$this->validationResultFactory = $validationResultFactory;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function validate(ReservationInterface $reservation): ValidationResult
34+
{
35+
$errors = [];
36+
$value = $reservation->getQuantity();
37+
38+
if (null === $value) {
39+
$errors[] = __('"%field" can not be null.', ['field' => ReservationInterface::QUANTITY]);
40+
}
41+
return $this->validationResultFactory->create(['errors' => $errors]);
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Inventory\Model\Reservation\Validator;
7+
8+
use Magento\Framework\Validation\ValidationResult;
9+
use Magento\InventoryApi\Api\Data\ReservationInterface;
10+
11+
/**
12+
* Extension point for base validation
13+
*
14+
* @api
15+
*/
16+
interface ReservationValidatorInterface
17+
{
18+
/**
19+
* @param ReservationInterface $reservation
20+
* @return ValidationResult
21+
*/
22+
public function validate(ReservationInterface $reservation): ValidationResult;
23+
}

0 commit comments

Comments
 (0)