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\InvalidArgumentException; |
14
|
|
|
use FAPI\Sylius\Model\Cart\Cart as Model; |
15
|
|
|
use FAPI\Sylius\Model\Cart\CartItem; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kasim Taskin <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class Cart extends HttpApi |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws Exception |
25
|
|
|
* |
26
|
|
|
* @return Model|ResponseInterface |
27
|
|
|
*/ |
28
|
|
|
public function get(int $id) |
29
|
|
|
{ |
30
|
|
|
if (empty($id)) { |
31
|
|
|
throw new InvalidArgumentException('Id cannot be empty'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$response = $this->httpGet('/api/v1/carts/'.$id); |
35
|
|
|
if (!$this->hydrator) { |
36
|
|
|
return $response; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Use any valid status code here |
40
|
|
|
if (200 !== $response->getStatusCode()) { |
41
|
|
|
$this->handleErrors($response); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws Exception |
49
|
|
|
* |
50
|
|
|
* @return Model|ResponseInterface |
51
|
|
|
*/ |
52
|
|
|
public function create(string $customer, string $channel, string $localeCode, array $params = []) |
53
|
|
|
{ |
54
|
|
|
if (empty($customer)) { |
55
|
|
|
throw new InvalidArgumentException('Customers field cannot be empty'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (empty($channel)) { |
59
|
|
|
throw new InvalidArgumentException('Channel cannot be empty'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (empty($localeCode)) { |
63
|
|
|
throw new InvalidArgumentException('Locale code cannot be empty'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$params['customer'] = $customer; |
67
|
|
|
$params['channel'] = $channel; |
68
|
|
|
$params['localeCode'] = $localeCode; |
69
|
|
|
|
70
|
|
|
$response = $this->httpPost('/api/v1/carts/', $params); |
71
|
|
|
if (!$this->hydrator) { |
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Use any valid status code here |
76
|
|
|
if (201 !== $response->getStatusCode()) { |
77
|
|
|
$this->handleErrors($response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws Exception |
85
|
|
|
* |
86
|
|
|
* @return CartItem|ResponseInterface |
87
|
|
|
*/ |
88
|
|
|
public function addItem(int $cartId, string $variant, int $quantity) |
89
|
|
|
{ |
90
|
|
|
if (empty($cartId)) { |
91
|
|
|
throw new InvalidArgumentException('Cart id field cannot be empty'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (empty($variant)) { |
95
|
|
|
throw new InvalidArgumentException('variant cannot be empty'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (empty($quantity)) { |
99
|
|
|
throw new InvalidArgumentException('quantity cannot be empty'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$params = [ |
103
|
|
|
'variant' => $variant, |
104
|
|
|
'quantity' => $quantity, |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
$response = $this->httpPost(\sprintf('/api/v1/carts/%d/items/', $cartId), $params); |
108
|
|
|
if (!$this->hydrator) { |
109
|
|
|
return $response; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Use any valid status code here |
113
|
|
|
if (201 !== $response->getStatusCode()) { |
114
|
|
|
$this->handleErrors($response); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->hydrator->hydrate($response, CartItem::class); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|