|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
| 20 | + |
| 21 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 22 | +use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; |
| 23 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 24 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 25 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 26 | +use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; |
| 27 | +use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest; |
| 28 | +use Magento\QuoteGraphQl\Model\Cart\ValidateMaskedQuoteId; |
| 29 | + |
| 30 | +/** |
| 31 | + * Creates a guest cart |
| 32 | + */ |
| 33 | +class CreateGuestCart implements ResolverInterface |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var CreateEmptyCartForGuest |
| 37 | + */ |
| 38 | + private CreateEmptyCartForGuest $createEmptyCartForGuest; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var MaskedQuoteIdToQuoteIdInterface |
| 42 | + */ |
| 43 | + private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var CartRepositoryInterface |
| 47 | + */ |
| 48 | + private CartRepositoryInterface $cartRepository; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ValidateMaskedQuoteId |
| 52 | + */ |
| 53 | + private ValidateMaskedQuoteId $validateMaskedQuoteId; |
| 54 | + |
| 55 | + /** |
| 56 | + * @param CreateEmptyCartForGuest $createEmptyCartForGuest |
| 57 | + * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId |
| 58 | + * @param CartRepositoryInterface $cartRepository |
| 59 | + * @param ValidateMaskedQuoteId $validateMaskedQuoteId |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + CreateEmptyCartForGuest $createEmptyCartForGuest, |
| 63 | + MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, |
| 64 | + CartRepositoryInterface $cartRepository, |
| 65 | + ValidateMaskedQuoteId $validateMaskedQuoteId |
| 66 | + ) { |
| 67 | + $this->createEmptyCartForGuest = $createEmptyCartForGuest; |
| 68 | + $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; |
| 69 | + $this->cartRepository = $cartRepository; |
| 70 | + $this->validateMaskedQuoteId = $validateMaskedQuoteId; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @inheritdoc |
| 75 | + */ |
| 76 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 77 | + { |
| 78 | + $customerId = $context->getUserId(); |
| 79 | + |
| 80 | + $predefinedMaskedQuoteId = null; |
| 81 | + if (isset($args['input']['cart_uid'])) { |
| 82 | + $predefinedMaskedQuoteId = $args['input']['cart_uid']; |
| 83 | + $this->validateMaskedQuoteId->execute($predefinedMaskedQuoteId); |
| 84 | + } |
| 85 | + |
| 86 | + if ($customerId === 0 || $customerId === null) { |
| 87 | + $maskedQuoteId = $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId); |
| 88 | + $cartId = $this->maskedQuoteIdToQuoteId->execute($maskedQuoteId); |
| 89 | + $cart = $this->cartRepository->get($cartId); |
| 90 | + } else { |
| 91 | + throw new GraphQlAlreadyExistsException( |
| 92 | + __('Use `Query.customerCart` for logged in customer.') |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return [ |
| 97 | + 'cart' => [ |
| 98 | + 'model' => $cart, |
| 99 | + ], |
| 100 | + ]; |
| 101 | + } |
| 102 | +} |
0 commit comments