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
|
|
|
* @SuppressWarnings(PHPMD.ExitExpression) |
12
|
|
|
*/ |
13
|
|
|
class RemServerController implements InjectionAwareInterface |
14
|
|
|
{ |
15
|
|
|
use InjectionAwareTrait; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initiate the REM Server. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function anyPrepare() |
25
|
|
|
{ |
26
|
|
|
$rem = $this->di->get("rem"); |
27
|
|
|
|
28
|
|
|
if (!$rem->hasDataset()) { |
29
|
|
|
$rem->init(); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Init or re-init the REM Server. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function anyInit() |
41
|
|
|
{ |
42
|
|
|
$this->di->get("rem")->init(); |
43
|
|
|
$this->di->get("response")->sendJson(["message" => "The session is initiated with the default dataset."]); |
44
|
|
|
exit; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Destroy the session to ease testing. |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function anyDestroy() |
55
|
|
|
{ |
56
|
|
|
$this->di->get("session")->destroy(); |
57
|
|
|
$this->di->get("response")->sendJson(["message" => "The session was destroyed."]); |
58
|
|
|
exit; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the dataset or parts of it. |
65
|
|
|
* |
66
|
|
|
* @param string $key for the dataset |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function getDataset($key) |
71
|
|
|
{ |
72
|
|
|
$request = $this->di->get("request"); |
73
|
|
|
|
74
|
|
|
$dataset = $this->di->get("rem")->getDataset($key); |
75
|
|
|
$offset = $request->getGet("offset", 0); |
76
|
|
|
$limit = $request->getGet("limit", 25); |
77
|
|
|
$res = [ |
78
|
|
|
"data" => array_slice($dataset, $offset, $limit), |
79
|
|
|
"offset" => $offset, |
80
|
|
|
"limit" => $limit, |
81
|
|
|
"total" => count($dataset) |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
$this->di->get("response")->sendJson($res); |
85
|
|
|
exit; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get one item from the dataset. |
92
|
|
|
* |
93
|
|
|
* @param string $key for the dataset |
94
|
|
|
* @param string $itemId for the item to get |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function getItem($key, $itemId) |
99
|
|
|
{ |
100
|
|
|
$response = $this->di->get("response"); |
101
|
|
|
|
102
|
|
|
$item = $this->di->get("rem")->getItem($key, $itemId); |
103
|
|
|
if (!$item) { |
104
|
|
|
$response->sendJson(["message" => "The item is not found."]); |
105
|
|
|
exit; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$response->sendJson($item); |
109
|
|
|
exit; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create a new item by getting the entry from the request body and add |
116
|
|
|
* to the dataset. |
117
|
|
|
* |
118
|
|
|
* @param string $key for the dataset |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function postItem($key) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$entry = $this->getRequestBody(); |
125
|
|
|
$item = $this->di->get("rem")->addItem($key, $entry); |
126
|
|
|
$this->di->get("response")->sendJson($item); |
127
|
|
|
exit; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Upsert/replace an item in the dataset, entry is taken from request body. |
133
|
|
|
* |
134
|
|
|
* @param string $key for the dataset |
135
|
|
|
* @param string $itemId where to save the entry |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
public function putItem($key, $itemId) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$entry = $this->getRequestBody(); |
142
|
|
|
$item = $this->di->get("rem")->upsertItem($key, $itemId, $entry); |
143
|
|
|
$this->di->get("response")->sendJson($item); |
144
|
|
|
exit; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Delete an item from the dataset. |
151
|
|
|
* |
152
|
|
|
* @param string $key for the dataset |
153
|
|
|
* @param string $itemId for the item to delete |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function deleteItem($key, $itemId) |
158
|
|
|
{ |
159
|
|
|
$this->di->get("rem")->deleteItem($key, $itemId); |
160
|
|
|
$this->di->get("response")->sendJson(null); |
161
|
|
|
exit; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Show a message that the route is unsupported, a local 404. |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function anyUnsupported() |
172
|
|
|
{ |
173
|
|
|
$this->di->get("response")->sendJson(["message" => "404. The api/ does not support that."], 404); |
174
|
|
|
exit; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the request body from the HTTP request and treat it as |
181
|
|
|
* JSON data. |
182
|
|
|
* |
183
|
|
|
* @return mixed as the JSON converted content. |
184
|
|
|
*/ |
185
|
|
|
protected function getRequestBody() |
186
|
|
|
{ |
187
|
|
|
$entry = $this->di->get("request")->getBody(); |
188
|
|
|
$entry = json_decode($entry, true); |
189
|
|
|
|
190
|
|
|
if (is_null($entry)) { |
191
|
|
|
$this->di->get("response")->sendJson(["message" => "500. Could not read HTTP request body as JSON."], 500); |
192
|
|
|
exit; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
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.