Skip to content

Commit 70356cf

Browse files
committed
LYNX-259: Extracted this validation to a separate class and use it for validation in both new CreateGuestCart and existing CreateEmptyCart
1 parent 625dced commit 70356cf

File tree

7 files changed

+307
-84
lines changed

7 files changed

+307
-84
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Cart;
20+
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
23+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
24+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
25+
26+
class ValidateMaskedQuoteId
27+
{
28+
/**
29+
* @var MaskedQuoteIdToQuoteIdInterface
30+
*/
31+
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
32+
33+
/**
34+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
35+
*/
36+
public function __construct(
37+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38+
) {
39+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
40+
}
41+
42+
/**
43+
* Validate masked id
44+
*
45+
* @param string $maskedId
46+
* @throws GraphQlAlreadyExistsException
47+
* @throws GraphQlInputException
48+
*/
49+
public function execute(string $maskedId): void
50+
{
51+
if (mb_strlen($maskedId) != 32) {
52+
throw new GraphQlInputException(__('Cart ID length should to be 32 symbols.'));
53+
}
54+
55+
if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) {
56+
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId));
57+
}
58+
}
59+
60+
/**
61+
* Check is quote with such maskedId already exists
62+
*
63+
* @param string $maskedId
64+
* @return bool
65+
*/
66+
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool
67+
{
68+
try {
69+
$this->maskedQuoteIdToQuoteId->execute($maskedId);
70+
return true;
71+
} catch (NoSuchEntityException $e) {
72+
return false;
73+
}
74+
}
75+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CreateEmptyCart.php

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10-
use Magento\Framework\Exception\NoSuchEntityException;
1110
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
13-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1411
use Magento\Framework\GraphQl\Query\ResolverInterface;
1512
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1613
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1714
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer;
1815
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest;
16+
use Magento\QuoteGraphQl\Model\Cart\ValidateMaskedQuoteId;
1917

2018
/**
2119
* @inheritdoc
@@ -37,19 +35,27 @@ class CreateEmptyCart implements ResolverInterface
3735
*/
3836
private $maskedQuoteIdToQuoteId;
3937

38+
/**
39+
* @var ValidateMaskedQuoteId
40+
*/
41+
private ValidateMaskedQuoteId $validateMaskedQuoteId;
42+
4043
/**
4144
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
4245
* @param CreateEmptyCartForGuest $createEmptyCartForGuest
4346
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
47+
* @param ValidateMaskedQuoteId $validateMaskedQuoteId
4448
*/
4549
public function __construct(
4650
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
4751
CreateEmptyCartForGuest $createEmptyCartForGuest,
48-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
52+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
53+
ValidateMaskedQuoteId $validateMaskedQuoteId
4954
) {
5055
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
5156
$this->createEmptyCartForGuest = $createEmptyCartForGuest;
5257
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
58+
$this->validateMaskedQuoteId = $validateMaskedQuoteId;
5359
}
5460

5561
/**
@@ -62,46 +68,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6268
$predefinedMaskedQuoteId = null;
6369
if (isset($args['input']['cart_id'])) {
6470
$predefinedMaskedQuoteId = $args['input']['cart_id'];
65-
$this->validateMaskedId($predefinedMaskedQuoteId);
71+
$this->validateMaskedQuoteId->execute($predefinedMaskedQuoteId);
6672
}
6773

6874
$maskedQuoteId = (0 === $customerId || null === $customerId)
6975
? $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId)
7076
: $this->createEmptyCartForCustomer->execute($customerId, $predefinedMaskedQuoteId);
7177
return $maskedQuoteId;
7278
}
73-
74-
/**
75-
* Validate masked id
76-
*
77-
* @param string $maskedId
78-
* @throws GraphQlAlreadyExistsException
79-
* @throws GraphQlInputException
80-
*/
81-
private function validateMaskedId(string $maskedId): void
82-
{
83-
if (mb_strlen($maskedId) != 32) {
84-
throw new GraphQlInputException(__('Cart ID length should to be 32 symbols.'));
85-
}
86-
87-
if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) {
88-
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId));
89-
}
90-
}
91-
92-
/**
93-
* Check is quote with such maskedId already exists
94-
*
95-
* @param string $maskedId
96-
* @return bool
97-
*/
98-
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool
99-
{
100-
try {
101-
$this->maskedQuoteIdToQuoteId->execute($maskedId);
102-
return true;
103-
} catch (NoSuchEntityException $e) {
104-
return false;
105-
}
106-
}
10779
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CreateGuestCart.php

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
namespace Magento\QuoteGraphQl\Model\Resolver;
2020

