| Current Path : /home/rtorresani/www/vendor/magento/module-quote-graph-ql/Model/Resolver/ |
| Current File : //home/rtorresani/www/vendor/magento/module-quote-graph-ql/Model/Resolver/RemoveItemFromCart.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\QuoteGraphQl\Model\Resolver;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
/**
* @inheritdoc
*/
class RemoveItemFromCart implements ResolverInterface
{
/**
* @var GetCartForUser
*/
private $getCartForUser;
/**
* @var CartItemRepositoryInterface
*/
private $cartItemRepository;
/**
* @var MaskedQuoteIdToQuoteId
*/
private $maskedQuoteIdToQuoteId;
/**
* @var ArgumentsProcessorInterface
*/
private $argsSelection;
/**
* @param GetCartForUser $getCartForUser
* @param CartItemRepositoryInterface $cartItemRepository
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
* @param ArgumentsProcessorInterface $argsSelection
*/
public function __construct(
GetCartForUser $getCartForUser,
CartItemRepositoryInterface $cartItemRepository,
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
ArgumentsProcessorInterface $argsSelection
) {
$this->getCartForUser = $getCartForUser;
$this->cartItemRepository = $cartItemRepository;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->argsSelection = $argsSelection;
}
/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$processedArgs = $this->argsSelection->process($info->fieldName, $args);
if (empty($processedArgs['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
}
$maskedCartId = $processedArgs['input']['cart_id'];
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
);
}
if (empty($processedArgs['input']['cart_item_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing.'));
}
$itemId = $processedArgs['input']['cart_item_id'];
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
/** Check if the current user is allowed to perform actions with the cart */
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
try {
$this->cartItemRepository->deleteById($cartId, $itemId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__('The cart doesn\'t contain the item'));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
return [
'cart' => [
'model' => $cart,
],
];
}
}