Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ItemRepository.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Api\Data\ProductOptionExtensionFactory;
11 use Magento\Catalog\Model\ProductOptionFactory;
15 use Magento\Framework\DataObject\Factory as DataObjectFactory;
19 use Magento\Sales\Api\Data\OrderItemSearchResultInterfaceFactory;
22 
28 {
32  protected $objectFactory;
33 
37  protected $metadata;
38 
43 
48 
52  protected $extensionFactory;
53 
57  protected $processorPool;
58 
62  protected $registry = [];
63 
67  private $collectionProcessor;
68 
79  public function __construct(
80  DataObjectFactory $objectFactory,
82  OrderItemSearchResultInterfaceFactory $searchResultFactory,
83  ProductOptionFactory $productOptionFactory,
84  ProductOptionExtensionFactory $extensionFactory,
85  array $processorPool = [],
86  CollectionProcessorInterface $collectionProcessor = null
87  ) {
88  $this->objectFactory = $objectFactory;
89  $this->metadata = $metadata;
90  $this->searchResultFactory = $searchResultFactory;
91  $this->productOptionFactory = $productOptionFactory;
92  $this->extensionFactory = $extensionFactory;
93  $this->processorPool = $processorPool;
94  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
95  }
96 
105  public function get($id)
106  {
107  if (!$id) {
108  throw new InputException(__('An ID is needed. Set the ID and try again.'));
109  }
110  if (!isset($this->registry[$id])) {
112  $orderItem = $this->metadata->getNewInstance()->load($id);
113  if (!$orderItem->getItemId()) {
114  throw new NoSuchEntityException(
115  __("The entity that was requested doesn't exist. Verify the entity and try again.")
116  );
117  }
118 
119  $this->addProductOption($orderItem);
120  $this->addParentItem($orderItem);
121  $this->registry[$id] = $orderItem;
122  }
123  return $this->registry[$id];
124  }
125 
132  public function getList(SearchCriteriaInterface $searchCriteria)
133  {
135  $searchResult = $this->searchResultFactory->create();
136  $searchResult->setSearchCriteria($searchCriteria);
137  $this->collectionProcessor->process($searchCriteria, $searchResult);
139  foreach ($searchResult->getItems() as $orderItem) {
140  $this->addProductOption($orderItem);
141  }
142 
143  return $searchResult;
144  }
145 
152  public function delete(OrderItemInterface $entity)
153  {
154  $this->metadata->getMapper()->delete($entity);
155  unset($this->registry[$entity->getEntityId()]);
156  return true;
157  }
158 
165  public function deleteById($id)
166  {
167  $entity = $this->get($id);
168  return $this->delete($entity);
169  }
170 
177  public function save(OrderItemInterface $entity)
178  {
179  if ($entity->getProductOption()) {
180  $request = $this->getBuyRequest($entity);
181  $entity->setProductOptions(['info_buyRequest' => $request->toArray()]);
182  }
183 
184  $this->metadata->getMapper()->save($entity);
185  $this->registry[$entity->getEntityId()] = $entity;
186  return $this->registry[$entity->getEntityId()];
187  }
188 
195  protected function addProductOption(OrderItemInterface $orderItem)
196  {
198  $request = $orderItem->getBuyRequest();
199 
200  $productType = $orderItem->getProductType();
201  if (isset($this->processorPool[$productType])
202  && !$orderItem->getParentItemId()) {
203  $data = $this->processorPool[$productType]->convertToProductOption($request);
204  if ($data) {
205  $this->setProductOption($orderItem, $data);
206  }
207  }
208 
209  if (isset($this->processorPool['custom_options'])
210  && !$orderItem->getParentItemId()) {
211  $data = $this->processorPool['custom_options']->convertToProductOption($request);
212  if ($data) {
213  $this->setProductOption($orderItem, $data);
214  }
215  }
216 
217  return $this;
218  }
219 
227  private function addParentItem(OrderItemInterface $orderItem)
228  {
229  if ($parentId = $orderItem->getParentItemId()) {
230  $orderItem->setParentItem($this->get($parentId));
231  }
232  }
233 
242  {
243  $productOption = $orderItem->getProductOption();
244  if (!$productOption) {
245  $productOption = $this->productOptionFactory->create();
246  $orderItem->setProductOption($productOption);
247  }
248 
249  $extensionAttributes = $productOption->getExtensionAttributes();
250  if (!$extensionAttributes) {
251  $extensionAttributes = $this->extensionFactory->create();
252  $productOption->setExtensionAttributes($extensionAttributes);
253  }
254 
255  $extensionAttributes->setData(key($data), current($data));
256 
257  return $this;
258  }
259 
267  {
268  $request = $this->objectFactory->create(['qty' => $entity->getQtyOrdered()]);
269 
270  $productType = $entity->getProductType();
271  if (isset($this->processorPool[$productType])
272  && !$entity->getParentItemId()) {
273  $productOption = $entity->getProductOption();
274  if ($productOption) {
275  $requestUpdate = $this->processorPool[$productType]->convertToBuyRequest($productOption);
276  $request->addData($requestUpdate->getData());
277  }
278  }
279 
280  if (isset($this->processorPool['custom_options'])
281  && !$entity->getParentItemId()) {
282  $productOption = $entity->getProductOption();
283  if ($productOption) {
284  $requestUpdate = $this->processorPool['custom_options']->convertToBuyRequest($productOption);
285  $request->addData($requestUpdate->getData());
286  }
287  }
288 
289  return $request;
290  }
291 
298  private function getCollectionProcessor()
299  {
300  if (!$this->collectionProcessor) {
301  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
302  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
303  );
304  }
305  return $this->collectionProcessor;
306  }
307 }
$orderItem
Definition: order.php:30
$id
Definition: fieldset.phtml:14
setProductOption(OrderItemInterface $orderItem, array $data)
__construct(DataObjectFactory $objectFactory, Metadata $metadata, OrderItemSearchResultInterfaceFactory $searchResultFactory, ProductOptionFactory $productOptionFactory, ProductOptionExtensionFactory $extensionFactory, array $processorPool=[], CollectionProcessorInterface $collectionProcessor=null)
__()
Definition: __.php:13
$searchCriteria
$entity
Definition: element.phtml:22
getBuyRequest(OrderItemInterface $entity)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)