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\Boilerplate\Api; |
11
|
|
|
|
12
|
|
|
use FAPI\Boilerplate\Exception; |
13
|
|
|
use FAPI\Boilerplate\Exception\Domain as DomainExceptions; |
14
|
|
|
use FAPI\Boilerplate\Exception\InvalidArgumentException; |
15
|
|
|
use FAPI\Boilerplate\Model\Tweet\TweetCreated; |
16
|
|
|
use FAPI\Boilerplate\Model\Tweet\TweetDeleted; |
17
|
|
|
use FAPI\Boilerplate\Model\Tweet\Tweets; |
18
|
|
|
use FAPI\Boilerplate\Model\Tweet\Tweet as TweetModel; |
19
|
|
|
use FAPI\Boilerplate\Model\Tweet\TweetUpdated; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class Tweet extends HttpApi |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param array $params |
29
|
|
|
* |
30
|
|
|
* @return Tweets|ResponseInterface |
31
|
|
|
* |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function index(array $params = []) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$response = $this->httpGet('/v1/tweets', $params); |
37
|
|
|
|
38
|
|
|
if (!$this->hydrator) { |
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Use any valid status code here |
43
|
|
|
if ($response->getStatusCode() !== 200) { |
44
|
|
|
$this->handleErrors($response); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->hydrator->hydrate($response, Tweets::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param int $id |
52
|
|
|
* |
53
|
|
|
* @return TweetModel|ResponseInterface |
54
|
|
|
* |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function get(int $id) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if (empty($id)) { |
60
|
|
|
throw new InvalidArgumentException('Id cannot be empty'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$response = $this->httpGet(sprintf('/v1/tweets/%d', $id)); |
64
|
|
|
|
65
|
|
|
if (!$this->hydrator) { |
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Use any valid status code here |
70
|
|
|
if ($response->getStatusCode() !== 200) { |
71
|
|
|
$this->handleErrors($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->hydrator->hydrate($response, TweetModel::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $message |
79
|
|
|
* @param string $location |
80
|
|
|
* @param array $hashtags |
81
|
|
|
* |
82
|
|
|
* @return TweetCreated|ResponseInterface |
83
|
|
|
* |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function create(string $message, string $location, array $hashtags = []) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (empty($message)) { |
89
|
|
|
throw new InvalidArgumentException('Message cannot be empty'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (empty($location)) { |
93
|
|
|
throw new InvalidArgumentException('Location cannot be empty'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$params = [ |
97
|
|
|
'message' => $message, |
98
|
|
|
'location' => $location, |
99
|
|
|
'hashtags' => $hashtags, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
$response = $this->httpPost('/v1/tweets', $params); |
103
|
|
|
|
104
|
|
|
if (!$this->hydrator) { |
105
|
|
|
return $response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Use any valid status code here |
109
|
|
|
if ($response->getStatusCode() !== 201) { |
110
|
|
|
switch ($response->getStatusCode()) { |
111
|
|
|
case 400: |
112
|
|
|
throw new DomainExceptions\ValidationException(); |
113
|
|
|
break; |
|
|
|
|
114
|
|
|
|
115
|
|
|
default: |
116
|
|
|
$this->handleErrors($response); |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->hydrator->hydrate($response, TweetCreated::class); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $id |
126
|
|
|
* @param string $message |
127
|
|
|
* @param string $location |
128
|
|
|
* @param array $hashtags |
129
|
|
|
* |
130
|
|
|
* @return TweetUpdated|ResponseInterface |
131
|
|
|
* |
132
|
|
|
* @throws Exception |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function update(int $id, string $message, string $location, array $hashtags = []) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
if (empty($message)) { |
137
|
|
|
throw new InvalidArgumentException('Message cannot be empty'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (empty($location)) { |
141
|
|
|
throw new InvalidArgumentException('Location cannot be empty'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$params = [ |
145
|
|
|
'message' => $message, |
146
|
|
|
'location' => $location, |
147
|
|
|
'hashtags' => $hashtags, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
$response = $this->httpPut(sprintf('/v1/tweets/%d', $id), $params); |
151
|
|
|
|
152
|
|
|
if (!$this->hydrator) { |
153
|
|
|
return $response; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Use any valid status code here |
157
|
|
|
if ($response->getStatusCode() !== 200) { |
158
|
|
|
switch ($response->getStatusCode()) { |
159
|
|
|
case 400: |
160
|
|
|
throw new DomainExceptions\ValidationException(); |
161
|
|
|
break; |
|
|
|
|
162
|
|
|
|
163
|
|
|
default: |
164
|
|
|
$this->handleErrors($response); |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this->hydrator->hydrate($response, TweetUpdated::class); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param int $id |
174
|
|
|
* |
175
|
|
|
* @return TweetDeleted|ResponseInterface |
176
|
|
|
* |
177
|
|
|
* @throws Exception |
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function delete(int $id) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
if (empty($id)) { |
182
|
|
|
throw new InvalidArgumentException('Id cannot be empty'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$response = $this->httpDelete(sprintf('/v1/tweets/%d', $id)); |
186
|
|
|
|
187
|
|
|
if (!$this->hydrator) { |
188
|
|
|
return $response; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Use any valid status code here |
192
|
|
|
if ($response->getStatusCode() !== 200) { |
193
|
|
|
$this->handleErrors($response); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->hydrator->hydrate($response, TweetDeleted::class); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
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.