7
7
8
8
namespace Magento \QuoteGraphQl \Model \Resolver ;
9
9
10
+ use Magento \Framework \Exception \NoSuchEntityException ;
10
11
use Magento \Framework \GraphQl \Config \Element \Field ;
12
+ use Magento \Framework \GraphQl \Exception \GraphQlAlreadyExistsException ;
13
+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
11
14
use Magento \Framework \GraphQl \Query \ResolverInterface ;
12
15
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
13
- use Magento \Quote \Api \CartManagementInterface ;
14
- use Magento \Quote \Api \GuestCartManagementInterface ;
15
- use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
16
- use Magento \Quote \Model \QuoteIdMaskFactory ;
16
+ use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
17
+ use Magento \QuoteGraphQl \Model \Cart \CreateEmptyCartForCustomer ;
18
+ use Magento \QuoteGraphQl \Model \Cart \CreateEmptyCartForGuest ;
17
19
18
20
/**
19
21
* @inheritdoc
20
22
*/
21
23
class CreateEmptyCart implements ResolverInterface
22
24
{
23
25
/**
24
- * @var CartManagementInterface
26
+ * @var CreateEmptyCartForCustomer
25
27
*/
26
- private $ cartManagement ;
28
+ private $ createEmptyCartForCustomer ;
27
29
28
30
/**
29
- * @var GuestCartManagementInterface
31
+ * @var CreateEmptyCartForGuest
30
32
*/
31
- private $ guestCartManagement ;
33
+ private $ createEmptyCartForGuest ;
32
34
33
35
/**
34
- * @var QuoteIdToMaskedQuoteIdInterface
36
+ * @var MaskedQuoteIdToQuoteIdInterface
35
37
*/
36
- private $ quoteIdToMaskedId ;
38
+ private $ maskedQuoteIdToQuoteId ;
37
39
38
40
/**
39
- * @var QuoteIdMaskFactory
40
- */
41
- private $ quoteIdMaskFactory ;
42
-
43
- /**
44
- * @param CartManagementInterface $cartManagement
45
- * @param GuestCartManagementInterface $guestCartManagement
46
- * @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
47
- * @param QuoteIdMaskFactory $quoteIdMaskFactory
41
+ * @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
42
+ * @param CreateEmptyCartForGuest $createEmptyCartForGuest
43
+ * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
48
44
*/
49
45
public function __construct (
50
- CartManagementInterface $ cartManagement ,
51
- GuestCartManagementInterface $ guestCartManagement ,
52
- QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedId ,
53
- QuoteIdMaskFactory $ quoteIdMaskFactory
46
+ CreateEmptyCartForCustomer $ createEmptyCartForCustomer ,
47
+ CreateEmptyCartForGuest $ createEmptyCartForGuest ,
48
+ MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId
54
49
) {
55
- $ this ->cartManagement = $ cartManagement ;
56
- $ this ->guestCartManagement = $ guestCartManagement ;
57
- $ this ->quoteIdToMaskedId = $ quoteIdToMaskedId ;
58
- $ this ->quoteIdMaskFactory = $ quoteIdMaskFactory ;
50
+ $ this ->createEmptyCartForCustomer = $ createEmptyCartForCustomer ;
51
+ $ this ->createEmptyCartForGuest = $ createEmptyCartForGuest ;
52
+ $ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
59
53
}
60
54
61
55
/**
@@ -65,19 +59,45 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
65
59
{
66
60
$ customerId = $ context ->getUserId ();
67
61
68
- if (0 !== $ customerId && null !== $ customerId ) {
69
- $ quoteId = $ this ->cartManagement ->createEmptyCartForCustomer ($ customerId );
70
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ quoteId );
71
-
72
- if (empty ($ maskedQuoteId )) {
73
- $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ();
74
- $ quoteIdMask ->setQuoteId ($ quoteId )->save ();
75
- $ maskedQuoteId = $ quoteIdMask ->getMaskedId ();
76
- }
77
- } else {
78
- $ maskedQuoteId = $ this ->guestCartManagement ->createEmptyCart ();
62
+ $ predefinedMaskedQuoteId = null ;
63
+ if (isset ($ args ['input ' ]['cart_id ' ])) {
64
+ $ predefinedMaskedQuoteId = $ args ['input ' ]['cart_id ' ];
65
+ $ this ->validateMaskedId ($ predefinedMaskedQuoteId );
79
66
}
80
67
68
+ $ maskedQuoteId = (0 === $ customerId || null === $ customerId )
69
+ ? $ this ->createEmptyCartForGuest ->execute ($ predefinedMaskedQuoteId )
70
+ : $ this ->createEmptyCartForCustomer ->execute ($ customerId , $ predefinedMaskedQuoteId );
81
71
return $ maskedQuoteId ;
82
72
}
73
+
74
+ /**
75
+ * @param string $maskedId
76
+ * @throws GraphQlAlreadyExistsException
77
+ * @throws GraphQlInputException
78
+ */
79
+ private function validateMaskedId (string $ maskedId ): void
80
+ {
81
+ if (mb_strlen ($ maskedId ) != 32 ) {
82
+ throw new GraphQlInputException (__ ('Cart ID length should to be 32 symbols. ' ));
83
+ }
84
+
85
+ if ($ this ->isQuoteWithSuchMaskedIdAlreadyExists ($ maskedId )) {
86
+ throw new GraphQlAlreadyExistsException (__ ('Cart with ID "%1" already exists. ' , $ maskedId ));
87
+ }
88
+ }
89
+
90
+ /**
91
+ * @param string $maskedId
92
+ * @return bool
93
+ */
94
+ private function isQuoteWithSuchMaskedIdAlreadyExists (string $ maskedId ): bool
95
+ {
96
+ try {
97
+ $ this ->maskedQuoteIdToQuoteId ->execute ($ maskedId );
98
+ return true ;
99
+ } catch (NoSuchEntityException $ e ) {
100
+ return false ;
101
+ }
102
+ }
83
103
}
0 commit comments