OrderItemRepository.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\GiftMessage\Model;
  8. use Magento\Framework\Exception\CouldNotSaveException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Framework\Exception\State\InvalidTransitionException;
  11. /**
  12. * Order item gift message repository object.
  13. */
  14. class OrderItemRepository implements \Magento\GiftMessage\Api\OrderItemRepositoryInterface
  15. {
  16. /**
  17. * Order factory.
  18. *
  19. * @var \Magento\Sales\Model\OrderFactory
  20. */
  21. protected $orderFactory;
  22. /**
  23. * Cached orders data.
  24. *
  25. * @var \Magento\Sales\Api\Data\OrderInterface[]
  26. */
  27. private $orders;
  28. /**
  29. * Store manager interface.
  30. *
  31. * @var \Magento\Store\Model\StoreManagerInterface
  32. */
  33. protected $storeManager;
  34. /**
  35. * Gift message save model.
  36. *
  37. * @var \Magento\GiftMessage\Model\Save
  38. */
  39. protected $giftMessageSaveModel;
  40. /**
  41. * Message helper.
  42. *
  43. * @var \Magento\GiftMessage\Helper\Message
  44. */
  45. protected $helper;
  46. /**
  47. * Message factory.
  48. *
  49. * @var \Magento\GiftMessage\Model\MessageFactory
  50. */
  51. protected $messageFactory;
  52. /**
  53. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  54. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  55. * @param \Magento\GiftMessage\Model\Save $giftMessageSaveModel
  56. * @param \Magento\GiftMessage\Helper\Message $helper
  57. * @param MessageFactory $messageFactory
  58. */
  59. public function __construct(
  60. \Magento\Sales\Model\OrderFactory $orderFactory,
  61. \Magento\Store\Model\StoreManagerInterface $storeManager,
  62. \Magento\GiftMessage\Model\Save $giftMessageSaveModel,
  63. \Magento\GiftMessage\Helper\Message $helper,
  64. \Magento\GiftMessage\Model\MessageFactory $messageFactory
  65. ) {
  66. $this->orderFactory = $orderFactory;
  67. $this->giftMessageSaveModel = $giftMessageSaveModel;
  68. $this->storeManager = $storeManager;
  69. $this->helper = $helper;
  70. $this->messageFactory = $messageFactory;
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function get($orderId, $orderItemId)
  76. {
  77. /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
  78. if (!$orderItem = $this->getItemById($orderId, $orderItemId)) {
  79. throw new NoSuchEntityException(
  80. __('No item with the provided ID was found in the Order. Verify the ID and try again.')
  81. );
  82. }
  83. if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) {
  84. throw new NoSuchEntityException(
  85. __(
  86. "No item with the provided ID was found in the Order, or a gift message isn't allowed. "
  87. . "Verify and try again."
  88. )
  89. );
  90. }
  91. $messageId = $orderItem->getGiftMessageId();
  92. if (!$messageId) {
  93. throw new NoSuchEntityException(
  94. __('No item with the provided ID was found in the Order. Verify the ID and try again.')
  95. );
  96. }
  97. return $this->messageFactory->create()->load($messageId);
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function save($orderId, $orderItemId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
  103. {
  104. /** @var \Magento\Sales\Api\Data\OrderInterface $order */
  105. $order = $this->orderFactory->create()->load($orderId);
  106. /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
  107. if (!$orderItem = $this->getItemById($orderId, $orderItemId)) {
  108. throw new NoSuchEntityException(
  109. __('No item with the provided ID was found in the Order. Verify the ID and try again.')
  110. );
  111. }
  112. if ($order->getIsVirtual()) {
  113. throw new InvalidTransitionException(__("Gift messages can't be used for virtual products."));
  114. }
  115. if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) {
  116. throw new CouldNotSaveException(__("The gift message isn't available."));
  117. }
  118. $message = [];
  119. $message[$orderItemId] = [
  120. 'type' => 'order_item',
  121. 'sender' => $giftMessage->getSender(),
  122. 'recipient' => $giftMessage->getRecipient(),
  123. 'message' => $giftMessage->getMessage(),
  124. ];
  125. $this->giftMessageSaveModel->setGiftmessages($message);
  126. try {
  127. $this->giftMessageSaveModel->saveAllInOrder();
  128. unset($this->orders[$orderId]);
  129. } catch (\Exception $e) {
  130. throw new CouldNotSaveException(
  131. __('The gift message couldn\'t be added to the "%1" order.', $e->getMessage()),
  132. $e
  133. );
  134. }
  135. return true;
  136. }
  137. /**
  138. * Get order item by id
  139. *
  140. * @param int $orderId
  141. * @param int $orderItemId
  142. * @return \Magento\Sales\Api\Data\OrderItemInterface|bool
  143. */
  144. protected function getItemById($orderId, $orderItemId)
  145. {
  146. if (!isset($this->orders[$orderId])) {
  147. $this->orders[$orderId] = $this->orderFactory->create()->load($orderId);
  148. }
  149. /** @var \Magento\Sales\Api\Data\OrderInterface $item */
  150. $order = $this->orders[$orderId];
  151. /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
  152. $item = $order->getItemById($orderItemId);
  153. if ($item !== null) {
  154. return $item;
  155. }
  156. return false;
  157. }
  158. }