CartRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  12. * Shopping cart gift message repository object for registered customer
  13. */
  14. class CartRepository implements \Magento\GiftMessage\Api\CartRepositoryInterface
  15. {
  16. /**
  17. * Quote repository.
  18. *
  19. * @var \Magento\Quote\Api\CartRepositoryInterface
  20. */
  21. protected $quoteRepository;
  22. /**
  23. * Store manager interface.
  24. *
  25. * @var \Magento\Store\Model\StoreManagerInterface
  26. */
  27. protected $storeManager;
  28. /**
  29. * Gift message manager.
  30. *
  31. * @var \Magento\GiftMessage\Model\GiftMessageManager
  32. */
  33. protected $giftMessageManager;
  34. /**
  35. * Message helper.
  36. *
  37. * @var \Magento\GiftMessage\Helper\Message
  38. */
  39. protected $helper;
  40. /**
  41. * Message factory.
  42. *
  43. * @var \Magento\GiftMessage\Model\MessageFactory
  44. */
  45. protected $messageFactory;
  46. /**
  47. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  48. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  49. * @param GiftMessageManager $giftMessageManager
  50. * @param \Magento\GiftMessage\Helper\Message $helper
  51. * @param MessageFactory $messageFactory
  52. */
  53. public function __construct(
  54. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  55. \Magento\Store\Model\StoreManagerInterface $storeManager,
  56. \Magento\GiftMessage\Model\GiftMessageManager $giftMessageManager,
  57. \Magento\GiftMessage\Helper\Message $helper,
  58. \Magento\GiftMessage\Model\MessageFactory $messageFactory
  59. ) {
  60. $this->quoteRepository = $quoteRepository;
  61. $this->giftMessageManager = $giftMessageManager;
  62. $this->storeManager = $storeManager;
  63. $this->helper = $helper;
  64. $this->messageFactory = $messageFactory;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function get($cartId)
  70. {
  71. /** @var \Magento\Quote\Model\Quote $quote */
  72. $quote = $this->quoteRepository->getActive($cartId);
  73. $messageId = $quote->getGiftMessageId();
  74. if (!$messageId) {
  75. return null;
  76. }
  77. return $this->messageFactory->create()->load($messageId);
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage)
  83. {
  84. /**
  85. * Quote.
  86. *
  87. * @var \Magento\Quote\Model\Quote $quote
  88. */
  89. $quote = $this->quoteRepository->getActive($cartId);
  90. if (0 == $quote->getItemsCount()) {
  91. throw new InputException(__("Gift messages can't be used for an empty cart. Add an item and try again."));
  92. }
  93. if ($quote->isVirtual()) {
  94. throw new InvalidTransitionException(__("Gift messages can't be used for virtual products."));
  95. }
  96. $messageText = $giftMessage->getMessage();
  97. if ($messageText && !$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) {
  98. throw new CouldNotSaveException(__("The gift message isn't available."));
  99. }
  100. $this->giftMessageManager->setMessage($quote, 'quote', $giftMessage);
  101. return true;
  102. }
  103. }