|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RestTemplate\Rest; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\Config\Exception\ConfigNotFoundException; |
|
6
|
|
|
use ByJG\Config\Exception\EnvironmentException; |
|
7
|
|
|
use ByJG\Config\Exception\KeyNotFoundException; |
|
8
|
|
|
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException; |
|
9
|
|
|
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException; |
|
10
|
|
|
use ByJG\RestServer\Exception\Error404Exception; |
|
11
|
|
|
use ByJG\RestServer\HttpRequest; |
|
12
|
|
|
use ByJG\RestServer\HttpResponse; |
|
13
|
|
|
use ByJG\Serializer\BinderObject; |
|
14
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
15
|
|
|
use RestTemplate\Model\Dummy; |
|
16
|
|
|
use RestTemplate\Model\DummyHex; |
|
17
|
|
|
use RestTemplate\Repository\DummyHexRepository; |
|
18
|
|
|
use RestTemplate\Repository\DummyRepository; |
|
19
|
|
|
|
|
20
|
|
|
class Sample extends ServiceAbstractBase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Simple ping |
|
24
|
|
|
* |
|
25
|
|
|
* @SWG\Get( |
|
26
|
|
|
* path="/sample/ping", |
|
27
|
|
|
* tags={"sample"}, |
|
28
|
|
|
* @SWG\Response( |
|
29
|
|
|
* response=200, |
|
30
|
|
|
* description="The object", |
|
31
|
|
|
* @SWG\Schema( |
|
32
|
|
|
* required={"result"}, |
|
33
|
|
|
* @SWG\Property(property="result", type="string") |
|
34
|
|
|
* ) |
|
35
|
|
|
* ) |
|
36
|
|
|
* ) |
|
37
|
|
|
* @param HttpResponse $response |
|
38
|
|
|
* @param HttpRequest $request |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getPing($response, $request) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$response->write([ |
|
43
|
|
|
'result' => 'pong' |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Get the rows from the Dummy table (used in the example) |
|
49
|
|
|
* @SWG\Get( |
|
50
|
|
|
* path="/sample/dummy/{field}", |
|
51
|
|
|
* tags={"sample"}, |
|
52
|
|
|
* @SWG\Parameter( |
|
53
|
|
|
* name="field", |
|
54
|
|
|
* in="path", |
|
55
|
|
|
* description="The field content to be returned", |
|
56
|
|
|
* required=true, |
|
57
|
|
|
* type="string" |
|
58
|
|
|
* ), |
|
59
|
|
|
* @SWG\Response( |
|
60
|
|
|
* response=200, |
|
61
|
|
|
* description="The object", |
|
62
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Dummy")) |
|
63
|
|
|
* ), |
|
64
|
|
|
* @SWG\Response( |
|
65
|
|
|
* response=404, |
|
66
|
|
|
* description="Not found", |
|
67
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
68
|
|
|
* ), |
|
69
|
|
|
* @SWG\Response( |
|
70
|
|
|
* response=500, |
|
71
|
|
|
* description="Erro Geral", |
|
72
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
73
|
|
|
* ) |
|
74
|
|
|
* ) |
|
75
|
|
|
* |
|
76
|
|
|
* @param HttpResponse $response |
|
77
|
|
|
* @param HttpRequest $request |
|
78
|
|
|
* @throws ConfigNotFoundException |
|
79
|
|
|
* @throws EnvironmentException |
|
80
|
|
|
* @throws KeyNotFoundException |
|
81
|
|
|
* @throws Error404Exception |
|
82
|
|
|
* @throws InvalidArgumentException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getDummy($response, $request) |
|
85
|
|
|
{ |
|
86
|
|
|
$dummyRepo = new DummyRepository(); |
|
87
|
|
|
$field = $request->get('field'); |
|
88
|
|
|
|
|
89
|
|
|
$result = $dummyRepo->getByField($field); |
|
90
|
|
|
if (empty($result)) { |
|
91
|
|
|
throw new Error404Exception('Pattern not found'); |
|
92
|
|
|
} |
|
93
|
|
|
$response->write( |
|
94
|
|
|
$result |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Save data content in the table Dummy |
|
100
|
|
|
* @SWG\Post( |
|
101
|
|
|
* path="/sample/dummy", |
|
102
|
|
|
* tags={"sample"}, |
|
103
|
|
|
* @SWG\Parameter( |
|
104
|
|
|
* name="body", |
|
105
|
|
|
* in="body", |
|
106
|
|
|
* description="The dummy data", |
|
107
|
|
|
* required=true, |
|
108
|
|
|
* @SWG\Schema(ref="#/definitions/Dummy") |
|
109
|
|
|
* ), |
|
110
|
|
|
* @SWG\Response( |
|
111
|
|
|
* response=200, |
|
112
|
|
|
* description="The object", |
|
113
|
|
|
* ), |
|
114
|
|
|
* @SWG\Response( |
|
115
|
|
|
* response=500, |
|
116
|
|
|
* description="Erro Geral", |
|
117
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
118
|
|
|
* ) |
|
119
|
|
|
* ) |
|
120
|
|
|
* |
|
121
|
|
|
* @param HttpResponse $response |
|
122
|
|
|
* @param HttpRequest $request |
|
123
|
|
|
* @throws ConfigNotFoundException |
|
124
|
|
|
* @throws EnvironmentException |
|
125
|
|
|
* @throws KeyNotFoundException |
|
126
|
|
|
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
|
127
|
|
|
* @throws OrmBeforeInvalidException |
|
128
|
|
|
* @throws OrmInvalidFieldsException |
|
129
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
|
130
|
|
|
* @throws InvalidArgumentException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function postDummy($response, $request) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$model = new Dummy(); |
|
135
|
|
|
$payload = json_decode($request->payload()); |
|
136
|
|
|
BinderObject::bindObject($payload, $model); |
|
137
|
|
|
|
|
138
|
|
|
$dummyRepo = new DummyRepository(); |
|
139
|
|
|
$dummyRepo->save($model); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the rows from the DummyHex table by ID |
|
144
|
|
|
* @SWG\Get( |
|
145
|
|
|
* path="/sample/dummyhex/{id}", |
|
146
|
|
|
* tags={"sample"}, |
|
147
|
|
|
* @SWG\Parameter( |
|
148
|
|
|
* name="id", |
|
149
|
|
|
* in="path", |
|
150
|
|
|
* description="The field content to be returned", |
|
151
|
|
|
* required=true, |
|
152
|
|
|
* type="string" |
|
153
|
|
|
* ), |
|
154
|
|
|
* @SWG\Response( |
|
155
|
|
|
* response=200, |
|
156
|
|
|
* description="The object", |
|
157
|
|
|
* @SWG\Schema(ref="#/definitions/DummyHex") |
|
158
|
|
|
* ), |
|
159
|
|
|
* @SWG\Response( |
|
160
|
|
|
* response=404, |
|
161
|
|
|
* description="Not found", |
|
162
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
163
|
|
|
* ), |
|
164
|
|
|
* @SWG\Response( |
|
165
|
|
|
* response=500, |
|
166
|
|
|
* description="Erro Geral", |
|
167
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
168
|
|
|
* ) |
|
169
|
|
|
* ) |
|
170
|
|
|
* |
|
171
|
|
|
* @param HttpResponse $response |
|
172
|
|
|
* @param HttpRequest $request |
|
173
|
|
|
* @throws ConfigNotFoundException |
|
174
|
|
|
* @throws EnvironmentException |
|
175
|
|
|
* @throws Error404Exception |
|
176
|
|
|
* @throws InvalidArgumentException |
|
177
|
|
|
* @throws KeyNotFoundException |
|
178
|
|
|
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
|
179
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getDummyHex($response, $request) |
|
182
|
|
|
{ |
|
183
|
|
|
$dummyRepo = new DummyHexRepository(); |
|
184
|
|
|
$id = $request->get('id'); |
|
185
|
|
|
|
|
186
|
|
|
$result = $dummyRepo->get($id); |
|
187
|
|
|
if (empty($result)) { |
|
188
|
|
|
throw new Error404Exception('Id not found'); |
|
189
|
|
|
} |
|
190
|
|
|
$response->write( |
|
191
|
|
|
$result |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Save data content in the table Dummy Hex |
|
197
|
|
|
* @SWG\Post( |
|
198
|
|
|
* path="/sample/dummyhex", |
|
199
|
|
|
* tags={"sample"}, |
|
200
|
|
|
* @SWG\Parameter( |
|
201
|
|
|
* name="body", |
|
202
|
|
|
* in="body", |
|
203
|
|
|
* description="The dummy data", |
|
204
|
|
|
* required=true, |
|
205
|
|
|
* @SWG\Schema(ref="#/definitions/DummyHex") |
|
206
|
|
|
* ), |
|
207
|
|
|
* @SWG\Response( |
|
208
|
|
|
* response=200, |
|
209
|
|
|
* description="The object", |
|
210
|
|
|
* @SWG\Schema(ref="#/definitions/DummyHex") |
|
211
|
|
|
* ), |
|
212
|
|
|
* @SWG\Response( |
|
213
|
|
|
* response=500, |
|
214
|
|
|
* description="Erro Geral", |
|
215
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
|
216
|
|
|
* ) |
|
217
|
|
|
* ) |
|
218
|
|
|
* |
|
219
|
|
|
* @param HttpResponse $response |
|
220
|
|
|
* @param HttpRequest $request |
|
221
|
|
|
* @throws ConfigNotFoundException |
|
222
|
|
|
* @throws EnvironmentException |
|
223
|
|
|
* @throws KeyNotFoundException |
|
224
|
|
|
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
|
225
|
|
|
* @throws OrmBeforeInvalidException |
|
226
|
|
|
* @throws OrmInvalidFieldsException |
|
227
|
|
|
* @throws \ByJG\Serializer\Exception\InvalidArgumentException |
|
228
|
|
|
* @throws InvalidArgumentException |
|
229
|
|
|
*/ |
|
230
|
|
|
public function postDummyHex($response, $request) |
|
231
|
|
|
{ |
|
232
|
|
|
$model = new DummyHex(); |
|
233
|
|
|
$payload = json_decode($request->payload()); |
|
234
|
|
|
BinderObject::bindObject($payload, $model); |
|
235
|
|
|
|
|
236
|
|
|
$dummyRepo = new DummyHexRepository(); |
|
237
|
|
|
$dummyRepo->save($model); |
|
238
|
|
|
|
|
239
|
|
|
$model = $dummyRepo->get($model->getId()); |
|
240
|
|
|
|
|
241
|
|
|
$response->write($model); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.