Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReservationBuilder.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 use Magento\Framework\Validation\ValidationResultFactory;
16 
21 {
25  private $stockId;
26 
30  private $sku;
31 
35  private $quantity;
36 
40  private $metadata;
41 
45  private $objectManager;
46 
50  private $snakeToCamelCaseConverter;
51 
55  private $validationResultFactory;
56 
62  public function __construct(
63  ObjectManagerInterface $objectManager,
64  SnakeToCamelCaseConverter $snakeToCamelCaseConverter,
65  ValidationResultFactory $validationResultFactory
66  ) {
67  $this->objectManager = $objectManager;
68  $this->snakeToCamelCaseConverter = $snakeToCamelCaseConverter;
69  $this->validationResultFactory = $validationResultFactory;
70  }
71 
75  public function setStockId(int $stockId): ReservationBuilderInterface
76  {
77  $this->stockId = $stockId;
78  return $this;
79  }
80 
84  public function setSku(string $sku): ReservationBuilderInterface
85  {
86  $this->sku = $sku;
87  return $this;
88  }
89 
93  public function setQuantity(float $quantity): ReservationBuilderInterface
94  {
95  $this->quantity = $quantity;
96  return $this;
97  }
98 
102  public function setMetadata(string $metadata = null): ReservationBuilderInterface
103  {
104  $this->metadata = $metadata;
105  return $this;
106  }
107 
111  public function build(): ReservationInterface
112  {
114  $validationResult = $this->validate();
115  if (!$validationResult->isValid()) {
116  throw new ValidationException(__('Validation error'), null, 0, $validationResult);
117  }
118 
119  $data = [
121  ReservationInterface::STOCK_ID => $this->stockId,
122  ReservationInterface::SKU => $this->sku,
123  ReservationInterface::QUANTITY => $this->quantity,
124  ReservationInterface::METADATA => $this->metadata,
125  ];
126 
127  $arguments = $this->convertArrayKeysFromSnakeToCamelCase($data);
128  $reservation = $this->objectManager->create(ReservationInterface::class, $arguments);
129 
130  $this->reset();
131 
132  return $reservation;
133  }
134 
138  private function validate()
139  {
140  $errors = [];
141 
142  if (null === $this->stockId) {
143  $errors[] = __('"%field" is expected to be a number.', ['field' => ReservationInterface::STOCK_ID]);
144  }
145 
146  if (null === $this->sku || '' === trim($this->sku)) {
147  $errors[] = __('"%field" can not be empty.', ['field' => ReservationInterface::SKU]);
148  }
149 
150  if (null === $this->quantity) {
151  $errors[] = __('"%field" can not be null.', ['field' => ReservationInterface::QUANTITY]);
152  }
153 
154  return $this->validationResultFactory->create(['errors' => $errors]);
155  }
156 
161  private function reset()
162  {
163  $this->stockId = null;
164  $this->sku = null;
165  $this->quantity = null;
166  $this->metadata = null;
167  }
168 
176  private function convertArrayKeysFromSnakeToCamelCase(array $array): array
177  {
178  $convertedArrayKeys = $this->snakeToCamelCaseConverter->convert(array_keys($array));
179  return array_combine($convertedArrayKeys, array_values($array));
180  }
181 }
$objectManager
Definition: bootstrap.php:17
__()
Definition: __.php:13
$arguments
$errors
Definition: overview.phtml:9
__construct(ObjectManagerInterface $objectManager, SnakeToCamelCaseConverter $snakeToCamelCaseConverter, ValidationResultFactory $validationResultFactory)