7
7
8
8
namespace Magento \QuoteGraphQl \Model \Cart ;
9
9
10
- use Magento \Framework \App \ObjectManager ;
11
10
use Magento \Framework \Exception \NoSuchEntityException ;
12
11
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
13
12
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
13
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15
- use Magento \Quote \Api \CartManagementInterface ;
16
14
use Magento \Quote \Api \CartRepositoryInterface ;
17
15
use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
18
16
use Magento \Quote \Model \Quote ;
19
- use Magento \Store \Api \StoreRepositoryInterface ;
20
17
21
18
/**
22
19
* Get cart
@@ -34,31 +31,31 @@ class GetCartForUser
34
31
private $ cartRepository ;
35
32
36
33
/**
37
- * @var CheckCartCheckoutAllowance
34
+ * @var IsActive
38
35
*/
39
- private $ checkoutAllowance ;
36
+ private $ isActive ;
40
37
41
38
/**
42
- * @var StoreRepositoryInterface
39
+ * @var UpdateCartCurrency
43
40
*/
44
- private $ storeRepository ;
41
+ private $ updateCartCurrency ;
45
42
46
43
/**
47
44
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
48
45
* @param CartRepositoryInterface $cartRepository
49
- * @param CheckCartCheckoutAllowance $checkoutAllowance
50
- * @param StoreRepositoryInterface $storeRepository
46
+ * @param IsActive $isActive
47
+ * @param UpdateCartCurrency $updateCartCurrency
51
48
*/
52
49
public function __construct (
53
50
MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
54
51
CartRepositoryInterface $ cartRepository ,
55
- CheckCartCheckoutAllowance $ checkoutAllowance ,
56
- StoreRepositoryInterface $ storeRepository = null
52
+ IsActive $ isActive ,
53
+ UpdateCartCurrency $ updateCartCurrency
57
54
) {
58
55
$ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
59
56
$ this ->cartRepository = $ cartRepository ;
60
- $ this ->checkoutAllowance = $ checkoutAllowance ;
61
- $ this ->storeRepository = $ storeRepository ?: ObjectManager:: getInstance ()-> get (StoreRepositoryInterface::class) ;
57
+ $ this ->isActive = $ isActive ;
58
+ $ this ->updateCartCurrency = $ updateCartCurrency ;
62
59
}
63
60
64
61
/**
@@ -77,26 +74,19 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
77
74
{
78
75
try {
79
76
$ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ cartHash );
80
- } catch (NoSuchEntityException $ exception ) {
81
- throw new GraphQlNoSuchEntityException (
82
- __ ('Could not find a cart with ID "%masked_cart_id" ' , ['masked_cart_id ' => $ cartHash ])
83
- );
84
- }
85
-
86
- try {
87
77
/** @var Quote $cart */
88
78
$ cart = $ this ->cartRepository ->get ($ cartId );
89
- } catch (NoSuchEntityException $ e ) {
79
+ } catch (NoSuchEntityException $ exception ) {
90
80
throw new GraphQlNoSuchEntityException (
91
81
__ ('Could not find a cart with ID "%masked_cart_id" ' , ['masked_cart_id ' => $ cartHash ])
92
82
);
93
83
}
94
84
95
- if (false === (bool )$ cart -> getIsActive ( )) {
85
+ if (false === (bool )$ this -> isActive -> execute ( $ cart )) {
96
86
throw new GraphQlNoSuchEntityException (__ ('The cart isn \'t active. ' ));
97
87
}
98
88
99
- $ cart = $ this ->updateCartCurrency ($ cart , $ storeId );
89
+ $ cart = $ this ->updateCartCurrency -> execute ($ cart , $ storeId );
100
90
101
91
$ cartCustomerId = (int )$ cart ->getCustomerId ();
102
92
@@ -115,68 +105,4 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
115
105
}
116
106
return $ cart ;
117
107
}
118
-
119
- /**
120
- * Gets the cart for the user validated and configured for guest checkout if applicable
121
- *
122
- * @param string $cartHash
123
- * @param int|null $customerId
124
- * @param int $storeId
125
- * @return Quote
126
- * @throws GraphQlAuthorizationException
127
- * @throws GraphQlInputException
128
- * @throws GraphQlNoSuchEntityException
129
- */
130
- public function getCartForCheckout (string $ cartHash , ?int $ customerId , int $ storeId ): Quote
131
- {
132
- try {
133
- $ cart = $ this ->execute ($ cartHash , $ customerId , $ storeId );
134
- } catch (NoSuchEntityException $ e ) {
135
- throw new GraphQlNoSuchEntityException (__ ($ e ->getMessage ()), $ e );
136
- }
137
- $ this ->checkoutAllowance ->execute ($ cart );
138
-
139
- if ((null === $ customerId || 0 === $ customerId )) {
140
- if (!$ cart ->getCustomerEmail ()) {
141
- throw new GraphQlInputException (__ ("Guest email for cart is missing. " ));
142
- }
143
- $ cart ->setCheckoutMethod (CartManagementInterface::METHOD_GUEST );
144
- }
145
-
146
- return $ cart ;
147
- }
148
-
149
- /**
150
- * Sets cart currency based on specified store.
151
- *
152
- * @param Quote $cart
153
- * @param int $storeId
154
- * @return Quote
155
- * @throws GraphQlInputException
156
- * @throws NoSuchEntityException
157
- */
158
- private function updateCartCurrency (Quote $ cart , int $ storeId ): Quote
159
- {
160
- $ cartStore = $ this ->storeRepository ->getById ($ cart ->getStoreId ());
161
- $ currentCartCurrencyCode = $ cartStore ->getCurrentCurrency ()->getCode ();
162
- if ((int )$ cart ->getStoreId () !== $ storeId ) {
163
- $ newStore = $ this ->storeRepository ->getById ($ storeId );
164
- if ($ cartStore ->getWebsite () !== $ newStore ->getWebsite ()) {
165
- throw new GraphQlInputException (
166
- __ ('Can \'t assign cart to store in different website. ' )
167
- );
168
- }
169
- $ cart ->setStoreId ($ storeId );
170
- $ cart ->setStoreCurrencyCode ($ newStore ->getCurrentCurrency ());
171
- $ cart ->setQuoteCurrencyCode ($ newStore ->getCurrentCurrency ());
172
- } elseif ($ cart ->getQuoteCurrencyCode () !== $ currentCartCurrencyCode ) {
173
- $ cart ->setQuoteCurrencyCode ($ cartStore ->getCurrentCurrency ());
174
- } else {
175
- return $ cart ;
176
- }
177
- $ this ->cartRepository ->save ($ cart );
178
- $ cart = $ this ->cartRepository ->get ($ cart ->getId ());
179
-
180
- return $ cart ;
181
- }
182
108
}
0 commit comments