ItemRepository.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Catalog\Model\ProductOptionProcessorInterface;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. use Magento\Framework\Api\SearchCriteriaInterface;
  10. use Magento\Framework\DataObject;
  11. use Magento\Framework\DataObject\Factory as DataObjectFactory;
  12. use Magento\Framework\Exception\InputException;
  13. use Magento\Framework\Exception\NoSuchEntityException;
  14. use Magento\Sales\Api\Data\OrderItemInterface;
  15. use Magento\Sales\Api\Data\OrderItemSearchResultInterfaceFactory;
  16. use Magento\Sales\Api\OrderItemRepositoryInterface;
  17. use Magento\Sales\Model\Order\ProductOption;
  18. use Magento\Sales\Model\ResourceModel\Metadata;
  19. /**
  20. * Repository class for @see OrderItemInterface
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class ItemRepository implements OrderItemRepositoryInterface
  24. {
  25. /**
  26. * @var DataObjectFactory
  27. */
  28. protected $objectFactory;
  29. /**
  30. * @var Metadata
  31. */
  32. protected $metadata;
  33. /**
  34. * @var OrderItemSearchResultInterfaceFactory
  35. */
  36. protected $searchResultFactory;
  37. /**
  38. * @var ProductOptionProcessorInterface[]
  39. */
  40. protected $processorPool;
  41. /**
  42. * @var OrderItemInterface[]
  43. */
  44. protected $registry = [];
  45. /**
  46. * @var CollectionProcessorInterface
  47. */
  48. private $collectionProcessor;
  49. /**
  50. * @var ProductOption
  51. */
  52. private $productOption;
  53. /**
  54. * @param DataObjectFactory $objectFactory
  55. * @param Metadata $metadata
  56. * @param OrderItemSearchResultInterfaceFactory $searchResultFactory
  57. * @param CollectionProcessorInterface $collectionProcessor
  58. * @param ProductOption $productOption
  59. * @param array $processorPool
  60. */
  61. public function __construct(
  62. DataObjectFactory $objectFactory,
  63. Metadata $metadata,
  64. OrderItemSearchResultInterfaceFactory $searchResultFactory,
  65. CollectionProcessorInterface $collectionProcessor,
  66. ProductOption $productOption,
  67. array $processorPool = []
  68. ) {
  69. $this->objectFactory = $objectFactory;
  70. $this->metadata = $metadata;
  71. $this->searchResultFactory = $searchResultFactory;
  72. $this->collectionProcessor = $collectionProcessor;
  73. $this->productOption = $productOption;
  74. $this->processorPool = $processorPool;
  75. }
  76. /**
  77. * Loads entity.
  78. *
  79. * @param int $id
  80. * @return OrderItemInterface
  81. * @throws InputException
  82. * @throws NoSuchEntityException
  83. */
  84. public function get($id)
  85. {
  86. if (!$id) {
  87. throw new InputException(__('An ID is needed. Set the ID and try again.'));
  88. }
  89. if (!isset($this->registry[$id])) {
  90. /** @var OrderItemInterface $orderItem */
  91. $orderItem = $this->metadata->getNewInstance()->load($id);
  92. if (!$orderItem->getItemId()) {
  93. throw new NoSuchEntityException(
  94. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  95. );
  96. }
  97. $this->productOption->add($orderItem);
  98. $this->addParentItem($orderItem);
  99. $this->registry[$id] = $orderItem;
  100. }
  101. return $this->registry[$id];
  102. }
  103. /**
  104. * Find entities by criteria
  105. *
  106. * @param SearchCriteriaInterface $searchCriteria
  107. * @return OrderItemInterface[]
  108. */
  109. public function getList(SearchCriteriaInterface $searchCriteria)
  110. {
  111. /** @var \Magento\Sales\Model\ResourceModel\Order\Item\Collection $searchResult */
  112. $searchResult = $this->searchResultFactory->create();
  113. $searchResult->setSearchCriteria($searchCriteria);
  114. $this->collectionProcessor->process($searchCriteria, $searchResult);
  115. /** @var OrderItemInterface $orderItem */
  116. foreach ($searchResult->getItems() as $orderItem) {
  117. $this->productOption->add($orderItem);
  118. }
  119. return $searchResult;
  120. }
  121. /**
  122. * Register entity to delete
  123. *
  124. * @param OrderItemInterface $entity
  125. * @return bool
  126. */
  127. public function delete(OrderItemInterface $entity)
  128. {
  129. $this->metadata->getMapper()->delete($entity);
  130. unset($this->registry[$entity->getEntityId()]);
  131. return true;
  132. }
  133. /**
  134. * Delete entity by Id
  135. *
  136. * @param int $id
  137. * @return bool
  138. */
  139. public function deleteById($id)
  140. {
  141. $entity = $this->get($id);
  142. return $this->delete($entity);
  143. }
  144. /**
  145. * Perform persist operations for one entity
  146. *
  147. * @param OrderItemInterface $entity
  148. * @return OrderItemInterface
  149. */
  150. public function save(OrderItemInterface $entity)
  151. {
  152. if ($entity->getProductOption()) {
  153. $request = $this->getBuyRequest($entity);
  154. $productOptions = $entity->getProductOptions();
  155. $productOptions['info_buyRequest'] = $request->toArray();
  156. $entity->setProductOptions($productOptions);
  157. }
  158. $this->metadata->getMapper()->save($entity);
  159. $this->registry[$entity->getEntityId()] = $entity;
  160. return $this->registry[$entity->getEntityId()];
  161. }
  162. /**
  163. * Set parent item.
  164. *
  165. * @param OrderItemInterface $orderItem
  166. * @throws InputException
  167. * @throws NoSuchEntityException
  168. */
  169. private function addParentItem(OrderItemInterface $orderItem)
  170. {
  171. if ($parentId = $orderItem->getParentItemId()) {
  172. $orderItem->setParentItem($this->get($parentId));
  173. } else {
  174. $orderCollection = $orderItem->getOrder()->getItemsCollection()->filterByParent($orderItem->getItemId());
  175. foreach ($orderCollection->getItems() as $item) {
  176. if ($item->getParentItemId() === $orderItem->getItemId()) {
  177. $item->setParentItem($orderItem);
  178. }
  179. }
  180. }
  181. }
  182. /**
  183. * Retrieve order item's buy request
  184. *
  185. * @param OrderItemInterface $entity
  186. * @return DataObject
  187. */
  188. protected function getBuyRequest(OrderItemInterface $entity)
  189. {
  190. $request = $this->objectFactory->create(['qty' => $entity->getQtyOrdered()]);
  191. $productType = $entity->getProductType();
  192. if (isset($this->processorPool[$productType])
  193. && !$entity->getParentItemId()) {
  194. $productOption = $entity->getProductOption();
  195. if ($productOption) {
  196. $requestUpdate = $this->processorPool[$productType]->convertToBuyRequest($productOption);
  197. $request->addData($requestUpdate->getData());
  198. }
  199. }
  200. if (isset($this->processorPool['custom_options'])
  201. && !$entity->getParentItemId()) {
  202. $productOption = $entity->getProductOption();
  203. if ($productOption) {
  204. $requestUpdate = $this->processorPool['custom_options']->convertToBuyRequest($productOption);
  205. $request->addData($requestUpdate->getData());
  206. }
  207. }
  208. return $request;
  209. }
  210. }