1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2013-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @author Michael Heap <[email protected]> |
37
|
|
|
* @copyright 2013-present Mediasift Ltd www.datasift.com |
38
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
39
|
|
|
* @link http://datasift.github.io/storyplayer |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
namespace StoryplayerInternals\SPv2\Modules\RuntimeTable; |
43
|
|
|
|
44
|
|
|
use Prose\Prose; |
45
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
46
|
|
|
use Storyplayer\SPv2\Modules\Log; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ExpectsRuntimeTable |
50
|
|
|
* |
51
|
|
|
* @uses Prose |
52
|
|
|
* @author Michael Heap <[email protected]> |
53
|
|
|
*/ |
54
|
|
|
class ExpectsRuntimeTable extends BaseRuntimeTable |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* hasItem |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* The key to look for inside the tableName table |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function hasItem($key) |
65
|
|
|
{ |
66
|
|
|
// get our table name from the constructor |
67
|
|
|
$tableName = $this->args[0]; |
68
|
|
|
|
69
|
|
|
// what are we doing? |
70
|
|
|
$log = Log::usingLog()->startAction("make sure item '{$key}' exists in the '{$tableName}' table"); |
71
|
|
|
|
72
|
|
|
// get the table config |
73
|
|
|
$tables = $this->getAllTables(); |
74
|
|
|
|
75
|
|
|
// make sure we have our runtime table |
76
|
|
View Code Duplication |
if (!isset($tables->$tableName)) { |
|
|
|
|
77
|
|
|
$msg = "table is empty / does not exist"; |
78
|
|
|
$log->endAction($msg); |
79
|
|
|
|
80
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "{$tableName} table existed", "{$tableName} table does not exist"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// make sure we have our details |
84
|
|
View Code Duplication |
if (!isset($tables->$tableName->$key)) { |
|
|
|
|
85
|
|
|
$msg = "table does not contain item '{$key}'"; |
86
|
|
|
$log->endAction($msg); |
87
|
|
|
|
88
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "{$tableName} table has item '{$key}'", "{$tableName} table has no item '{$key}'"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// all done |
92
|
|
|
$log->endAction(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* doesNotHaveItem |
97
|
|
|
* |
98
|
|
|
* @param string $key |
99
|
|
|
* The key to look for inside the tableName table |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function doesNotHaveItem($key) |
104
|
|
|
{ |
105
|
|
|
// get our table name from the constructor |
106
|
|
|
$tableName = $this->args[0]; |
107
|
|
|
|
108
|
|
|
// what are we doing? |
109
|
|
|
$log = Log::usingLog()->startAction("make sure there is no existing item '{$key}' in '{$tableName}'"); |
110
|
|
|
|
111
|
|
|
// get the table config |
112
|
|
|
$tables = $this->getAllTables(); |
113
|
|
|
|
114
|
|
|
// make sure we have our table |
115
|
|
|
if (!isset($tables->$tableName)) { |
116
|
|
|
$msg = "table is empty / does not exist"; |
117
|
|
|
$log->endAction($msg); |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// make sure we don't have the details |
122
|
|
View Code Duplication |
if (isset($tables->$tableName->$key)) { |
|
|
|
|
123
|
|
|
$msg = "table already contains no item '{$key}'"; |
124
|
|
|
$log->endAction($msg); |
125
|
|
|
|
126
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "{$tableName} table has no item '{$key}'", "{$tableName} table has item '{$key}'"); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// all done |
130
|
|
|
$log->endAction(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* exists |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function exists() |
139
|
|
|
{ |
140
|
|
|
// get our table name from the constructor |
141
|
|
|
$tableName = $this->args[0]; |
142
|
|
|
|
143
|
|
|
// what are we doing? |
144
|
|
|
$log = Log::usingLog()->startAction("make sure runtime table '{$tableName}' exists"); |
145
|
|
|
|
146
|
|
|
// get the table config |
147
|
|
|
$tables = $this->getAllTables(); |
148
|
|
|
|
149
|
|
|
// make sure we have the named table |
150
|
|
|
if (!isset($tables->$tableName)) { |
151
|
|
|
$log->endAction("table does not exist"); |
152
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "runtime table '{$tableName}' exists", "runtime table '{$tableName}' does not exist"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// all done |
156
|
|
|
$log->endAction(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* doesNotExist |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function doesNotExist() |
165
|
|
|
{ |
166
|
|
|
// get our table name from the constructor |
167
|
|
|
$tableName = $this->args[0]; |
168
|
|
|
|
169
|
|
|
// what are we doing? |
170
|
|
|
$log = Log::usingLog()->startAction("make sure runtime table '{$tableName}' does not exist"); |
171
|
|
|
|
172
|
|
|
// get the table config |
173
|
|
|
$tables = $this->getAllTables(); |
174
|
|
|
|
175
|
|
|
// make sure we do not have the named table |
176
|
|
|
if (isset($tables->$tableName)) { |
177
|
|
|
$log->endAction("table exists"); |
178
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "runtime table '{$tableName}' does not exist", "runtime table '{$tableName}' exists"); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// all done |
182
|
|
|
$log->endAction(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.