14
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
16
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17
- use Magento \GiftMessage \Api \ItemRepositoryInterface ;
18
- use Magento \Quote \Api \CartItemRepositoryInterface ;
19
17
use Magento \Quote \Api \CartRepositoryInterface ;
20
- use Magento \Quote \Model \Quote ;
21
18
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
22
- use Magento \QuoteGraphQl \Model \Cart \ UpdateCartItem ;
19
+ use Magento \QuoteGraphQl \Model \CartItem \ DataProvider \ UpdateCartItems as UpdateCartItemsProvider ;
23
20
24
21
/**
25
22
* @inheritdoc
26
23
*/
27
24
class UpdateCartItems implements ResolverInterface
28
25
{
29
- /**
30
- * @var UpdateCartItem
31
- */
32
- private $ updateCartItem ;
33
-
34
26
/**
35
27
* @var GetCartForUser
36
28
*/
37
29
private $ getCartForUser ;
38
30
39
- /**
40
- * @var CartItemRepositoryInterface
41
- */
42
- private $ cartItemRepository ;
43
-
44
31
/**
45
32
* @var CartRepositoryInterface
46
33
*/
47
34
private $ cartRepository ;
48
35
49
36
/**
50
- * @var ItemRepositoryInterface
37
+ * @var UpdateCartItemsProvider
51
38
*/
52
- private $ itemRepository ;
39
+ private $ updateCartItems ;
53
40
54
41
/**
55
- * @param GetCartForUser $getCartForUser
56
- * @param CartItemRepositoryInterface $cartItemRepository
57
- * @param UpdateCartItem $updateCartItem
58
- * @param CartRepositoryInterface $cartRepository
59
- * @param ItemRepositoryInterface $itemRepository
42
+ * @param GetCartForUser $getCartForUser
43
+ * @param CartRepositoryInterface $cartRepository
44
+ * @param UpdateCartItemsProvider $updateCartItems
60
45
*/
61
46
public function __construct (
62
47
GetCartForUser $ getCartForUser ,
63
- CartItemRepositoryInterface $ cartItemRepository ,
64
- UpdateCartItem $ updateCartItem ,
65
48
CartRepositoryInterface $ cartRepository ,
66
- ItemRepositoryInterface $ itemRepository
49
+ UpdateCartItemsProvider $ updateCartItems
67
50
) {
68
51
$ this ->getCartForUser = $ getCartForUser ;
69
- $ this ->cartItemRepository = $ cartItemRepository ;
70
- $ this ->updateCartItem = $ updateCartItem ;
71
52
$ this ->cartRepository = $ cartRepository ;
72
- $ this ->itemRepository = $ itemRepository ;
53
+ $ this ->updateCartItems = $ updateCartItems ;
73
54
}
74
55
75
56
/**
@@ -80,20 +61,21 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
80
61
if (empty ($ args ['input ' ]['cart_id ' ])) {
81
62
throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing. ' ));
82
63
}
64
+
83
65
$ maskedCartId = $ args ['input ' ]['cart_id ' ];
84
66
85
67
if (empty ($ args ['input ' ]['cart_items ' ])
86
68
|| !is_array ($ args ['input ' ]['cart_items ' ])
87
69
) {
88
70
throw new GraphQlInputException (__ ('Required parameter "cart_items" is missing. ' ));
89
71
}
90
- $ cartItems = $ args ['input ' ]['cart_items ' ];
91
72
73
+ $ cartItems = $ args ['input ' ]['cart_items ' ];
92
74
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
93
75
$ cart = $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
94
76
95
77
try {
96
- $ this ->processCartItems ($ cart , $ cartItems );
78
+ $ this ->updateCartItems -> processCartItems ($ cart , $ cartItems );
97
79
$ this ->cartRepository ->save ($ cart );
98
80
} catch (NoSuchEntityException $ e ) {
99
81
throw new GraphQlNoSuchEntityException (__ ($ e ->getMessage ()), $ e );
@@ -107,65 +89,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
107
89
],
108
90
];
109
91
}
110
-
111
- /**
112
- * Process cart items
113
- *
114
- * @param Quote $cart
115
- * @param array $items
116
- * @throws GraphQlInputException
117
- * @throws LocalizedException
118
- */
119
- private function processCartItems (Quote $ cart , array $ items ): void
120
- {
121
- foreach ($ items as $ item ) {
122
- if (empty ($ item ['cart_item_id ' ])) {
123
- throw new GraphQlInputException (__ ('Required parameter "cart_item_id" for "cart_items" is missing. ' ));
124
- }
125
- $ itemId = (int )$ item ['cart_item_id ' ];
126
- $ customizableOptions = $ item ['customizable_options ' ] ?? [];
127
-
128
- $ cartItem = $ cart ->getItemById ($ itemId );
129
- if ($ cartItem && $ cartItem ->getParentItemId ()) {
130
- throw new GraphQlInputException (__ ('Child items may not be updated. ' ));
131
- }
132
-
133
- if (count ($ customizableOptions ) === 0 && !isset ($ item ['quantity ' ])) {
134
- throw new GraphQlInputException (__ ('Required parameter "quantity" for "cart_items" is missing. ' ));
135
- }
136
- $ quantity = (float )$ item ['quantity ' ];
137
-
138
- if ($ quantity <= 0.0 ) {
139
- $ this ->cartItemRepository ->deleteById ((int )$ cart ->getId (), $ itemId );
140
- } else {
141
- $ this ->updateCartItem ->execute ($ cart , $ itemId , $ quantity , $ customizableOptions );
142
- }
143
-
144
- if (!empty ($ item ['gift_message ' ])) {
145
- $ this ->updateGiftMessageForItem ($ cart , $ item , $ itemId );
146
- }
147
- }
148
- }
149
-
150
- /**
151
- * Update Gift Message for Quote item
152
- *
153
- * @param Quote $cart
154
- * @param array $item
155
- * @param int $itemId
156
- *
157
- * @throws GraphQlInputException
158
- */
159
- private function updateGiftMessageForItem (Quote $ cart , array $ item , int $ itemId )
160
- {
161
- try {
162
- $ giftItemMessage = $ this ->itemRepository ->get ($ cart ->getEntityId (), $ itemId );
163
- $ giftItemMessage ->setRecipient ($ item ['gift_message ' ]['to ' ]);
164
- $ giftItemMessage ->setSender ($ item ['gift_message ' ]['from ' ]);
165
- $ giftItemMessage ->setMessage ($ item ['gift_message ' ]['message ' ]);
166
- $ this ->itemRepository ->save ($ cart ->getEntityId (), $ giftItemMessage , $ itemId );
167
- } catch (LocalizedException $ exception ) {
168
- throw new GraphQlInputException (__ ('Gift Message can not be updated. ' ));
169
- }
170
- }
171
92
}
0 commit comments