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\Cart\Cart as Model; |
16
|
|
|
use FAPI\Sylius\Model\Cart\CartItem; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Kasim Taskin <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class Cart extends HttpApi |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param int $id |
26
|
|
|
* |
27
|
|
|
* @throws Exception |
28
|
|
|
* |
29
|
|
|
* @return Cart|ResponseInterface |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
public function get(int $id) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if (empty($id)) { |
34
|
|
|
throw new InvalidArgumentException('Id cannot be empty'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$response = $this->httpGet("/api/v1/carts/{$id}"); |
38
|
|
|
|
39
|
|
|
if (!$this->hydrator) { |
40
|
|
|
return $response; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Use any valid status code here |
44
|
|
|
if (200 !== $response->getStatusCode()) { |
45
|
|
|
$this->handleErrors($response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $message |
|
|
|
|
53
|
|
|
* @param string $location |
|
|
|
|
54
|
|
|
* @param array $hashtags |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @throws Exception |
57
|
|
|
* |
58
|
|
|
* @return Cart|ResponseInterface |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function create(string $customer, string $channel, string $localeCode) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (empty($customer)) { |
63
|
|
|
throw new InvalidArgumentException('Customers field cannot be empty'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (empty($channel)) { |
67
|
|
|
throw new InvalidArgumentException('Channel cannot be empty'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (empty($localeCode)) { |
71
|
|
|
throw new InvalidArgumentException('Locale code cannot be empty'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$params = [ |
75
|
|
|
'customer' => $customer, |
76
|
|
|
'channel' => $channel, |
77
|
|
|
'localeCode' => $localeCode, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
$response = $this->httpPost('/api/v1/carts/', $params); |
81
|
|
|
|
82
|
|
|
if (!$this->hydrator) { |
83
|
|
|
return $response; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Use any valid status code here |
87
|
|
|
if (201 !== $response->getStatusCode()) { |
88
|
|
|
switch ($response->getStatusCode()) { |
89
|
|
|
case 400: |
90
|
|
|
throw new DomainExceptions\ValidationException(); |
91
|
|
|
break; |
|
|
|
|
92
|
|
|
default: |
93
|
|
|
$this->handleErrors($response); |
94
|
|
|
|
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->hydrator->hydrate($response, Model::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $cartId |
104
|
|
|
* @param string $variant |
105
|
|
|
* @param int $quantity |
106
|
|
|
* |
107
|
|
|
* @throws Exception\DomainException |
108
|
|
|
* @throws Exception\Domain\ValidationException |
109
|
|
|
* |
110
|
|
|
* @return CartItem|ResponseInterface |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function addItem(int $cartId, string $variant, int $quantity) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
if (empty($cartId)) { |
115
|
|
|
throw new InvalidArgumentException('Cart id field cannot be empty'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (empty($variant)) { |
119
|
|
|
throw new InvalidArgumentException('variant cannot be empty'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (empty($quantity)) { |
123
|
|
|
throw new InvalidArgumentException('quantity cannot be empty'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$params = [ |
127
|
|
|
'variant' => $variant, |
128
|
|
|
'quantity' => $quantity, |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$response = $this->httpPost("/api/v1/carts/{$cartId}/items/", $params); |
132
|
|
|
|
133
|
|
|
if (!$this->hydrator) { |
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Use any valid status code here |
138
|
|
|
if (201 !== $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
|
|
|
return $this->hydrator->hydrate($response, CartItem::class); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.