12
12
use Magento \Framework \Exception \InputException ;
13
13
use Magento \Framework \Exception \NoSuchEntityException ;
14
14
use Magento \Framework \Exception \StateException ;
15
- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
16
15
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
17
16
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
18
17
use Magento \Framework \GraphQl \Query \ResolverInterface ;
19
18
use Magento \Framework \GraphQl \Config \Element \Field ;
20
19
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
21
20
use Magento \Framework \Stdlib \ArrayManager ;
22
- use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
23
- use Magento \QuoteGraphQl \Model \Authorization \IsCartMutationAllowedForCurrentUser ;
24
21
use Magento \Quote \Model \Quote \AddressFactory as QuoteAddressFactory ;
25
22
use Magento \Quote \Model \ResourceModel \Quote \Address as QuoteAddressResource ;
26
23
use Magento \Checkout \Model \ShippingInformationFactory ;
24
+ use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
27
25
28
26
/**
29
27
* Class SetShippingMethodsOnCart
@@ -47,20 +45,15 @@ class SetShippingMethodsOnCart implements ResolverInterface
47
45
*/
48
46
private $ quoteAddressResource ;
49
47
50
- /**
51
- * @var MaskedQuoteIdToQuoteIdInterface
52
- */
53
- private $ maskedQuoteIdToQuoteId ;
54
-
55
48
/**
56
49
* @var ArrayManager
57
50
*/
58
51
private $ arrayManager ;
59
52
60
53
/**
61
- * @var IsCartMutationAllowedForCurrentUser
54
+ * @var GetCartForUser
62
55
*/
63
- private $ isCartMutationAllowedForCurrentUser ;
56
+ private $ getCartForUser ;
64
57
65
58
/**
66
59
* @var ShippingInformationManagementInterface
@@ -70,27 +63,23 @@ class SetShippingMethodsOnCart implements ResolverInterface
70
63
/**
71
64
* SetShippingMethodsOnCart constructor.
72
65
* @param ArrayManager $arrayManager
73
- * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
74
- * @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
66
+ * @param GetCartForUser $getCartForUser
75
67
* @param ShippingInformationManagementInterface $shippingInformationManagement
76
68
* @param QuoteAddressFactory $quoteAddressFactory
77
69
* @param QuoteAddressResource $quoteAddressResource
78
70
* @param ShippingInformationFactory $shippingInformationFactory
79
71
*/
80
72
public function __construct (
81
73
ArrayManager $ arrayManager ,
82
- MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
83
- IsCartMutationAllowedForCurrentUser $ isCartMutationAllowedForCurrentUser ,
74
+ GetCartForUser $ getCartForUser ,
84
75
ShippingInformationManagementInterface $ shippingInformationManagement ,
85
76
QuoteAddressFactory $ quoteAddressFactory ,
86
77
QuoteAddressResource $ quoteAddressResource ,
87
78
ShippingInformationFactory $ shippingInformationFactory
88
79
) {
89
80
$ this ->arrayManager = $ arrayManager ;
90
- $ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
91
- $ this ->isCartMutationAllowedForCurrentUser = $ isCartMutationAllowedForCurrentUser ;
81
+ $ this ->getCartForUser = $ getCartForUser ;
92
82
$ this ->shippingInformationManagement = $ shippingInformationManagement ;
93
-
94
83
$ this ->quoteAddressResource = $ quoteAddressResource ;
95
84
$ this ->quoteAddressFactory = $ quoteAddressFactory ;
96
85
$ this ->shippingInformationFactory = $ shippingInformationFactory ;
@@ -111,34 +100,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
111
100
throw new GraphQlInputException (__ ('Required parameter "shipping_methods" is missing ' ));
112
101
}
113
102
114
- $ shippingMethod = reset ($ shippingMethods ); // TODO: provide implementation for multishipping
103
+ $ shippingMethod = reset ($ shippingMethods );
115
104
116
105
if (!$ shippingMethod ['cart_address_id ' ]) {
117
106
throw new GraphQlInputException (__ ('Required parameter "cart_address_id" is missing ' ));
118
107
}
119
- if (!$ shippingMethod ['shipping_carrier_code ' ]) { // FIXME: check the E_WARNING here
108
+ if (!$ shippingMethod ['shipping_carrier_code ' ]) {
120
109
throw new GraphQlInputException (__ ('Required parameter "shipping_carrier_code" is missing ' ));
121
110
}
122
- if (!$ shippingMethod ['shipping_method_code ' ]) { // FIXME: check the E_WARNING here
111
+ if (!$ shippingMethod ['shipping_method_code ' ]) {
123
112
throw new GraphQlInputException (__ ('Required parameter "shipping_method_code" is missing ' ));
124
113
}
125
114
126
- try {
127
- $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ((string ) $ maskedCartId );
128
- } catch (NoSuchEntityException $ exception ) {
129
- throw new GraphQlNoSuchEntityException (
130
- __ ('Could not find a cart with ID "%masked_cart_id" ' , ['masked_cart_id ' => $ maskedCartId ])
131
- );
132
- }
133
-
134
- if (false === $ this ->isCartMutationAllowedForCurrentUser ->execute ($ cartId )) {
135
- throw new GraphQlAuthorizationException (
136
- __ (
137
- 'The current user cannot perform operations on cart "%masked_cart_id" ' ,
138
- ['masked_cart_id ' => $ maskedCartId ]
139
- )
140
- );
141
- }
115
+ $ userId = $ context ->getUserId ();
116
+ $ cart = $ this ->getCartForUser ->execute ((string ) $ maskedCartId , $ userId );
142
117
143
118
$ quoteAddress = $ this ->quoteAddressFactory ->create ();
144
119
$ this ->quoteAddressResource ->load ($ quoteAddress , $ shippingMethod ['cart_address_id ' ]);
@@ -153,7 +128,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
153
128
$ shippingInformation ->setShippingMethodCode ($ shippingMethod ['shipping_method_code ' ]);
154
129
155
130
try {
156
- $ this ->shippingInformationManagement ->saveAddressInformation ($ cartId , $ shippingInformation );
131
+ $ this ->shippingInformationManagement ->saveAddressInformation ($ cart -> getId () , $ shippingInformation );
157
132
} catch (NoSuchEntityException $ exception ) {
158
133
throw new GraphQlNoSuchEntityException (__ ($ exception ->getMessage ()));
159
134
} catch (StateException $ exception ) {
@@ -164,7 +139,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
164
139
165
140
return [
166
141
'cart ' => [
167
- 'cart_id ' => $ maskedCartId
142
+ 'cart_id ' => $ maskedCartId ,
143
+ 'model ' => $ cart
168
144
]
169
145
];
170
146
}
0 commit comments