GuestCartItemRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Model\GuestCart;
  8. use Magento\Quote\Api\Data\CartItemInterface;
  9. use Magento\Quote\Api\CartItemRepositoryInterface;
  10. use Magento\Quote\Model\QuoteIdMask;
  11. use Magento\Quote\Model\QuoteIdMaskFactory;
  12. /**
  13. * Cart Item repository class for guest carts.
  14. */
  15. class GuestCartItemRepository implements \Magento\Quote\Api\GuestCartItemRepositoryInterface
  16. {
  17. /**
  18. * @var \Magento\Quote\Api\CartItemRepositoryInterface
  19. */
  20. protected $repository;
  21. /**
  22. * @var QuoteIdMaskFactory
  23. */
  24. protected $quoteIdMaskFactory;
  25. /**
  26. * Constructs a read service object.
  27. *
  28. * @param \Magento\Quote\Api\CartItemRepositoryInterface $repository
  29. * @param QuoteIdMaskFactory $quoteIdMaskFactory
  30. */
  31. public function __construct(
  32. \Magento\Quote\Api\CartItemRepositoryInterface $repository,
  33. QuoteIdMaskFactory $quoteIdMaskFactory
  34. ) {
  35. $this->quoteIdMaskFactory = $quoteIdMaskFactory;
  36. $this->repository = $repository;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getList($cartId)
  42. {
  43. /** @var $quoteIdMask QuoteIdMask */
  44. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  45. $cartItemList = $this->repository->getList($quoteIdMask->getQuoteId());
  46. /** @var $item CartItemInterface */
  47. foreach ($cartItemList as $item) {
  48. $item->setQuoteId($quoteIdMask->getMaskedId());
  49. }
  50. return $cartItemList;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
  56. {
  57. /** @var $quoteIdMask QuoteIdMask */
  58. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartItem->getQuoteId(), 'masked_id');
  59. $cartItem->setQuoteId($quoteIdMask->getQuoteId());
  60. return $this->repository->save($cartItem);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function deleteById($cartId, $itemId)
  66. {
  67. /** @var $quoteIdMask QuoteIdMask */
  68. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  69. return $this->repository->deleteById($quoteIdMask->getQuoteId(), $itemId);
  70. }
  71. }