7
7
8
8
namespace Magento \Quote \Model \Cart ;
9
9
10
- use Magento \Catalog \Api \ProductRepositoryInterface ;
11
- use Magento \Framework \App \ObjectManager ;
12
10
use Magento \Framework \Exception \NoSuchEntityException ;
13
11
use Magento \Quote \Api \CartRepositoryInterface ;
14
12
use Magento \Quote \Model \Cart \BuyRequest \BuyRequestBuilder ;
23
21
*/
24
22
class AddProductsToCart
25
23
{
26
- /**#@+
27
- * Error message codes
28
- */
29
- private const ERROR_PRODUCT_NOT_FOUND = 'PRODUCT_NOT_FOUND ' ;
30
- private const ERROR_INSUFFICIENT_STOCK = 'INSUFFICIENT_STOCK ' ;
31
- private const ERROR_NOT_SALABLE = 'NOT_SALABLE ' ;
32
- private const ERROR_UNDEFINED = 'UNDEFINED ' ;
33
- /**#@-*/
34
-
35
- /**
36
- * List of error messages and codes.
37
- */
38
- private const MESSAGE_CODES = [
39
- 'Could not find a product with SKU ' => self ::ERROR_PRODUCT_NOT_FOUND ,
40
- 'The required options you selected are not available ' => self ::ERROR_NOT_SALABLE ,
41
- 'Product that you are trying to add is not available. ' => self ::ERROR_NOT_SALABLE ,
42
- 'This product is out of stock ' => self ::ERROR_INSUFFICIENT_STOCK ,
43
- 'There are no source items ' => self ::ERROR_NOT_SALABLE ,
44
- 'The fewest you may purchase is ' => self ::ERROR_INSUFFICIENT_STOCK ,
45
- 'The most you may purchase is ' => self ::ERROR_INSUFFICIENT_STOCK ,
46
- 'The requested qty is not available ' => self ::ERROR_INSUFFICIENT_STOCK ,
47
- ];
48
-
49
24
/**
50
25
* @var CartRepositoryInterface
51
26
*/
@@ -67,25 +42,29 @@ class AddProductsToCart
67
42
private $ productReader ;
68
43
69
44
/**
70
- * @param ProductRepositoryInterface $productRepository
45
+ * @var AddProductsToCartError
46
+ */
47
+ private $ error ;
48
+
49
+ /**
71
50
* @param CartRepositoryInterface $cartRepository
72
51
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
73
52
* @param BuyRequestBuilder $requestBuilder
74
- * @param ProductReaderInterface|null $productReader
75
- *
76
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
53
+ * @param ProductReaderInterface $productReader
54
+ * @param AddProductsToCartError $addProductsToCartError
77
55
*/
78
56
public function __construct (
79
- ProductRepositoryInterface $ productRepository ,
80
57
CartRepositoryInterface $ cartRepository ,
81
58
MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
82
59
BuyRequestBuilder $ requestBuilder ,
83
- ProductReaderInterface $ productReader = null
60
+ ProductReaderInterface $ productReader ,
61
+ AddProductsToCartError $ addProductsToCartError
84
62
) {
85
63
$ this ->cartRepository = $ cartRepository ;
86
64
$ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
87
65
$ this ->requestBuilder = $ requestBuilder ;
88
- $ this ->productReader = $ productReader ?: ObjectManager::getInstance ()->get (ProductReaderInterface::class);
66
+ $ this ->productReader = $ productReader ;
67
+ $ this ->error = $ addProductsToCartError ;
89
68
}
90
69
91
70
/**
@@ -106,7 +85,7 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
106
85
107
86
/** @var MessageInterface $error */
108
87
foreach ($ errors as $ error ) {
109
- $ allErrors [] = $ this ->createError ($ error ->getText ());
88
+ $ allErrors [] = $ this ->error -> create ($ error ->getText ());
110
89
}
111
90
}
112
91
@@ -181,22 +160,22 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
181
160
$ result = null ;
182
161
183
162
if ($ cartItem ->getQuantity () <= 0 ) {
184
- $ errors [] = $ this ->createError (
163
+ $ errors [] = $ this ->error -> create (
185
164
__ ('The product quantity should be greater than 0 ' )->render (),
186
165
$ cartItemPosition
187
166
);
188
167
} else {
189
168
$ product = $ this ->productReader ->getProductBySku ($ sku );
190
169
if (!$ product || !$ product ->isSaleable () || !$ product ->isAvailable ()) {
191
- $ errors [] = $ this ->createError (
170
+ $ errors [] = $ this ->error -> create (
192
171
__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
193
172
$ cartItemPosition
194
173
);
195
174
} else {
196
175
try {
197
176
$ result = $ cart ->addProduct ($ product , $ this ->requestBuilder ->build ($ cartItem ));
198
177
} catch (\Throwable $ e ) {
199
- $ errors [] = $ this ->createError (
178
+ $ errors [] = $ this ->error -> create (
200
179
__ ($ e ->getMessage ())->render (),
201
180
$ cartItemPosition
202
181
);
@@ -205,50 +184,14 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
205
184
206
185
if (is_string ($ result )) {
207
186
foreach (array_unique (explode ("\n" , $ result )) as $ error ) {
208
- $ errors [] = $ this ->createError (__ ($ error )->render (), $ cartItemPosition );
187
+ $ errors [] = $ this ->error -> create (__ ($ error )->render (), $ cartItemPosition );
209
188
}
210
189
}
211
190
}
212
191
213
192
return $ errors ;
214
193
}
215
194
216
- /**
217
- * Returns an error object
218
- *
219
- * @param string $message
220
- * @param int $cartItemPosition
221
- * @return Data\Error
222
- */
223
- private function createError (string $ message , int $ cartItemPosition = 0 ): Data \Error
224
- {
225
- return new Data \Error (
226
- $ message ,
227
- $ this ->getErrorCode ($ message ),
228
- $ cartItemPosition
229
- );
230
- }
231
-
232
- /**
233
- * Get message error code.
234
- *
235
- * TODO: introduce a separate class for getting error code from a message
236
- *
237
- * @param string $message
238
- * @return string
239
- */
240
- private function getErrorCode (string $ message ): string
241
- {
242
- foreach (self ::MESSAGE_CODES as $ codeMessage => $ code ) {
243
- if (false !== stripos ($ message , $ codeMessage )) {
244
- return $ code ;
245
- }
246
- }
247
-
248
- /* If no code was matched, return the default one */
249
- return self ::ERROR_UNDEFINED ;
250
- }
251
-
252
195
/**
253
196
* Creates a new output from existing errors
254
197
*
0 commit comments