ItemRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Shopping cart gift message item repository object for registered customer
  14. */
  15. class ItemRepository implements \Magento\GiftMessage\Api\ItemRepositoryInterface
  16. {
  17. /**
  18. * Quote repository.
  19. *
  20. * @var \Magento\Quote\Api\CartRepositoryInterface
  21. */
  22. protected $quoteRepository;
  23. /**
  24. * Store manager interface.
  25. *
  26. * @var \Magento\Store\Model\StoreManagerInterface
  27. */
  28. protected $storeManager;
  29. /**
  30. * Gift message manager.
  31. *
  32. * @var \Magento\GiftMessage\Model\GiftMessageManager
  33. */
  34. protected $giftMessageManager;
  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\Quote\Api\CartRepositoryInterface $quoteRepository
  49. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  50. * @param GiftMessageManager $giftMessageManager
  51. * @param \Magento\GiftMessage\Helper\Message $helper
  52. * @param MessageFactory $messageFactory
  53. */
  54. public function __construct(
  55. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  56. \Magento\Store\Model\StoreManagerInterface $storeManager,
  57. \Magento\GiftMessage\Model\GiftMessageManager $giftMessageManager,
  58. \Magento\GiftMessage\Helper\Message $helper,
  59. \Magento\GiftMessage\Model\MessageFactory $messageFactory
  60. ) {
  61. $this->quoteRepository = $quoteRepository;
  62. $this->giftMessageManager = $giftMessageManager;
  63. $this->storeManager = $storeManager;
  64. $this->helper = $helper;
  65. $this->messageFactory = $messageFactory;
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function get($cartId, $itemId)
  71. {
  72. /**
  73. * Quote.
  74. *
  75. * @var \Magento\Quote\Model\Quote $quote
  76. */
  77. $quote = $this->quoteRepository->getActive($cartId);
  78. if (!$item = $quote->getItemById($itemId)) {
  79. throw new NoSuchEntityException(
  80. __('No item with the provided ID was found in the Cart. Verify the ID and try again.')
  81. );
  82. }
  83. $messageId = $item->getGiftMessageId();
  84. if (!$messageId) {
  85. return null;
  86. }
  87. /**
  88. * Model.
  89. *
  90. * @var \Magento\GiftMessage\Model\Message $model
  91. */
  92. return $this->messageFactory->create()->load($messageId);
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage, $itemId)
  98. {
  99. /**
  100. * Quote.
  101. *
  102. * @var \Magento\Quote\Model\Quote $quote
  103. */
  104. $quote = $this->quoteRepository->getActive($cartId);
  105. if (!$item = $quote->getItemById($itemId)) {
  106. throw new NoSuchEntityException(
  107. __(
  108. 'No product with the "%1" itemId exists in the Cart. Verify your information and try again.',
  109. $itemId
  110. )
  111. );
  112. }
  113. if ($item->getIsVirtual()) {
  114. throw new InvalidTransitionException(__('Gift messages can\'t be used for virtual products.'));
  115. }
  116. $messageText = $giftMessage->getMessage();
  117. if ($messageText && !$this->helper->isMessagesAllowed('items', $quote, $this->storeManager->getStore())) {
  118. throw new CouldNotSaveException(__("The gift message isn't available."));
  119. }
  120. $this->giftMessageManager->setMessage($quote, 'quote_item', $giftMessage, $itemId);
  121. return true;
  122. }
  123. }