21-
use Magento\Framework\Exception\NoSuchEntityException;
2221
use Magento\Framework\GraphQl\Config\Element\Field;
2322
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
24-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2523
use Magento\Framework\GraphQl\Query\ResolverInterface;
2624
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2725
use Magento\Quote\Api\CartRepositoryInterface;
2826
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
2927
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest;
28+
use Magento\QuoteGraphQl\Model\Cart\ValidateMaskedQuoteId;
3029

3130
/**
3231
* Creates a guest cart
@@ -48,19 +47,27 @@ class CreateGuestCart implements ResolverInterface
4847
*/
4948
private CartRepositoryInterface $cartRepository;
5049

50+
/**
51+
* @var ValidateMaskedQuoteId
52+
*/
53+
private ValidateMaskedQuoteId $validateMaskedQuoteId;
54+
5155
/**
5256
* @param CreateEmptyCartForGuest $createEmptyCartForGuest
5357
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
5458
* @param CartRepositoryInterface $cartRepository
59+
* @param ValidateMaskedQuoteId $validateMaskedQuoteId
5560
*/
5661
public function __construct(
5762
CreateEmptyCartForGuest $createEmptyCartForGuest,
5863
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
59-
CartRepositoryInterface $cartRepository
64+
CartRepositoryInterface $cartRepository,
65+
ValidateMaskedQuoteId $validateMaskedQuoteId
6066
) {
6167
$this->createEmptyCartForGuest = $createEmptyCartForGuest;
6268
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6369
$this->cartRepository = $cartRepository;
70+
$this->validateMaskedQuoteId = $validateMaskedQuoteId;
6471
}
6572

6673
/**
@@ -73,7 +80,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7380
$predefinedMaskedQuoteId = null;
7481
if (isset($args['input']['cart_uid'])) {
7582
$predefinedMaskedQuoteId = $args['input']['cart_uid'];
76-
$this->validateMaskedId($predefinedMaskedQuoteId);
83+
$this->validateMaskedQuoteId->execute($predefinedMaskedQuoteId);
7784
}
7885

7986
if ($customerId === 0 || $customerId === null) {
@@ -82,7 +89,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8289
$cart = $this->cartRepository->get($cartId);
8390
} else {
8491
throw new GraphQlAlreadyExistsException(
85-
__('Use `Query.cart` or `Query.customerCart` for logged in customer.')
92+
__('Use `Query.customerCart` for logged in customer.')
8693
);
8794
}
8895

@@ -92,38 +99,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9299
],
93100
];
94101
}
95-
96-
/**
97-
* Validate masked id
98-
*
99-
* @param string $maskedId
100-
* @throws GraphQlAlreadyExistsException
101-
* @throws GraphQlInputException
102-
*/
103-
private function validateMaskedId(string $maskedId): void
104-
{
105-
if (mb_strlen($maskedId) != 32) {
106-
throw new GraphQlInputException(__('Cart ID length should be 32 characters.'));
107-
}
108-
109-
if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) {
110-
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId));
111-
}
112-
}
113-
114-
/**
115-
* Check is quote with such maskedId already exists
116-
*
117-
* @param string $maskedId
118-
* @return bool
119-
*/
120-
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool
121-
{
122-
try {
123-
$this->maskedQuoteIdToQuoteId->execute($maskedId);
124-
return true;
125-
} catch (NoSuchEntityException $e) {
126-
return false;
127-
}
128-
}
129102
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Query {
99

1010
type Mutation {
1111
createGuestCart(input: CreateGuestCartInput): CreateGuestCartOutput @doc(description: "Create a new shopping cart") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateGuestCart")
12-
createEmptyCart(input: createEmptyCartInput): String @deprecated(reason: "Use `Mutation.createGuestCart` for guest and `Query.cart` or `Query.customerCart` for logged in customer") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Create an empty shopping cart for a guest or logged in user")
12+
createEmptyCart(input: createEmptyCartInput @doc(description: "An optional input object that assigns the specified ID to the cart.")): String @deprecated(reason: "Use `Mutation.createGuestCart` or `Query.customerCart` for logged in customer") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Create an empty shopping cart for a guest or logged in user")
1313
addSimpleProductsToCart(input: AddSimpleProductsToCartInput @doc(description: "An input object that defines which simple products to add to the cart.")): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart") @doc(description:"Add one or more simple products to the specified cart. We recommend using `addProductsToCart` instead.")
1414
addVirtualProductsToCart(input: AddVirtualProductsToCartInput @doc(description: "An input object that defines which virtual products to add to the cart.")): AddVirtualProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart") @doc(description:"Add one or more virtual products to the specified cart. We recommend using `addProductsToCart` instead.")
1515
applyCouponToCart(input: ApplyCouponToCartInput @doc(description: "An input object that defines the coupon code to apply to the cart.")): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ApplyCouponToCart") @doc(description:"Apply a pre-defined coupon code to the specified cart.")

0 commit comments

Comments
 (0)