|
| 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\Exception\GraphQlAlreadyExistsException; |
| 13 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 14 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 17 | +use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface; |
| 18 | +use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest; |
| 19 | + |
| 20 | +/** |
| 21 | + * Creates a guest cart |
| 22 | + */ |
| 23 | +class CreateGuestCart implements ResolverInterface |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var CreateEmptyCartForGuest |
| 27 | + */ |
| 28 | + private CreateEmptyCartForGuest $createEmptyCartForGuest; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var MaskedQuoteIdToQuoteIdInterface |
| 32 | + */ |
| 33 | + private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var CartRepositoryInterface |
| 37 | + */ |
| 38 | + private CartRepositoryInterface $cartRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param CreateEmptyCartForGuest $createEmptyCartForGuest |
| 42 | + * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId |
| 43 | + * @param CartRepositoryInterface $cartRepository |
| 44 | + */ |
| 45 | + public function __construct( |
| 46 | + CreateEmptyCartForGuest $createEmptyCartForGuest, |
| 47 | + MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, |
| 48 | + CartRepositoryInterface $cartRepository |
| 49 | + ) { |
| 50 | + $this->createEmptyCartForGuest = $createEmptyCartForGuest; |
| 51 | + $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; |
| 52 | + $this->cartRepository = $cartRepository; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates an empty cart for guest and returns a cart object |
| 57 | + * |
| 58 | + * @param Field $field |
| 59 | + * @param $context |
| 60 | + * @param ResolveInfo $info |
| 61 | + * @param array|null $value |
| 62 | + * @param array|null $args |
| 63 | + * @return array[] |
| 64 | + * @throws GraphQlAlreadyExistsException |
| 65 | + * @throws GraphQlInputException |
| 66 | + * @throws NoSuchEntityException |
| 67 | + */ |
| 68 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 69 | + { |
| 70 | + $customerId = $context->getUserId(); |
| 71 | + |
| 72 | + $predefinedMaskedQuoteId = null; |
| 73 | + if (isset($args['input']['cart_uid'])) { |
| 74 | + $predefinedMaskedQuoteId = $args['input']['cart_uid']; |
| 75 | + $this->validateMaskedId($predefinedMaskedQuoteId); |
| 76 | + } |
| 77 | + |
| 78 | + if (0 === $customerId || null === $customerId) { |
| 79 | + $maskedQuoteId = $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId); |
| 80 | + $cartId = $this->maskedQuoteIdToQuoteId->execute($maskedQuoteId); |
| 81 | + $cart = $this->cartRepository->get($cartId); |
| 82 | + } else { |
| 83 | + throw new GraphQlAlreadyExistsException(__('Use `Query.customerCart` for logged in customer.')); |
| 84 | + } |
| 85 | + |
| 86 | + return [ |
| 87 | + 'cart' => [ |
| 88 | + 'model' => $cart, |
| 89 | + ], |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Validate masked id |
| 95 | + * |
| 96 | + * @param string $maskedId |
| 97 | + * @throws GraphQlAlreadyExistsException |
| 98 | + * @throws GraphQlInputException |
| 99 | + */ |
| 100 | + private function validateMaskedId(string $maskedId): void |
| 101 | + { |
| 102 | + if (mb_strlen($maskedId) != 32) { |
| 103 | + throw new GraphQlInputException(__('Cart ID length should be 32 characters.')); |
| 104 | + } |
| 105 | + |
| 106 | + if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) { |
| 107 | + throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Check is quote with such maskedId already exists |
| 113 | + * |
| 114 | + * @param string $maskedId |
| 115 | + * @return bool |
| 116 | + */ |
| 117 | + private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool |
| 118 | + { |
| 119 | + try { |
| 120 | + $this->maskedQuoteIdToQuoteId->execute($maskedId); |
| 121 | + return true; |
| 122 | + } catch (NoSuchEntityException $e) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments