1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Api\V1; |
4
|
|
|
|
5
|
|
|
use App\Interfaces\Controllers\IApi; |
6
|
|
|
use App\Reuse\Controllers\AbstractApi; |
7
|
|
|
use App\Component\Http\Response; |
8
|
|
|
use App\Component\Container; |
9
|
|
|
use App\Model\Repository\Users; |
10
|
|
|
use OpenApi\Annotations as OA; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @OA\Info( |
14
|
|
|
* version="1.0", |
15
|
|
|
* title="Api Restful Controller" |
16
|
|
|
* ) |
17
|
|
|
*/ |
18
|
|
|
final class Restful extends AbstractApi implements IApi |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* user repository |
23
|
|
|
* |
24
|
|
|
* @var Users |
25
|
|
|
*/ |
26
|
|
|
protected $userRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* slugs |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $slugs; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* sql |
37
|
|
|
* |
38
|
|
|
* @var String |
39
|
|
|
*/ |
40
|
|
|
protected $sql; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* sql values to bind statement |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $bindValues; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* error |
51
|
|
|
* |
52
|
|
|
* @var Boolean |
53
|
|
|
*/ |
54
|
|
|
protected $error; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* error message |
58
|
|
|
* |
59
|
|
|
* @var String |
60
|
|
|
*/ |
61
|
|
|
protected $errorMessage; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* instanciate |
65
|
|
|
* |
66
|
|
|
* @param Container $container |
67
|
|
|
*/ |
68
|
11 |
|
public function __construct(Container $container) |
69
|
|
|
{ |
70
|
11 |
|
$this->userRepository = new Users($container); |
71
|
11 |
|
$this->error = false; |
72
|
11 |
|
$this->errorMessage = ''; |
73
|
11 |
|
parent::__construct($container); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* index |
78
|
|
|
* |
79
|
|
|
* @OA\Get( |
80
|
|
|
* path="/api/v1/restful/index", |
81
|
|
|
* summary="Search for a something item", |
82
|
|
|
* @OA\RequestBody( |
83
|
|
|
* @OA\MediaType( |
84
|
|
|
* mediaType="application/json", |
85
|
|
|
* @OA\Schema( |
86
|
|
|
* @OA\Property( |
87
|
|
|
* property="id", |
88
|
|
|
* type="string" |
89
|
|
|
* ), |
90
|
|
|
* @OA\Property( |
91
|
|
|
* property="name", |
92
|
|
|
* type="string" |
93
|
|
|
* ), |
94
|
|
|
* example={"id": 10} |
95
|
|
|
* ) |
96
|
|
|
* ) |
97
|
|
|
* ), |
98
|
|
|
* @OA\Response( |
99
|
|
|
* response=200, |
100
|
|
|
* description="OK" |
101
|
|
|
* ) |
102
|
|
|
* ) |
103
|
|
|
* @return Restful |
104
|
|
|
*/ |
105
|
1 |
|
final public function index(array $slugs = []): Restful |
106
|
|
|
{ |
107
|
1 |
|
$this->slugs = $slugs; |
108
|
1 |
|
$this->userRepository->find( |
109
|
1 |
|
['name'], |
110
|
|
|
[ |
111
|
1 |
|
'name' => ['john', 'elisa'], |
112
|
|
|
'job>' => 0 |
113
|
|
|
] |
114
|
|
|
); |
115
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
116
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
117
|
1 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* store |
122
|
|
|
* |
123
|
|
|
* @OA\Post( |
124
|
|
|
* path="/api/v1/restful", |
125
|
|
|
* summary="Adds a new something item", |
126
|
|
|
* @OA\RequestBody( |
127
|
|
|
* @OA\MediaType( |
128
|
|
|
* mediaType="application/json", |
129
|
|
|
* @OA\Schema( |
130
|
|
|
* @OA\Property( |
131
|
|
|
* property="id", |
132
|
|
|
* type="string" |
133
|
|
|
* ), |
134
|
|
|
* @OA\Property( |
135
|
|
|
* property="name", |
136
|
|
|
* type="string" |
137
|
|
|
* ), |
138
|
|
|
* example={"id": 10, "name": "Jessica Smith"} |
139
|
|
|
* ) |
140
|
|
|
* ) |
141
|
|
|
* ), |
142
|
|
|
* @OA\Response( |
143
|
|
|
* response=200, |
144
|
|
|
* description="OK" |
145
|
|
|
* ) |
146
|
|
|
* ) |
147
|
|
|
* @return Restful |
148
|
|
|
*/ |
149
|
2 |
|
final public function store(array $slugs = []): Restful |
150
|
|
|
{ |
151
|
2 |
|
$this->slugs = $slugs; |
152
|
2 |
|
$this->bindValues = []; |
153
|
|
|
try { |
154
|
2 |
|
$this->userRepository->insert($this->getParams()); |
155
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
156
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
157
|
1 |
|
} catch (\Exception $e) { |
158
|
1 |
|
$this->error = true; |
159
|
1 |
|
$this->errorMessage = $e->getMessage(); |
160
|
1 |
|
$this->sql = ''; |
161
|
|
|
} |
162
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* update |
167
|
|
|
* |
168
|
|
|
* @OA\Put( |
169
|
|
|
* path="/api/v1/restful", |
170
|
|
|
* summary="Modify something item", |
171
|
|
|
* @OA\RequestBody( |
172
|
|
|
* @OA\MediaType( |
173
|
|
|
* mediaType="application/json", |
174
|
|
|
* @OA\Schema( |
175
|
|
|
* @OA\Property( |
176
|
|
|
* property="id", |
177
|
|
|
* type="string" |
178
|
|
|
* ), |
179
|
|
|
* @OA\Property( |
180
|
|
|
* property="name", |
181
|
|
|
* type="string" |
182
|
|
|
* ), |
183
|
|
|
* example={"id": 10, "name": "Jessica Smoke"} |
184
|
|
|
* ) |
185
|
|
|
* ) |
186
|
|
|
* ), |
187
|
|
|
* @OA\Response( |
188
|
|
|
* response=200, |
189
|
|
|
* description="OK" |
190
|
|
|
* ) |
191
|
|
|
* ) |
192
|
|
|
* @return Restful |
193
|
|
|
*/ |
194
|
2 |
|
final public function update(array $slugs = []): Restful |
195
|
|
|
{ |
196
|
2 |
|
$this->slugs = $slugs; |
197
|
2 |
|
$this->bindValues = []; |
198
|
|
|
try { |
199
|
2 |
|
$params = $this->getParams(); |
200
|
2 |
|
$pk = $this->userRepository->getPrimary(); |
201
|
2 |
|
if (false === isset($params[$pk])) { |
202
|
1 |
|
throw new \Exception('Missing primary : ' . $pk); |
203
|
|
|
} |
204
|
1 |
|
$pkValue = $params[$pk]; |
205
|
1 |
|
unset($params[$pk]); |
206
|
1 |
|
$this->userRepository->update($params, [$pk => $pkValue]); |
207
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
208
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
209
|
1 |
|
} catch (\Exception $e) { |
210
|
1 |
|
$this->error = true; |
211
|
1 |
|
$this->errorMessage = $e->getMessage(); |
212
|
1 |
|
$this->sql = ''; |
213
|
|
|
} |
214
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* delete |
219
|
|
|
* |
220
|
|
|
* @OA\Delete( |
221
|
|
|
* path="/api/v1/restful", |
222
|
|
|
* summary="Delete something item", |
223
|
|
|
* @OA\RequestBody( |
224
|
|
|
* @OA\MediaType( |
225
|
|
|
* mediaType="application/json", |
226
|
|
|
* @OA\Schema( |
227
|
|
|
* @OA\Property( |
228
|
|
|
* property="id", |
229
|
|
|
* type="string" |
230
|
|
|
* ), |
231
|
|
|
* example={"id": 10} |
232
|
|
|
* ) |
233
|
|
|
* ) |
234
|
|
|
* ), |
235
|
|
|
* @OA\Response( |
236
|
|
|
* response=200, |
237
|
|
|
* description="OK" |
238
|
|
|
* ) |
239
|
|
|
* ) |
240
|
|
|
* @return Restful |
241
|
|
|
*/ |
242
|
2 |
|
final public function delete(array $slugs = []): Restful |
243
|
|
|
{ |
244
|
2 |
|
$this->slugs = $slugs; |
245
|
2 |
|
$this->bindValues = []; |
246
|
|
|
try { |
247
|
2 |
|
$params = $this->getParams(); |
248
|
2 |
|
$pk = $this->userRepository->getPrimary(); |
249
|
2 |
|
if (false === isset($params[$pk])) { |
250
|
1 |
|
throw new \Exception('Missing primary : ' . $pk); |
251
|
|
|
} |
252
|
1 |
|
$this->userRepository->delete([$pk => $params[$pk]]); |
253
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
254
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
255
|
1 |
|
} catch (\Exception $e) { |
256
|
1 |
|
$this->error = true; |
257
|
1 |
|
$this->errorMessage = $e->getMessage(); |
258
|
1 |
|
$this->sql = ''; |
259
|
|
|
} |
260
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* set response with for a classname and action |
265
|
|
|
* |
266
|
|
|
* @param string $classname |
267
|
|
|
* @param string $action |
268
|
|
|
* @return Restful |
269
|
|
|
*/ |
270
|
1 |
|
protected function setResponse(string $classname, string $action): Restful |
271
|
|
|
{ |
272
|
1 |
|
$this->response |
273
|
1 |
|
->setCode($this->getStatusCode()) |
274
|
1 |
|
->setContent( |
275
|
|
|
[ |
276
|
1 |
|
'error' => $this->error, |
277
|
1 |
|
'errorMessage' => $this->errorMessage, |
278
|
|
|
'datas' => [ |
279
|
1 |
|
'method' => $this->getRequest()->getMethod(), |
280
|
1 |
|
'params' => $this->getParams(), |
281
|
1 |
|
'controller' => $classname, |
282
|
1 |
|
'action' => $action, |
283
|
1 |
|
'query' => $this->sql, |
284
|
1 |
|
'queryValues' => $this->bindValues, |
285
|
1 |
|
'slugs' => $this->slugs |
286
|
|
|
] |
287
|
|
|
] |
288
|
|
|
); |
289
|
1 |
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* returns true if error happened |
294
|
|
|
* |
295
|
|
|
* @return boolean |
296
|
|
|
*/ |
297
|
7 |
|
protected function isError(): bool |
298
|
|
|
{ |
299
|
7 |
|
return $this->error === true; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* returns http status code |
304
|
|
|
* |
305
|
|
|
* @return int |
306
|
|
|
*/ |
307
|
7 |
|
protected function getStatusCode(): int |
308
|
|
|
{ |
309
|
7 |
|
return (true === $this->isError()) |
310
|
3 |
|
? Response::HTTP_BAD_REQUEST |
311
|
7 |
|
: Response::HTTP_OK; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|