| Current Path : /home/rtorresani/www/vendor/magento/module-quote-graph-ql/Model/Cart/ |
| Current File : //home/rtorresani/www/vendor/magento/module-quote-graph-ql/Model/Cart/GetCartForUser.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\QuoteGraphQl\Model\Cart;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote;
/**
* Get cart
*/
class GetCartForUser
{
/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;
/**
* @var CartRepositoryInterface
*/
private $cartRepository;
/**
* @var IsActive
*/
private $isActive;
/**
* @var UpdateCartCurrency
*/
private $updateCartCurrency;
/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
* @param IsActive $isActive
* @param UpdateCartCurrency $updateCartCurrency
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository,
IsActive $isActive,
UpdateCartCurrency $updateCartCurrency
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
$this->isActive = $isActive;
$this->updateCartCurrency = $updateCartCurrency;
}
/**
* Get cart for user
*
* @param string $cartHash
* @param int|null $customerId
* @param int $storeId
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws NoSuchEntityException
*/
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
/** @var Quote $cart */
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}
if (false === (bool)$this->isActive->execute($cart)) {
throw new GraphQlNoSuchEntityException(__('The cart isn\'t active.'));
}
$cart = $this->updateCartCurrency->execute($cart, $storeId);
$cartCustomerId = (int)$cart->getCustomerId();
/* Guest cart, allow operations */
if (0 === $cartCustomerId && (null === $customerId || 0 === $customerId)) {
return $cart;
}
if ($cartCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}
return $cart;
}
}