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