77
88namespace Magento \Quote \Model \Cart ;
99
10- use Magento \Catalog \Api \ProductRepositoryInterface ;
11- use Magento \Framework \App \ObjectManager ;
1210use Magento \Framework \Exception \NoSuchEntityException ;
1311use Magento \Quote \Api \CartRepositoryInterface ;
1412use Magento \Quote \Model \Cart \BuyRequest \BuyRequestBuilder ;
2321 */
2422class AddProductsToCart
2523{
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-
4924 /**
5025 * @var CartRepositoryInterface
5126 */
@@ -67,25 +42,29 @@ class AddProductsToCart
6742 private $ productReader ;
6843
6944 /**
70- * @param ProductRepositoryInterface $productRepository
45+ * @var AddProductsToCartError
46+ */
47+ private $ error ;
48+
49+ /**
7150 * @param CartRepositoryInterface $cartRepository
7251 * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
7352 * @param BuyRequestBuilder $requestBuilder
74- * @param ProductReaderInterface|null $productReader
75- *
76- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
53+ * @param ProductReaderInterface $productReader
54+ * @param AddProductsToCartError $addProductsToCartError
7755 */
7856 public function __construct (
79- ProductRepositoryInterface $ productRepository ,
8057 CartRepositoryInterface $ cartRepository ,
8158 MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
8259 BuyRequestBuilder $ requestBuilder ,
83- ProductReaderInterface $ productReader = null
60+ ProductReaderInterface $ productReader ,
61+ AddProductsToCartError $ addProductsToCartError
8462 ) {
8563 $ this ->cartRepository = $ cartRepository ;
8664 $ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
8765 $ this ->requestBuilder = $ requestBuilder ;
88- $ this ->productReader = $ productReader ?: ObjectManager::getInstance ()->get (ProductReaderInterface::class);
66+ $ this ->productReader = $ productReader ;
67+ $ this ->error = $ addProductsToCartError ;
8968 }
9069
9170 /**
@@ -106,7 +85,7 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
10685
10786 /** @var MessageInterface $error */
10887 foreach ($ errors as $ error ) {
109- $ allErrors [] = $ this ->createError ($ error ->getText ());
88+ $ allErrors [] = $ this ->error -> create ($ error ->getText ());
11089 }
11190 }
11291
@@ -181,22 +160,22 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
181160 $ result = null ;
182161
183162 if ($ cartItem ->getQuantity () <= 0 ) {
184- $ errors [] = $ this ->createError (
163+ $ errors [] = $ this ->error -> create (
185164 __ ('The product quantity should be greater than 0 ' )->render (),
186165 $ cartItemPosition
187166 );
188167 } else {
189168 $ product = $ this ->productReader ->getProductBySku ($ sku );
190169 if (!$ product || !$ product ->isSaleable () || !$ product ->isAvailable ()) {
191- $ errors [] = $ this ->createError (
170+ $ errors [] = $ this ->error -> create (
192171 __ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
193172 $ cartItemPosition
194173 );
195174 } else {
196175 try {
197176 $ result = $ cart ->addProduct ($ product , $ this ->requestBuilder ->build ($ cartItem ));
198177 } catch (\Throwable $ e ) {
199- $ errors [] = $ this ->createError (
178+ $ errors [] = $ this ->error -> create (
200179 __ ($ e ->getMessage ())->render (),
201180 $ cartItemPosition
202181 );
@@ -205,50 +184,14 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
205184
206185 if (is_string ($ result )) {
207186 foreach (array_unique (explode ("\n" , $ result )) as $ error ) {
208- $ errors [] = $ this ->createError (__ ($ error )->render (), $ cartItemPosition );
187+ $ errors [] = $ this ->error -> create (__ ($ error )->render (), $ cartItemPosition );
209188 }
210189 }
211190 }
212191
213192 return $ errors ;
214193 }
215194
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-
252195 /**
253196 * Creates a new output from existing errors
254197 *
0 commit comments