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
|
|
|
? [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 |
|
$this->userRepository->insert($this->getParams()); |
166
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
167
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
168
|
1 |
|
} catch (\Exception $e) { |
169
|
1 |
|
$this->error = true; |
170
|
1 |
|
$this->errorMessage = $e->getMessage(); |
171
|
1 |
|
$this->sql = ''; |
172
|
|
|
} |
173
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* update |
178
|
|
|
* |
179
|
|
|
* @OA\Put( |
180
|
|
|
* path="/api/v1/restful", |
181
|
|
|
* summary="Modify something item", |
182
|
|
|
* @OA\RequestBody( |
183
|
|
|
* @OA\MediaType( |
184
|
|
|
* mediaType="application/json", |
185
|
|
|
* @OA\Schema( |
186
|
|
|
* @OA\Property( |
187
|
|
|
* property="id", |
188
|
|
|
* type="string" |
189
|
|
|
* ), |
190
|
|
|
* @OA\Property( |
191
|
|
|
* property="name", |
192
|
|
|
* type="string" |
193
|
|
|
* ), |
194
|
|
|
* example={"id": 10, "name": "Jessica Smoke"} |
195
|
|
|
* ) |
196
|
|
|
* ) |
197
|
|
|
* ), |
198
|
|
|
* @OA\Response( |
199
|
|
|
* response=200, |
200
|
|
|
* description="OK" |
201
|
|
|
* ) |
202
|
|
|
* ) |
203
|
|
|
* @return Restful |
204
|
|
|
*/ |
205
|
2 |
|
final public function update(array $slugs = []): Restful |
206
|
|
|
{ |
207
|
2 |
|
$this->slugs = $slugs; |
208
|
2 |
|
$this->bindValues = []; |
209
|
|
|
try { |
210
|
2 |
|
$params = $this->getParams(); |
211
|
2 |
|
$pk = $this->userRepository->getPrimary(); |
212
|
2 |
|
if (false === isset($params[$pk])) { |
213
|
1 |
|
throw new \Exception('Missing primary : ' . $pk); |
214
|
|
|
} |
215
|
1 |
|
$pkValue = $params[$pk]; |
216
|
1 |
|
unset($params[$pk]); |
217
|
1 |
|
$this->userRepository->update($params, [$pk => $pkValue]); |
218
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
219
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
220
|
1 |
|
} catch (\Exception $e) { |
221
|
1 |
|
$this->error = true; |
222
|
1 |
|
$this->errorMessage = $e->getMessage(); |
223
|
1 |
|
$this->sql = ''; |
224
|
|
|
} |
225
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* delete |
230
|
|
|
* |
231
|
|
|
* @OA\Delete( |
232
|
|
|
* path="/api/v1/restful", |
233
|
|
|
* summary="Delete something item", |
234
|
|
|
* @OA\RequestBody( |
235
|
|
|
* @OA\MediaType( |
236
|
|
|
* mediaType="application/json", |
237
|
|
|
* @OA\Schema( |
238
|
|
|
* @OA\Property( |
239
|
|
|
* property="id", |
240
|
|
|
* type="string" |
241
|
|
|
* ), |
242
|
|
|
* example={"id": 10} |
243
|
|
|
* ) |
244
|
|
|
* ) |
245
|
|
|
* ), |
246
|
|
|
* @OA\Response( |
247
|
|
|
* response=200, |
248
|
|
|
* description="OK" |
249
|
|
|
* ) |
250
|
|
|
* ) |
251
|
|
|
* @return Restful |
252
|
|
|
*/ |
253
|
2 |
|
final public function delete(array $slugs = []): Restful |
254
|
|
|
{ |
255
|
2 |
|
$this->slugs = $slugs; |
256
|
2 |
|
$this->bindValues = []; |
257
|
|
|
try { |
258
|
2 |
|
$params = $this->getParams(); |
259
|
2 |
|
$pk = $this->userRepository->getPrimary(); |
260
|
2 |
|
if (false === isset($params[$pk])) { |
261
|
1 |
|
throw new \Exception('Missing primary : ' . $pk); |
262
|
|
|
} |
263
|
1 |
|
$this->userRepository->delete([$pk => $params[$pk]]); |
264
|
1 |
|
$this->sql = $this->userRepository->getSql(); |
265
|
1 |
|
$this->bindValues = $this->userRepository->getBuilderValues(); |
266
|
1 |
|
} catch (\Exception $e) { |
267
|
1 |
|
$this->error = true; |
268
|
1 |
|
$this->errorMessage = $e->getMessage(); |
269
|
1 |
|
$this->sql = ''; |
270
|
|
|
} |
271
|
2 |
|
return $this->setResponse(__CLASS__, __FUNCTION__); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* set response with for a classname and action |
276
|
|
|
* |
277
|
|
|
* @param string $classname |
278
|
|
|
* @param string $action |
279
|
|
|
* @return Restful |
280
|
|
|
*/ |
281
|
1 |
|
protected function setResponse(string $classname, string $action): Restful |
282
|
|
|
{ |
283
|
1 |
|
$this->response |
284
|
1 |
|
->setCode($this->getStatusCode()) |
285
|
1 |
|
->setContent( |
286
|
|
|
[ |
287
|
1 |
|
'error' => $this->error, |
288
|
1 |
|
'errorMessage' => $this->errorMessage, |
289
|
|
|
'datas' => [ |
290
|
1 |
|
'method' => $this->getRequest()->getMethod(), |
291
|
1 |
|
'params' => $this->getParams(), |
292
|
1 |
|
'controller' => $classname, |
293
|
1 |
|
'action' => $action, |
294
|
1 |
|
'query' => $this->sql, |
295
|
1 |
|
'queryValues' => $this->bindValues, |
296
|
1 |
|
'slugs' => $this->slugs, |
297
|
1 |
|
'rowset' => $this->db->getRowset() |
298
|
|
|
] |
299
|
|
|
] |
300
|
|
|
); |
301
|
1 |
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* returns true if error happened |
306
|
|
|
* |
307
|
|
|
* @return boolean |
308
|
|
|
*/ |
309
|
7 |
|
protected function isError(): bool |
310
|
|
|
{ |
311
|
7 |
|
return $this->error === true; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* returns http status code |
316
|
|
|
* |
317
|
|
|
* @return int |
318
|
|
|
*/ |
319
|
7 |
|
protected function getStatusCode(): int |
320
|
|
|
{ |
321
|
7 |
|
return (true === $this->isError()) |
322
|
3 |
|
? Response::HTTP_BAD_REQUEST |
323
|
7 |
|
: Response::HTTP_OK; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|