7
7
8
8
namespace Magento \QuoteGraphQl \Model \Resolver ;
9
9
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Exception \CouldNotSaveException ;
10
12
use Magento \Framework \GraphQl \Config \Element \Field ;
13
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
11
14
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
12
16
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
17
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
- use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
15
- use Magento \Quote \Api \CartRepositoryInterface ;
16
18
use Magento \GraphQl \Model \Query \ContextInterface ;
17
- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
19
+ use Magento \Quote \Api \CartRepositoryInterface ;
20
+ use Magento \Quote \Model \Cart \CustomerCartResolver ;
21
+ use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
22
+ use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
18
23
19
24
/**
20
25
* Merge Carts Resolver
26
+ *
27
+ * @SuppressWarnings(PHPMD.LongVariable)
21
28
*/
22
29
class MergeCarts implements ResolverInterface
23
30
{
@@ -31,44 +38,95 @@ class MergeCarts implements ResolverInterface
31
38
*/
32
39
private $ cartRepository ;
33
40
41
+ /**
42
+ * @var CustomerCartResolver
43
+ */
44
+ private $ customerCartResolver ;
45
+
46
+ /**
47
+ * @var QuoteIdToMaskedQuoteIdInterface
48
+ */
49
+ private $ quoteIdToMaskedQuoteId ;
50
+
34
51
/**
35
52
* @param GetCartForUser $getCartForUser
36
53
* @param CartRepositoryInterface $cartRepository
54
+ * @param CustomerCartResolver|null $customerCartResolver
55
+ * @param QuoteIdToMaskedQuoteIdInterface|null $quoteIdToMaskedQuoteId
37
56
*/
38
57
public function __construct (
39
58
GetCartForUser $ getCartForUser ,
40
- CartRepositoryInterface $ cartRepository
59
+ CartRepositoryInterface $ cartRepository ,
60
+ CustomerCartResolver $ customerCartResolver = null ,
61
+ QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId = null
41
62
) {
42
63
$ this ->getCartForUser = $ getCartForUser ;
43
64
$ this ->cartRepository = $ cartRepository ;
65
+ $ this ->customerCartResolver = $ customerCartResolver
66
+ ?: ObjectManager::getInstance ()->get (CustomerCartResolver::class);
67
+ $ this ->quoteIdToMaskedQuoteId = $ quoteIdToMaskedQuoteId
68
+ ?: ObjectManager::getInstance ()->get (QuoteIdToMaskedQuoteIdInterface::class);
44
69
}
45
70
46
71
/**
47
72
* @inheritdoc
48
73
*/
49
- public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
50
- {
74
+ public function resolve (
75
+ Field $ field ,
76
+ $ context ,
77
+ ResolveInfo $ info ,
78
+ array $ value = null ,
79
+ array $ args = null
80
+ ) {
51
81
if (empty ($ args ['source_cart_id ' ])) {
52
- throw new GraphQlInputException (__ ('Required parameter "source_cart_id" is missing ' ));
53
- }
54
-
55
- if (empty ($ args ['destination_cart_id ' ])) {
56
- throw new GraphQlInputException (__ ('Required parameter "destination_cart_id" is missing ' ));
82
+ throw new GraphQlInputException (__ (
83
+ 'Required parameter "source_cart_id" is missing '
84
+ ));
57
85
}
58
86
59
87
/** @var ContextInterface $context */
60
88
if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
61
- throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
89
+ throw new GraphQlAuthorizationException (__ (
90
+ 'The current customer isn \'t authorized. '
91
+ ));
92
+ }
93
+ $ currentUserId = $ context ->getUserId ();
94
+
95
+ if (!isset ($ args ['destination_cart_id ' ])) {
96
+ try {
97
+ $ cart = $ this ->customerCartResolver ->resolve ($ currentUserId );
98
+ } catch (CouldNotSaveException $ exception ) {
99
+ throw new GraphQlNoSuchEntityException (
100
+ __ ('Could not create empty cart for customer ' ),
101
+ $ exception
102
+ );
103
+ }
104
+ $ customerMaskedCartId = $ this ->quoteIdToMaskedQuoteId ->execute (
105
+ (int ) $ cart ->getId ()
106
+ );
107
+ } else {
108
+ if (empty ($ args ['destination_cart_id ' ])) {
109
+ throw new GraphQlInputException (__ (
110
+ 'The parameter "destination_cart_id" cannot be empty '
111
+ ));
112
+ }
62
113
}
63
114
64
115
$ guestMaskedCartId = $ args ['source_cart_id ' ];
65
- $ customerMaskedCartId = $ args ['destination_cart_id ' ];
116
+ $ customerMaskedCartId = $ customerMaskedCartId ?? $ args ['destination_cart_id ' ];
66
117
67
- $ currentUserId = $ context ->getUserId ();
68
118
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
69
119
// passing customerId as null enforces source cart should always be a guestcart
70
- $ guestCart = $ this ->getCartForUser ->execute ($ guestMaskedCartId , null , $ storeId );
71
- $ customerCart = $ this ->getCartForUser ->execute ($ customerMaskedCartId , $ currentUserId , $ storeId );
120
+ $ guestCart = $ this ->getCartForUser ->execute (
121
+ $ guestMaskedCartId ,
122
+ null ,
123
+ $ storeId
124
+ );
125
+ $ customerCart = $ this ->getCartForUser ->execute (
126
+ $ customerMaskedCartId ,
127
+ $ currentUserId ,
128
+ $ storeId
129
+ );
72
130
$ customerCart ->merge ($ guestCart );
73
131
$ guestCart ->setIsActive (false );
74
132
$ this ->cartRepository ->save ($ customerCart );
0 commit comments