1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util; |
4
|
|
|
|
5
|
|
|
use Jabe\Engine\{ |
6
|
|
|
ProcessEngineConfiguration, |
7
|
|
|
ProcessEngineException |
8
|
|
|
}; |
9
|
|
|
use Jabe\Engine\Authorization\AuthorizationInterface; |
10
|
|
|
use Jabe\Engine\Impl\Context\Context; |
11
|
|
|
use Jabe\Engine\Impl\Interceptor\CommandContext; |
12
|
|
|
|
13
|
|
|
class EnsureUtil |
14
|
|
|
{ |
15
|
|
|
public static function ensureNotNull(string $message, string $variableName, $variable = null, ...$variables): void |
16
|
|
|
{ |
17
|
|
|
if ($variable === null) { |
18
|
|
|
throw self::generateException($message, $variableName, "is null"); |
19
|
|
|
} |
20
|
|
|
if (is_array($variables) && count($variables) > 0) { |
21
|
|
|
foreach ($variables as $var) { |
22
|
|
|
if ($var === null) { |
23
|
|
|
throw self::generateException($message, $variableName, "contains null value"); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function ensureNotEmpty(string $message, string $variableName, array $variable = []): void |
30
|
|
|
{ |
31
|
|
|
if (empty($variable)) { |
32
|
|
|
throw self::generateException($message, $variableName, "is empty"); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function ensureEquals(string $message, string $variableName, $obj1, $obj2): void |
37
|
|
|
{ |
38
|
|
|
if ($obj1 != $obj2) { |
39
|
|
|
throw self::generateException($message, $variableName, "value differs from expected"); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function ensurePositive(string $message, string $variableName, $value): void |
44
|
|
|
{ |
45
|
|
|
if ($value <= 0) { |
46
|
|
|
throw self::generateException($message, $variableName, "is not greater than 0"); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function ensureLessThan(string $message, string $variableName, $value1, $value2): void |
51
|
|
|
{ |
52
|
|
|
if ($value1 >= $value2) { |
53
|
|
|
throw self::generateException($message, $variableName, "is not less than" . $value2); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function ensureGreaterThanOrEqual(string $message, string $variableName, $value1, $value2): void |
58
|
|
|
{ |
59
|
|
|
if ($value1 < $value2) { |
60
|
|
|
throw self::generateException($message, $variableName, "is not greater than or equal to " . $value2); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function ensureInstanceOf($instance, string $type): void |
65
|
|
|
{ |
66
|
|
|
if (!($instance instanceof $type)) { |
67
|
|
|
throw new \Exception(sprintf("Object of class %s is not instance of type %s", get_class($instance), $type)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function ensureOnlyOneNotNull(string $message, ...$values): void |
72
|
|
|
{ |
73
|
|
|
$oneNotNull = false; |
74
|
|
|
foreach ($values as $value) { |
75
|
|
|
if ($value != null) { |
76
|
|
|
if ($oneNotNull) { |
77
|
|
|
throw self::generateException($message); |
78
|
|
|
} |
79
|
|
|
$oneNotNull = true; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
if (!$oneNotNull) { |
83
|
|
|
throw self::generateException($message); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function ensureAtLeastOneNotNull(string $message, ...$values): void |
88
|
|
|
{ |
89
|
|
|
foreach ($values as $value) { |
90
|
|
|
if ($value !== null) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
throw self::generateException($message); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function ensureAtLeastOneNotEmpty(string $message, ...$values): void |
98
|
|
|
{ |
99
|
|
|
foreach ($values as $value) { |
100
|
|
|
if (!empty($value)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
throw self::generateException($message); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function ensureNotContainsEmptyString(string $message, string $variableName, array $variable = []): void |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
foreach ($values as $value) { |
|
|
|
|
110
|
|
|
if (empty($value)) { |
111
|
|
|
throw self::generateException($message, $variableName, "contains empty string"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function ensureNotContainsNull(string $message, string $variableName, array $variable = []): void |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
foreach ($values as $value) { |
|
|
|
|
119
|
|
|
if ($value === null) { |
120
|
|
|
throw self::generateException($message, $variableName, "contains null"); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function ensureNumberOfElements(string $message, string $variableName, array $collection, int $elements): void |
126
|
|
|
{ |
127
|
|
|
if (count($collection) != $elements) { |
128
|
|
|
throw self::generateException($message, $variableName, "does not have " . $elements . " elements"); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function ensureValidIndividualResourceId(string $message, string $id): void |
133
|
|
|
{ |
134
|
|
|
self::ensureNotNull($message, "id", $id); |
135
|
|
|
if (AuthorizationInterface::ANY === $id) { |
136
|
|
|
throw self::generateException($message, "id", "cannot be " . AuthorizationInterface::ANY); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function ensureValidIndividualResourceIds(string $message, string $ids): void |
141
|
|
|
{ |
142
|
|
|
self::ensureNotNull($message, "id", $ids); |
143
|
|
|
foreach ($ids as $id) { |
|
|
|
|
144
|
|
|
self::ensureValidIndividualResourceId($message, $id); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public static function ensureWhitelistedResourceId(CommandContext $commandContext, string $resourceType, string $resourceId): void |
149
|
|
|
{ |
150
|
|
|
$resourcePattern = self::determineResourceWhitelistPattern($commandContext->getProcessEngineConfiguration(), $resourceType); |
151
|
|
|
|
152
|
|
|
if (!preg_match($resourcePattern, $resourceId)) { |
153
|
|
|
throw self::generateException($resourceType . " has an invalid id", "'" . $resourceId . "'", "is not a valid resource identifier."); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public static function ensureTrue(string $message, bool $value): void |
158
|
|
|
{ |
159
|
|
|
if (!$value) { |
160
|
|
|
throw new ProcessEngineException($message); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function ensureFalse(string $message, bool $value): void |
165
|
|
|
{ |
166
|
|
|
self::ensureTrue($message, !$value); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public static function ensureActiveCommandContext(string $operation): void |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
if (Context::getCommandContext() == null) { |
172
|
|
|
//throw LOG.notInsideCommandContext(operation); |
173
|
|
|
throw new \Exception("notInsideCommandContext"); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected static function determineResourceWhitelistPattern(ProcessEngineConfiguration $processEngineConfiguration, string $resourceType): string |
178
|
|
|
{ |
179
|
|
|
$resourcePattern = null; |
180
|
|
|
|
181
|
|
|
if ($resourceType == "User") { |
182
|
|
|
$resourcePattern = $processEngineConfiguration->getUserResourceWhitelistPattern(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($resourceType == "Group") { |
186
|
|
|
$resourcePattern = $processEngineConfiguration->getGroupResourceWhitelistPattern(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if ($resourceType == "Tenant") { |
190
|
|
|
$resourcePattern = $processEngineConfiguration->getTenantResourceWhitelistPattern(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (!empty($resourcePattern)) { |
194
|
|
|
return $resourcePattern; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $processEngineConfiguration->getGeneralResourceWhitelistPattern(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
protected static function generateException(string $message, string $variableName = null, string $description = null): \Exception |
201
|
|
|
{ |
202
|
|
|
$formattedMessage = self::formatMessage($message, $variableName, $description); |
203
|
|
|
return new \Exception($formattedMessage); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
protected static function formatMessage(string $message, string $variableName = null, string $description = null): string |
207
|
|
|
{ |
208
|
|
|
if ($variableName !== null && $description !== null) { |
209
|
|
|
return sprintf("%s: %s %s", $message, $variableName, $description); |
210
|
|
|
} |
211
|
|
|
return $message; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.