OrderRepository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\InputException;
  10. use Magento\Framework\Exception\State\InvalidTransitionException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. /**
  13. * Order gift message repository object.
  14. */
  15. class OrderRepository implements \Magento\GiftMessage\Api\OrderRepositoryInterface
  16. {
  17. /**
  18. * Order factory.
  19. *
  20. * @var \Magento\Sales\Model\OrderFactory
  21. */
  22. protected $orderFactory;
  23. /**
  24. * Store manager interface.
  25. *
  26. * @var \Magento\Store\Model\StoreManagerInterface
  27. */
  28. protected $storeManager;
  29. /**
  30. * Gift message save model.
  31. *
  32. * @var \Magento\GiftMessage\Model\Save
  33. */
  34. protected $giftMessageSaveModel;
  35. /**
  36. * Message helper.
  37. *
  38. * @var \Magento\GiftMessage\Helper\Message
  39. */
  40. protected $helper;
  41. /**
  42. * Message factory.
  43. *
  44. * @var \Magento\GiftMessage\Model\MessageFactory
  45. */
  46. protected $messageFactory;
  47. /**
  48. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  49. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  50. * @param \Magento\GiftMessage\Model\Save $giftMessageSaveModel
  51. * @param \Magento\GiftMessage\Helper\Message $helper
  52. * @param MessageFactory $messageFactory
  53. */
  54. public function __construct(
  55. \Magento\Sales\Model\OrderFactory $orderFactory,
  56. \Magento\Store\Model\StoreManagerInterface $storeManager,
  57. \Magento\GiftMessage\Model\Save $giftMessageSaveModel,
  58. \Magento\GiftMessage\Helper\Message $helper,
  59. \Magento\GiftMessage\Model\MessageFactory $messageFactory
  60. ) {
  61. $this->orderFactory = $orderFactory;
  62. $this->giftMessageSaveModel = $giftMessageSaveModel;
  63. $this->storeManager = $storeManager;
  64. $this->helper = $helper;
  65. $this->messageFactory = $messageFactory;
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function get($orderId)
  71. {
  72. /** @var \Magento\Sales\Api\Data\OrderInterface $order */
  73. $order = $this->orderFactory->create()->load($orderId);
  74. if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) {
  75. throw new NoSuchEntityException(
  76. __("Either no order exists with this ID or gift message isn't allowed.")
  77. );
  78. }
  79. $messageId = $order->getGiftMessageId();
  80. if (!$messageId) {
  81. throw new NoSuchEntityException(
  82. __('No item with the provided ID was found in the Order. Verify the ID and try again.')
  83. );
  84. }
  85. return $this->messageFactory->create()->load($messageId);
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
  91. {
  92. /** @var \Magento\Sales\Api\Data\OrderInterface $order */
  93. $order = $this->orderFactory->create()->load($orderId);
  94. if (!$order->getEntityId()) {
  95. throw new NoSuchEntityException(__('No order exists with this ID. Verify your information and try again.'));
  96. }
  97. if (0 == $order->getTotalItemCount()) {
  98. throw new InputException(
  99. __("Gift messages can't be used for an empty order. Create an order, add an item, and try again.")
  100. );
  101. }
  102. if ($order->getIsVirtual()) {
  103. throw new InvalidTransitionException(__("Gift messages can't be used for virtual products."));
  104. }
  105. if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) {
  106. throw new CouldNotSaveException(__("The gift message isn't available."));
  107. }
  108. $message = [];
  109. $message[$orderId] = [
  110. 'type' => 'order',
  111. $giftMessage::CUSTOMER_ID => $giftMessage->getCustomerId(),
  112. $giftMessage::SENDER => $giftMessage->getSender(),
  113. $giftMessage::RECIPIENT => $giftMessage->getRecipient(),
  114. $giftMessage::MESSAGE => $giftMessage->getMessage(),
  115. ];
  116. $this->giftMessageSaveModel->setGiftmessages($message);
  117. try {
  118. $this->giftMessageSaveModel->saveAllInOrder();
  119. } catch (\Exception $e) {
  120. throw new CouldNotSaveException(
  121. __('The gift message couldn\'t be added to the "%1" order.', $e->getMessage()),
  122. $e
  123. );
  124. }
  125. return true;
  126. }
  127. }