8
8
namespace Magento \QuoteGraphQl \Model \Resolver \Cart ;
9
9
10
10
use Magento \Authorization \Model \UserContextInterface ;
11
- use Magento \Catalog \Api \ProductRepositoryInterface ;
12
- use Magento \Framework \DataObject ;
13
- use Magento \Framework \DataObjectFactory ;
14
11
use Magento \Framework \Exception \NoSuchEntityException ;
15
12
use Magento \Framework \GraphQl \Config \Element \Field ;
16
13
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
25
22
use Magento \Quote \Api \GuestCartRepositoryInterface ;
26
23
use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
27
24
use Magento \Quote \Model \Quote ;
28
- use Magento \QuoteGraphQl \Model \Hydrator \CartHydrator ;
25
+ use Magento \QuoteGraphQl \Model \Cart \AddSimpleProductToCartProcessor ;
26
+ use Magento \QuoteGraphQl \Model \Resolver \DataProvider \Cart \CartHydrator ;
29
27
30
28
/**
31
29
* Add simple product to cart GraphQl resolver
32
- *
33
30
* {@inheritdoc}
34
31
*/
35
32
class AddSimpleProductsToCart implements ResolverInterface
36
33
{
34
+ /**
35
+ * @var AddSimpleProductToCartProcessor
36
+ */
37
+ private $ addSimpleProductToCartProcessor ;
38
+
37
39
/**
38
40
* @var CartRepositoryInterface
39
41
*/
@@ -44,21 +46,11 @@ class AddSimpleProductsToCart implements ResolverInterface
44
46
*/
45
47
private $ maskedQuoteIdToQuoteId ;
46
48
47
- /**
48
- * @var DataObjectFactory
49
- */
50
- private $ dataObjectFactory ;
51
-
52
49
/**
53
50
* @var GuestCartRepositoryInterface
54
51
*/
55
52
private $ guestCartRepository ;
56
53
57
- /**
58
- * @var ProductRepositoryInterface
59
- */
60
- private $ productRepository ;
61
-
62
54
/**
63
55
* @var CartHydrator
64
56
*/
@@ -80,42 +72,38 @@ class AddSimpleProductsToCart implements ResolverInterface
80
72
private $ userContext ;
81
73
82
74
/**
83
- * @param DataObjectFactory $dataObjectFactory
75
+ * @param AddSimpleProductToCartProcessor $addSimpleProductToCartProcessor
84
76
* @param CartHydrator $cartHydrator
85
77
* @param ArrayManager $arrayManager
86
78
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
87
79
* @param CartRepositoryInterface $cartRepository
88
80
* @param GuestCartRepositoryInterface $guestCartRepository
89
- * @param ProductRepositoryInterface $productRepository
90
81
* @param ValueFactory $valueFactory
91
82
* @param UserContextInterface $userContext
92
83
*/
93
84
public function __construct (
94
- DataObjectFactory $ dataObjectFactory ,
85
+ AddSimpleProductToCartProcessor $ addSimpleProductToCartProcessor ,
95
86
CartHydrator $ cartHydrator ,
96
87
ArrayManager $ arrayManager ,
97
88
MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
98
89
CartRepositoryInterface $ cartRepository ,
99
90
GuestCartRepositoryInterface $ guestCartRepository ,
100
- ProductRepositoryInterface $ productRepository ,
101
91
ValueFactory $ valueFactory ,
102
92
UserContextInterface $ userContext
103
93
) {
104
94
$ this ->valueFactory = $ valueFactory ;
105
95
$ this ->userContext = $ userContext ;
106
96
$ this ->arrayManager = $ arrayManager ;
107
- $ this ->productRepository = $ productRepository ;
108
97
$ this ->cartHydrator = $ cartHydrator ;
109
98
$ this ->guestCartRepository = $ guestCartRepository ;
110
- $ this ->dataObjectFactory = $ dataObjectFactory ;
111
99
$ this ->cartRepository = $ cartRepository ;
112
100
$ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
101
+ $ this ->addSimpleProductToCartProcessor = $ addSimpleProductToCartProcessor ;
113
102
}
114
103
115
104
/**
116
105
* Resolve adding simple product to cart for customers/guests
117
- *
118
- * {@inheritDoc}
106
+ * {@inheritdoc}
119
107
*/
120
108
public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null ) : Value
121
109
{
@@ -136,11 +124,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
136
124
137
125
$ cart = $ this ->getCart ((string ) $ cartHash );
138
126
139
- foreach ($ cartItems as $ cartItem ) {
140
- $ sku = $ this ->arrayManager ->get ('details/sku ' , $ cartItem );
141
- $ product = $ this ->productRepository ->get ($ sku );
127
+ foreach ($ cartItems as $ cartItemData ) {
128
+ $ sku = $ this ->arrayManager ->get ('details/sku ' , $ cartItemData );
142
129
143
- $ message = $ cart -> addProduct ( $ product , $ this -> getBuyRequest ( $ cartItem ) );
130
+ $ message = $ this -> addSimpleProductToCartProcessor -> process ( $ cart , $ cartItemData );
144
131
145
132
if (is_string ($ message )) {
146
133
throw new GraphQlInputException (
@@ -166,33 +153,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
166
153
return $ this ->valueFactory ->create ($ result );
167
154
}
168
155
169
- /**
170
- * Format GraphQl input data to a shape that buy request has
171
- *
172
- * @param array $cartItem
173
- * @return DataObject
174
- */
175
- private function getBuyRequest ($ cartItem ): DataObject
176
- {
177
- $ customOptions = [];
178
- $ qty = $ this ->arrayManager ->get ('details/qty ' , $ cartItem );
179
- $ customizableOptions = $ this ->arrayManager ->get ('customizable_options ' , $ cartItem , []);
180
-
181
- foreach ($ customizableOptions as $ customizableOption ) {
182
- $ customOptions [$ customizableOption ['id ' ]] = $ customizableOption ['value ' ];
183
- }
184
-
185
- return $ this ->dataObjectFactory ->create ([
186
- 'data ' => [
187
- 'qty ' => $ qty ,
188
- 'options ' => $ customOptions
189
- ]
190
- ]);
191
- }
192
-
193
156
/**
194
157
* Collecting cart errors
195
- *
196
158
* @param CartInterface|Quote $cart
197
159
* @return string
198
160
*/
@@ -210,7 +172,6 @@ private function getCartErrors($cart): string
210
172
211
173
/**
212
174
* Retrieving quote mode based on customer authorization
213
- *
214
175
* @param string $cartHash
215
176
* @return CartInterface|Quote
216
177
* @throws NoSuchEntityException
0 commit comments