1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\Sylius\Api; |
11
|
|
|
|
12
|
|
|
use FAPI\Sylius\Exception; |
13
|
|
|
use FAPI\Sylius\Exception\Domain as DomainExceptions; |
14
|
|
|
use FAPI\Sylius\Exception\InvalidArgumentException; |
15
|
|
|
use FAPI\Sylius\Model\Checkout\PaymentCollection; |
16
|
|
|
use FAPI\Sylius\Model\Checkout\ShipmentCollection; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Kasim Taskin <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class Checkout extends HttpApi |
23
|
|
|
{ |
24
|
|
|
const SHIPPING_ADDRESS_FIELDS = [ |
25
|
|
|
'firstName', |
26
|
|
|
'lastName', |
27
|
|
|
'city', |
28
|
|
|
'postcode', |
29
|
|
|
'street', |
30
|
|
|
'countryCode', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws Exception |
35
|
|
|
* |
36
|
|
|
* @return ResponseInterface|void |
37
|
|
|
*/ |
38
|
|
|
public function updateAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = []) |
39
|
|
|
{ |
40
|
|
|
if (empty($cartId)) { |
41
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (empty($shippingAddress)) { |
45
|
|
|
throw new InvalidArgumentException('Shipping address cannot be empty'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach (self::SHIPPING_ADDRESS_FIELDS as $field) { |
49
|
|
|
if (empty($shippingAddress[$field])) { |
50
|
|
|
throw new InvalidArgumentException("Field {$field} missing in shipping address"); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$params = [ |
55
|
|
|
'shippingAddress' => $shippingAddress, |
56
|
|
|
'differentBillingAddress' => $differentBillingAddress, |
57
|
|
|
'billingAddress' => $billingAddress, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$response = $this->httpPut("/api/v1/checkouts/addressing/{$cartId}", $params); |
61
|
|
|
if (!$this->hydrator) { |
62
|
|
|
return $response; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Use any valid status code here |
66
|
|
|
if (204 !== $response->getStatusCode()) { |
67
|
|
|
switch ($response->getStatusCode()) { |
68
|
|
|
case 400: |
69
|
|
|
throw new DomainExceptions\ValidationException(); |
70
|
|
|
break; |
|
|
|
|
71
|
|
|
default: |
72
|
|
|
$this->handleErrors($response); |
73
|
|
|
|
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws Exception |
81
|
|
|
* |
82
|
|
|
* @return ResponseInterface|void |
83
|
|
|
*/ |
84
|
|
|
public function updatePaymentMethod(int $cartId, string $paymentMethodCode) |
85
|
|
|
{ |
86
|
|
|
if (empty($cartId)) { |
87
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (empty($paymentMethodCode)) { |
91
|
|
|
throw new InvalidArgumentException('Payment method code cannot be empty'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$params = [ |
95
|
|
|
'payments' => [ |
96
|
|
|
[ |
97
|
|
|
'method' => $paymentMethodCode, |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$response = $this->httpPut("/api/v1/checkouts/select-payment/{$cartId}", $params); |
103
|
|
|
if (!$this->hydrator) { |
104
|
|
|
return $response; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Use any valid status code here |
108
|
|
|
if (204 !== $response->getStatusCode()) { |
109
|
|
|
switch ($response->getStatusCode()) { |
110
|
|
|
case 400: |
111
|
|
|
throw new DomainExceptions\ValidationException(); |
112
|
|
|
break; |
|
|
|
|
113
|
|
|
default: |
114
|
|
|
$this->handleErrors($response); |
115
|
|
|
|
116
|
|
|
break; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @throws Exception |
123
|
|
|
* |
124
|
|
|
* @return ResponseInterface|void |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function complete(int $cartId) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if (empty($cartId)) { |
129
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$response = $this->httpPut("/api/v1/checkouts/complete/{$cartId}"); |
133
|
|
|
if (!$this->hydrator) { |
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Use any valid status code here |
138
|
|
|
if (204 !== $response->getStatusCode()) { |
139
|
|
|
switch ($response->getStatusCode()) { |
140
|
|
|
case 400: |
141
|
|
|
throw new DomainExceptions\ValidationException(); |
142
|
|
|
break; |
|
|
|
|
143
|
|
|
default: |
144
|
|
|
$this->handleErrors($response); |
145
|
|
|
|
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @throws Exception |
153
|
|
|
* |
154
|
|
|
* @return ResponseInterface|ShipmentCollection |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
public function getShippingMethods(int $cartId) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
if (empty($cartId)) { |
159
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$response = $this->httpGet("/api/v1/checkouts/select-shipping/{$cartId}"); |
163
|
|
|
if (!$this->hydrator) { |
164
|
|
|
return $response; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Use any valid status code here |
168
|
|
|
if (200 !== $response->getStatusCode()) { |
169
|
|
|
switch ($response->getStatusCode()) { |
170
|
|
|
case 400: |
171
|
|
|
throw new DomainExceptions\ValidationException(); |
172
|
|
|
break; |
|
|
|
|
173
|
|
|
default: |
174
|
|
|
$this->handleErrors($response); |
175
|
|
|
|
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->hydrator->hydrate($response, ShipmentCollection::class); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @throws Exception |
185
|
|
|
* |
186
|
|
|
* @return PaymentCollection|ResponseInterface |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function getPaymentMethods(int $cartId) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
if (empty($cartId)) { |
191
|
|
|
throw new InvalidArgumentException('Cart id cannot be empty'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$response = $this->httpGet("/api/v1/checkouts/select-payment/{$cartId}"); |
195
|
|
|
if (!$this->hydrator) { |
196
|
|
|
return $response; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Use any valid status code here |
200
|
|
|
if (200 !== $response->getStatusCode()) { |
201
|
|
|
switch ($response->getStatusCode()) { |
202
|
|
|
case 400: |
203
|
|
|
throw new DomainExceptions\ValidationException(); |
204
|
|
|
break; |
|
|
|
|
205
|
|
|
default: |
206
|
|
|
$this->handleErrors($response); |
207
|
|
|
|
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $this->hydrator->hydrate($response, PaymentCollection::class); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.