| Current Path : /home/rtorresani/www/vendor/magento/module-quote/Model/Cart/ |
| Current File : //home/rtorresani/www/vendor/magento/module-quote/Model/Cart/AddProductsToCartError.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Quote\Model\Cart;
/**
* Create instances of errors on adding products to cart. Identify error code based on the message
*/
class AddProductsToCartError
{
/**#@+
* Error message codes
*/
private const ERROR_PRODUCT_NOT_FOUND = 'PRODUCT_NOT_FOUND';
private const ERROR_INSUFFICIENT_STOCK = 'INSUFFICIENT_STOCK';
private const ERROR_NOT_SALABLE = 'NOT_SALABLE';
private const ERROR_UNDEFINED = 'UNDEFINED';
/**#@-*/
/**
* List of error messages and codes.
*/
private const MESSAGE_CODES = [
'Could not find a product with SKU' => self::ERROR_PRODUCT_NOT_FOUND,
'The required options you selected are not available' => self::ERROR_NOT_SALABLE,
'Product that you are trying to add is not available.' => self::ERROR_NOT_SALABLE,
'This product is out of stock' => self::ERROR_INSUFFICIENT_STOCK,
'There are no source items' => self::ERROR_NOT_SALABLE,
'The fewest you may purchase is' => self::ERROR_INSUFFICIENT_STOCK,
'The most you may purchase is' => self::ERROR_INSUFFICIENT_STOCK,
'The requested qty is not available' => self::ERROR_INSUFFICIENT_STOCK,
];
/**
* Returns an error object
*
* @param string $message
* @param int $cartItemPosition
* @return Data\Error
*/
public function create(string $message, int $cartItemPosition = 0): Data\Error
{
return new Data\Error(
$message,
$this->getErrorCode($message),
$cartItemPosition
);
}
/**
* Get message error code.
*
* @param string $message
* @return string
*/
private function getErrorCode(string $message): string
{
foreach (self::MESSAGE_CODES as $codeMessage => $code) {
if (false !== stripos($message, $codeMessage)) {
return $code;
}
}
/* If no code was matched, return the default one */
return self::ERROR_UNDEFINED;
}
}