1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2013-present Mediasift Ltd |
5
|
|
|
* Copyright (c) 2016-present Ganbaro Digital Ltd |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Redistribution and use in source and binary forms, with or without |
9
|
|
|
* modification, are permitted provided that the following conditions |
10
|
|
|
* are met: |
11
|
|
|
* |
12
|
|
|
* * Redistributions of source code must retain the above copyright |
13
|
|
|
* notice, this list of conditions and the following disclaimer. |
14
|
|
|
* |
15
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
16
|
|
|
* notice, this list of conditions and the following disclaimer in |
17
|
|
|
* the documentation and/or other materials provided with the |
18
|
|
|
* distribution. |
19
|
|
|
* |
20
|
|
|
* * Neither the names of the copyright holders nor the names of his |
21
|
|
|
* contributors may be used to endorse or promote products derived |
22
|
|
|
* from this software without specific prior written permission. |
23
|
|
|
* |
24
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
25
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
26
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
27
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
28
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
29
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
30
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
31
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
32
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
33
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
34
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
35
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
36
|
|
|
* |
37
|
|
|
* @author Michael Heap <[email protected]> |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com |
41
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
42
|
|
|
* @link http://datasift.github.io/storyplayer |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
namespace StoryplayerInternals\SPv2\Modules\RuntimeTable; |
46
|
|
|
|
47
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
48
|
|
|
use Storyplayer\SPv2\Modules\Log; |
49
|
|
|
use StoryplayerInternals\SPv2\Modules\RuntimeTable; |
50
|
|
|
|
51
|
|
|
class FromRuntimeTable extends BaseRuntimeTable |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* get a table from the runtime tables |
55
|
|
|
* |
56
|
|
|
* if the table does not exist, it will be created |
57
|
|
|
* |
58
|
|
|
* @return object |
59
|
|
|
* The table from the config |
60
|
|
|
*/ |
61
|
|
|
public function getTable() |
62
|
|
|
{ |
63
|
|
|
// get our table name from the constructor |
64
|
|
|
$tableName = $this->args[0]; |
65
|
|
|
|
66
|
|
|
// what are we doing? |
67
|
|
|
$log = Log::usingLog()->startAction("get '{$tableName}' table from runtime config"); |
68
|
|
|
|
69
|
|
|
// get the table config |
70
|
|
|
$tables = RuntimeTable::fromRuntimeTables()->getAllTablesSilently(); |
71
|
|
|
|
72
|
|
|
// make sure we have a table |
73
|
|
|
if (!isset($tables->$tableName)){ |
74
|
|
|
Log::usingLog()->writeToLog("table '{$tableName}' does not exist; creating empty table"); |
75
|
|
|
RuntimeTable::usingRuntimeTables()->createTable($tableName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// all done |
79
|
|
|
$log->endAction(); |
80
|
|
|
return $tables->$tableName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* get a table from the runtime config (if it exists) |
85
|
|
|
* |
86
|
|
|
* @return object|null |
87
|
|
|
* The table from the config, or NULL if there is no table |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function getTableIfExists() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// get our table name from the constructor |
92
|
|
|
$tableName = $this->args[0]; |
93
|
|
|
|
94
|
|
|
// what are we doing? |
95
|
|
|
$log = Log::usingLog()->startAction("get '{$tableName}' table from runtime config"); |
96
|
|
|
|
97
|
|
|
// get the table config |
98
|
|
|
$tables = RuntimeTable::fromRuntimeTables()->getAllTablesSilently(); |
99
|
|
|
|
100
|
|
|
// make sure we have a table |
101
|
|
|
if (!isset($tables->$tableName)){ |
102
|
|
|
$log->endAction("table '{$tableName}' does not exist"); |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// all done |
107
|
|
|
$log->endAction(); |
108
|
|
|
return $tables->$tableName; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* get a group from our runtime table |
113
|
|
|
* |
114
|
|
|
* if the group does not exist, it will be created |
115
|
|
|
* |
116
|
|
|
* @param string $group |
117
|
|
|
* the name of the group that we want |
118
|
|
|
* @return BaseObject |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function getGroupFromTable($group) |
121
|
|
|
{ |
122
|
|
|
// get our table name from the constructor |
123
|
|
|
$tableName = $this->args[0]; |
124
|
|
|
|
125
|
|
|
// what are we doing? |
126
|
|
|
$log = Log::usingLog()->startAction("get '{$tableName}->{$group}' table group from runtime config"); |
127
|
|
|
|
128
|
|
|
// get the table |
129
|
|
|
$table = $this->getTable(); |
130
|
|
|
|
131
|
|
|
// make sure we have a group |
132
|
|
|
if (!isset($table->$group)) { |
133
|
|
|
$log->writeToLog("'{$group}' does not exist; creating"); |
134
|
|
|
$table->$group = new BaseObject; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// all done |
138
|
|
|
$log->endAction(); |
139
|
|
|
return $table->$group; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the value of a specific key |
144
|
|
|
* |
145
|
|
|
* @param string $key |
146
|
|
|
* The key to look for inside the tableName table |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
* The value of the key |
150
|
|
|
*/ |
151
|
|
|
public function getItem($key) |
152
|
|
|
{ |
153
|
|
|
// get our table name from the constructor |
154
|
|
|
$tableName = $this->args[0]; |
155
|
|
|
|
156
|
|
|
// what are we doing? |
157
|
|
|
$log = Log::usingLog()->startAction("get details for '{$key}' from {$tableName} table"); |
158
|
|
|
|
159
|
|
|
// get the table |
160
|
|
|
$table = $this->getTableIfExists(); |
161
|
|
|
|
162
|
|
|
// make sure we have a hosts table |
163
|
|
|
if (!$table) { |
164
|
|
|
$msg = "table does not exist"; |
165
|
|
|
$log->endAction($msg); |
166
|
|
|
|
167
|
|
|
return null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// do we have the entry we're looking for? |
171
|
|
View Code Duplication |
if (!isset($table->$key)) { |
|
|
|
|
172
|
|
|
$msg = "table does not contain an entry for '{$key}'"; |
173
|
|
|
$log->endAction($msg); |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// all done |
178
|
|
|
$log->endAction(); |
179
|
|
|
return $table->$key; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the value of a specific key from a group |
184
|
|
|
* |
185
|
|
|
* @param string $key |
186
|
|
|
* The key to look for inside the tableName table |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
* The value of the $key |
190
|
|
|
*/ |
191
|
|
|
public function getItemFromGroup($group, $key) |
192
|
|
|
{ |
193
|
|
|
// get our table name from the constructor |
194
|
|
|
$tableName = $this->args[0]; |
195
|
|
|
|
196
|
|
|
// what are we doing? |
197
|
|
|
$log = Log::usingLog()->startAction("get details for '{$group}->{$key}' from {$tableName} table"); |
198
|
|
|
|
199
|
|
|
// get the table config |
200
|
|
|
$table = $this->getTableIfExists(); |
201
|
|
|
|
202
|
|
|
// make sure we have a table |
203
|
|
|
if (!$table) { |
204
|
|
|
$msg = "table does not exist"; |
205
|
|
|
$log->endAction($msg); |
206
|
|
|
|
207
|
|
|
return null; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// make sure we have the group |
211
|
|
View Code Duplication |
if (!isset($table->$group)) { |
|
|
|
|
212
|
|
|
$msg = "table has no group '{$group}'"; |
213
|
|
|
$log->endAction($msg); |
214
|
|
|
|
215
|
|
|
return null; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// do we have the entry we're looking for? |
219
|
|
View Code Duplication |
if (!isset($table->$group->$key)) { |
|
|
|
|
220
|
|
|
$msg = "table does not contain an entry for '{$group}->{$key}'"; |
221
|
|
|
$log->endAction($msg); |
222
|
|
|
return null; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// all done |
226
|
|
|
$log->endAction(); |
227
|
|
|
return $table->$group->$key; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* is the table empty? |
232
|
|
|
* |
233
|
|
|
* @return boolean |
234
|
|
|
* TRUE if the table is empty |
235
|
|
|
* TRUE if the table does not exist |
236
|
|
|
* FALSE otherwise |
237
|
|
|
*/ |
238
|
|
|
public function getIsEmpty() |
239
|
|
|
{ |
240
|
|
|
// shorthand |
241
|
|
|
$tableName = $this->args[0]; |
242
|
|
|
|
243
|
|
|
// what are we doing? |
244
|
|
|
$log = Log::usingLog()->startAction("is table '{$tableName}' empty?"); |
245
|
|
|
|
246
|
|
|
// get the table |
247
|
|
|
$table = RuntimeTable::fromRuntimeTables()->getTableSilently($tableName); |
248
|
|
|
if ($table === null) { |
249
|
|
|
$log->endAction("table does not exist; reporting that it is empty"); |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// is it empty? |
254
|
|
|
$contents = get_object_vars($table); |
255
|
|
|
$contentCount = count($contents); |
256
|
|
|
if (count($contents) !== 0) { |
257
|
|
|
$log->endAction("table is not empty; has '{$contentCount}' item(s)"); |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
// if we get here, the table is empty |
262
|
|
|
$log->endAction("table is empty"); |
263
|
|
|
return true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* does this table contain a specific item? |
268
|
|
|
* |
269
|
|
|
* @param string $key |
270
|
|
|
* the item that we are looking for |
271
|
|
|
* @return boolean |
272
|
|
|
* TRUE if the table contains $key |
273
|
|
|
* FALSE otherwise |
274
|
|
|
*/ |
275
|
|
|
public function hasItem($key) |
276
|
|
|
{ |
277
|
|
|
// shorthand |
278
|
|
|
$tableName = $this->args[0]; |
279
|
|
|
|
280
|
|
|
// what are we doing? |
281
|
|
|
$log = Log::usingLog()->startAction("does table '{$tableName}' contain item '{$key}'?"); |
282
|
|
|
|
283
|
|
|
// get our table |
284
|
|
|
$table = $this->getTableIfExists(); |
285
|
|
|
if (!$table) { |
286
|
|
|
$log->endAction("table does not exist"); |
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if (!isset($table->$key)) { |
291
|
|
|
$log->endAction("table does not contain item '{$key}'"); |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// if we get here, all is good |
296
|
|
|
$log->endAction("table contains item '{$key}'"); |
297
|
|
|
return true; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* does this table contain a specific group? |
302
|
|
|
* |
303
|
|
|
* @param string $key |
304
|
|
|
* the group that we are looking for |
305
|
|
|
* @return boolean |
306
|
|
|
* TRUE if the table contains $key |
307
|
|
|
* FALSE otherwise |
308
|
|
|
*/ |
309
|
|
|
public function hasGroup($key) |
310
|
|
|
{ |
311
|
|
|
// shorthand |
312
|
|
|
$tableName = $this->args[0]; |
313
|
|
|
|
314
|
|
|
// what are we doing? |
315
|
|
|
$log = Log::usingLog()->startAction("does table '{$tableName}' contain group '{$key}'?"); |
316
|
|
|
|
317
|
|
|
// get our table |
318
|
|
|
$table = $this->getTableIfExists(); |
319
|
|
|
if (!$table) { |
320
|
|
|
$log->endAction("table does not exist"); |
321
|
|
|
return false; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
if (!isset($table->$key)) { |
325
|
|
|
$log->endAction("table does not contain group '{$key}'"); |
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// is it a group? |
330
|
|
|
if (!$table->$key instanceof BaseObject) { |
331
|
|
|
$log->endAction("table contains item '{$key}', but it is not a group"); |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// if we get here, all is good |
336
|
|
|
$log->endAction("table contains group '{$key}'"); |
337
|
|
|
return true; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
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.