1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2013-present Mediasift Ltd |
5
|
|
|
* Copyright (c) 2015-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 Stuart Herbert <[email protected]> |
38
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace StoryplayerInternals\SPv2\Modules\RuntimeTable; |
45
|
|
|
|
46
|
|
|
use DataSift\Stone\ObjectLib\BaseObject; |
47
|
|
|
use Prose\Prose; |
48
|
|
|
use Storyplayer\SPv2\Modules\Log; |
49
|
|
|
use StoryplayerInternals\SPv2\Modules\RuntimeTable; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* UsingRuntimeTables |
53
|
|
|
* |
54
|
|
|
* @uses Prose |
55
|
|
|
* @author Stuart Herbert <[email protected]> |
56
|
|
|
*/ |
57
|
|
|
class UsingRuntimeTables extends Prose |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* create a runtime table |
61
|
|
|
* |
62
|
|
|
* if the table already exists, it will NOT be overwritten |
63
|
|
|
* |
64
|
|
|
* @param string $tableName |
65
|
|
|
* the table to create |
66
|
|
|
* @return BaseObject |
67
|
|
|
* the created table, ready for use |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function createTable($tableName) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
// what are we doing? |
72
|
|
|
$log = Log::usingLog()->startAction("create runtime table '{$tableName}'"); |
73
|
|
|
|
74
|
|
|
// get the current runtime tables |
75
|
|
|
$tables = RuntimeTable::fromRuntimeTables()->getAllTables(); |
76
|
|
|
if (!isset($tables->$tableName)) { |
77
|
|
|
$log->writeToLog("table does not exist; creating"); |
78
|
|
|
$tables->$tableName = new BaseObject; |
79
|
|
|
} |
80
|
|
|
else { |
81
|
|
|
$log->writeToLog("table already exists"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// all done |
85
|
|
|
$log->endAction(); |
86
|
|
|
return $tables->$tableName; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* remove a runtime table, even if it has content |
91
|
|
|
* |
92
|
|
|
* @param string $tableName |
93
|
|
|
* the name of the table to use |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function removeTable($tableName) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
// what are we doing? |
99
|
|
|
$log = Log::usingLog()->startAction("remove table '{$tableName}' from the runtime tables"); |
100
|
|
|
|
101
|
|
|
// does the table exist? |
102
|
|
|
if (!RuntimeTable::fromRuntimeTables()->getTableExists($tableName)) { |
103
|
|
|
$log->endAction(); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// get the tables |
108
|
|
|
$tables = RuntimeTable::fromRuntimeTables()->getAllTablesSilently(); |
109
|
|
|
unset($tables->$tableName); |
110
|
|
|
|
111
|
|
|
// all done |
112
|
|
|
$log->endAction("table '{$tableName}' removed"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* remove a table if, and only if, it is empty |
117
|
|
|
* |
118
|
|
|
* @param string $tableName |
119
|
|
|
* the name of the table to remove |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function removeTableIfEmpty($tableName) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
// what are we doing? |
125
|
|
|
$log = Log::usingLog()->startAction("remove table '{$tableName}' from the runtime tables if it is empty"); |
126
|
|
|
|
127
|
|
|
// is the table empty? |
128
|
|
|
if (!RuntimeTable::fromRuntimeTable($tableName)->getIsEmpty()) { |
129
|
|
|
$log->endAction("not removing; table is not empty"); |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// if we get here, the table is empty |
134
|
|
|
$tables = RuntimeTable::fromRuntimeTables()->getAllTablesSilently(); |
135
|
|
|
unset($tables->$tableName); |
136
|
|
|
|
137
|
|
|
// all done |
138
|
|
|
$log->endAction("table '{$tableName}' removed; table was empty"); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.