1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\RemServer; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A controller for the REM Server. |
10
|
|
|
*/ |
11
|
|
|
class RemServerController implements InjectionAwareInterface |
12
|
|
|
{ |
13
|
|
|
use InjectionAwareTrait; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initiate the REM Server. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
1 |
|
public function anyPrepare() |
23
|
|
|
{ |
24
|
1 |
|
$rem = $this->di->get("rem"); |
25
|
|
|
|
26
|
1 |
|
if (!$rem->hasDataset()) { |
27
|
1 |
|
$rem->init(); |
28
|
1 |
|
} |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Init or re-init the REM Server. |
35
|
|
|
* |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
12 |
|
public function anyInit() |
39
|
|
|
{ |
40
|
12 |
|
$this->di->get("rem")->init(); |
41
|
12 |
|
return $this->di->get("response")->sendJson( |
42
|
12 |
|
["message" => "The session is initiated with the default dataset."] |
43
|
12 |
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Destroy the session to ease testing. |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
1 |
|
public function anyDestroy() |
54
|
|
|
{ |
55
|
1 |
|
$this->di->get("session")->destroy(); |
56
|
1 |
|
return $this->di->get("response")->sendJson( |
57
|
1 |
|
["message" => "The session was destroyed."] |
58
|
1 |
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the dataset or parts of it. |
65
|
|
|
* |
66
|
|
|
* @param string $key for the dataset |
67
|
|
|
* |
68
|
|
|
* @return Response |
69
|
|
|
*/ |
70
|
1 |
|
public function getDataset($key) |
71
|
|
|
{ |
72
|
1 |
|
$request = $this->di->get("request"); |
73
|
|
|
|
74
|
1 |
|
$dataset = $this->di->get("rem")->getDataset($key); |
75
|
1 |
|
$offset = $request->getGet("offset", 0); |
76
|
1 |
|
$limit = $request->getGet("limit", 25); |
77
|
|
|
$res = [ |
78
|
1 |
|
"data" => array_slice($dataset, $offset, $limit), |
79
|
1 |
|
"offset" => $offset, |
80
|
1 |
|
"limit" => $limit, |
81
|
1 |
|
"total" => count($dataset) |
82
|
1 |
|
]; |
83
|
|
|
|
84
|
1 |
|
return $this->di->get("response")->sendJson($res); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get one item from the dataset. |
91
|
|
|
* |
92
|
|
|
* @param string $key for the dataset |
93
|
|
|
* @param string $itemId for the item to get |
94
|
|
|
* |
95
|
|
|
* @return Response |
96
|
|
|
*/ |
97
|
2 |
|
public function getItem($key, $itemId) |
98
|
|
|
{ |
99
|
2 |
|
$response = $this->di->get("response"); |
100
|
|
|
|
101
|
2 |
|
$item = $this->di->get("rem")->getItem($key, $itemId); |
102
|
2 |
|
if (!$item) { |
103
|
2 |
|
return $response->sendJson(["message" => "The item is not found."]); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $response->sendJson($item); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create a new item by getting the entry from the request body and add |
113
|
|
|
* to the dataset. |
114
|
|
|
* |
115
|
|
|
* @param string $key for the dataset |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
5 |
View Code Duplication |
public function postItem($key) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
try { |
122
|
5 |
|
$entry = $this->getRequestBody(); |
123
|
2 |
|
$item = $this->di->get("rem")->addItem($key, $entry); |
124
|
5 |
|
} catch (Exception $e) { |
125
|
3 |
|
return $this->di->get("response")->sendJson( |
126
|
3 |
|
["message" => "500. HTTP request body is not an object/array or valid JSON."], |
127
|
|
|
500 |
128
|
3 |
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
return $this->di->get("response")->sendJson($item); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Upsert/replace an item in the dataset, entry is taken from request body. |
137
|
|
|
* |
138
|
|
|
* @param string $key for the dataset |
139
|
|
|
* @param string $itemId where to save the entry |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
2 |
View Code Duplication |
public function putItem($key, $itemId) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
try { |
146
|
2 |
|
$entry = $this->getRequestBody(); |
147
|
1 |
|
$item = $this->di->get("rem")->upsertItem($key, $itemId, $entry); |
148
|
2 |
|
} catch (Exception $e) { |
149
|
1 |
|
return $this->di->get("response")->sendJson( |
150
|
1 |
|
["message" => "500. HTTP request body is not an object/array or valid JSON."], |
151
|
|
|
500 |
152
|
1 |
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $this->di->get("response")->sendJson($item); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Delete an item from the dataset. |
162
|
|
|
* |
163
|
|
|
* @param string $key for the dataset |
164
|
|
|
* @param string $itemId for the item to delete |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
1 |
|
public function deleteItem($key, $itemId) |
169
|
|
|
{ |
170
|
1 |
|
$this->di->get("rem")->deleteItem($key, $itemId); |
171
|
1 |
|
return $this->di->get("response")->sendJson(null); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Show a message that the route is unsupported, a local 404. |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
1 |
|
public function anyUnsupported() |
182
|
|
|
{ |
183
|
1 |
|
return $this->di->get("response")->sendJson( |
184
|
1 |
|
["message" => "404. The api/ does not support that."], |
185
|
|
|
404 |
186
|
1 |
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the request body from the HTTP request and treat it as |
193
|
|
|
* JSON data. |
194
|
|
|
* |
195
|
|
|
* @throws Exception when request body is invalid JSON. |
196
|
|
|
* |
197
|
|
|
* @return mixed as the JSON converted content. |
198
|
|
|
*/ |
199
|
6 |
|
protected function getRequestBody() |
200
|
|
|
{ |
201
|
6 |
|
$entry = $this->di->get("request")->getBody(); |
202
|
6 |
|
$entry = json_decode($entry, true); |
203
|
|
|
|
204
|
6 |
|
if (is_null($entry)) { |
205
|
4 |
|
throw new Exception("Could not read HTTP request body as JSON."); |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
return $entry; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.