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