|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 13 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 14 | +use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer; |
| 15 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 16 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
| 17 | +use Magento\Quote\Api\CartManagementInterface; |
| 18 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 19 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 20 | +use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel; |
| 21 | + |
| 22 | +/** |
| 23 | + * Get cart for the customer |
| 24 | + */ |
| 25 | +class CustomerCart implements ResolverInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var CreateEmptyCartForCustomer |
| 29 | + */ |
| 30 | + private $createEmptyCartForCustomer; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CartManagementInterface |
| 34 | + */ |
| 35 | + private $cartManagement; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var QuoteIdMaskFactory |
| 39 | + */ |
| 40 | + private $quoteIdMaskFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var QuoteIdMaskResourceModel |
| 44 | + */ |
| 45 | + private $quoteIdMaskResourceModel; |
| 46 | + /** |
| 47 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 48 | + */ |
| 49 | + private $quoteIdToMaskedQuoteId; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param CreateEmptyCartForCustomer $createEmptyCartForCustomer |
| 53 | + * @param CartManagementInterface $cartManagement |
| 54 | + * @param QuoteIdMaskFactory $quoteIdMaskFactory |
| 55 | + * @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel |
| 56 | + * @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + CreateEmptyCartForCustomer $createEmptyCartForCustomer, |
| 60 | + CartManagementInterface $cartManagement, |
| 61 | + QuoteIdMaskFactory $quoteIdMaskFactory, |
| 62 | + QuoteIdMaskResourceModel $quoteIdMaskResourceModel, |
| 63 | + QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId |
| 64 | + ) { |
| 65 | + $this->createEmptyCartForCustomer = $createEmptyCartForCustomer; |
| 66 | + $this->cartManagement = $cartManagement; |
| 67 | + $this->quoteIdMaskFactory = $quoteIdMaskFactory; |
| 68 | + $this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel; |
| 69 | + $this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritdoc |
| 74 | + */ |
| 75 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 76 | + { |
| 77 | + $currentUserId = $context->getUserId(); |
| 78 | + |
| 79 | + /** @var ContextInterface $context */ |
| 80 | + if (false === $context->getExtensionAttributes()->getIsCustomer()) { |
| 81 | + throw new GraphQlAuthorizationException(__('The request is allowed for logged in customer')); |
| 82 | + } |
| 83 | + try { |
| 84 | + $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
| 85 | + } catch (NoSuchEntityException $e) { |
| 86 | + $this->createEmptyCartForCustomer->execute($currentUserId, null); |
| 87 | + $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
| 88 | + } |
| 89 | + |
| 90 | + $maskedId = $this->quoteIdToMaskedQuoteId->execute((int) $cart->getId()); |
| 91 | + if (empty($maskedId)) { |
| 92 | + $quoteIdMask = $this->quoteIdMaskFactory->create(); |
| 93 | + $quoteIdMask->setQuoteId((int) $cart->getId()); |
| 94 | + $this->quoteIdMaskResourceModel->save($quoteIdMask); |
| 95 | + } |
| 96 | + |
| 97 | + return [ |
| 98 | + 'model' => $cart |
| 99 | + ]; |
| 100 | + } |
| 101 | +} |
0 commit comments