This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
GetCartForUser.php
106 lines (94 loc) · 3.15 KB
/
GetCartForUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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\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;
/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
}
/**
* Get cart for user
*
* @param string $cartHash
* @param int|null $customerId
* @param int $storeId
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
* @throws NoSuchEntityException
*/
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}
try {
/** @var Quote $cart */
$cart = $this->cartRepository->get($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
);
}
if (false === (bool)$cart->getIsActive()) {
throw new GraphQlNoSuchEntityException(
__('Current user does not have an active cart.')
);
}
if ((int)$cart->getStoreId() !== $storeId) {
throw new GraphQlNoSuchEntityException(
__(
'Wrong store code specified for cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}
$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;
}
}