1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Opensoft\Rollout; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Richard Fullmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Feature |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $name; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $groups = array(); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $users = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
private $percentage = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $requestParam; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $data = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $name |
45
|
|
|
* @param string|null $settings |
46
|
|
|
*/ |
47
|
|
|
public function __construct($name, $settings = null) |
48
|
|
|
{ |
49
|
|
|
$this->name = $name; |
50
|
|
|
if ($settings) { |
|
|
|
|
51
|
|
|
$settings = explode('|', $settings); |
52
|
|
|
|
53
|
|
|
if (isset($settings[3])) { |
54
|
|
|
$rawRequestParam = $settings[3]; |
55
|
|
|
$this->requestParam = $rawRequestParam; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
//We can not trust the list function because of backwords compatibility |
59
|
|
|
if (isset($settings[4])) { |
60
|
|
|
$rawData = $settings[4]; |
61
|
|
|
$this->data = !empty($rawData)? json_decode($rawData, true) : array(); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
list($rawPercentage, $rawUsers, $rawGroups) = $settings; |
65
|
|
|
$this->percentage = (int) $rawPercentage; |
66
|
|
|
$this->users = !empty($rawUsers) ? explode(',', $rawUsers) : array(); |
67
|
|
|
$this->groups = !empty($rawGroups) ? explode(',', $rawGroups) : array(); |
68
|
|
|
} else { |
69
|
|
|
$this->clear(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getName() |
77
|
|
|
{ |
78
|
|
|
return $this->name; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param integer $percentage |
83
|
|
|
*/ |
84
|
|
|
public function setPercentage($percentage) |
85
|
|
|
{ |
86
|
|
|
$this->percentage = $percentage; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return integer |
91
|
|
|
*/ |
92
|
|
|
public function getPercentage() |
93
|
|
|
{ |
94
|
|
|
return $this->percentage; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function serialize() |
101
|
|
|
{ |
102
|
|
|
return implode('|', array( |
103
|
|
|
$this->percentage, |
104
|
|
|
implode(',', $this->users), |
105
|
|
|
implode(',', $this->groups), |
106
|
|
|
$this->requestParam, |
107
|
|
|
json_encode($this->data) |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param RolloutUserInterface $user |
113
|
|
|
*/ |
114
|
|
|
public function addUser(RolloutUserInterface $user) |
115
|
|
|
{ |
116
|
|
|
if (!in_array($user->getRolloutIdentifier(), $this->users)) { |
117
|
|
|
$this->users[] = $user->getRolloutIdentifier(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param RolloutUserInterface $user |
123
|
|
|
*/ |
124
|
|
|
public function removeUser(RolloutUserInterface $user) |
125
|
|
|
{ |
126
|
|
|
if (($key = array_search($user->getRolloutIdentifier(), $this->users)) !== false) { |
127
|
|
|
unset($this->users[$key]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getUsers() |
135
|
|
|
{ |
136
|
|
|
return $this->users; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $group |
141
|
|
|
*/ |
142
|
|
|
public function addGroup($group) |
143
|
|
|
{ |
144
|
|
|
if (!in_array($group, $this->groups)) { |
145
|
|
|
$this->groups[] = $group; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $group |
151
|
|
|
*/ |
152
|
|
|
public function removeGroup($group) |
153
|
|
|
{ |
154
|
|
|
if (($key = array_search($group, $this->groups)) !== false) { |
155
|
|
|
unset($this->groups[$key]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function getGroups() |
163
|
|
|
{ |
164
|
|
|
return $this->groups; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string|null |
169
|
|
|
*/ |
170
|
|
|
public function getRequestParam() |
171
|
|
|
{ |
172
|
|
|
return $this->requestParam; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string|null $requestParam |
177
|
|
|
*/ |
178
|
|
|
public function setRequestParam($requestParam) |
179
|
|
|
{ |
180
|
|
|
$this->requestParam = $requestParam; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getData() |
187
|
|
|
{ |
188
|
|
|
return $this->data; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param array $data |
193
|
|
|
*/ |
194
|
|
|
public function setData(array $data) |
195
|
|
|
{ |
196
|
|
|
$this->data = $data; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Clear the feature of all configuration |
201
|
|
|
*/ |
202
|
|
|
public function clear() |
203
|
|
|
{ |
204
|
|
|
$this->groups = array(); |
205
|
|
|
$this->users = array(); |
206
|
|
|
$this->percentage = 0; |
207
|
|
|
$this->requestParam = ''; |
208
|
|
|
$this->data = array(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Is the feature active? |
213
|
|
|
* |
214
|
|
|
* @param Rollout $rollout |
215
|
|
|
* @param RolloutUserInterface|null $user |
216
|
|
|
* @param array $requestParameters |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function isActive(Rollout $rollout, RolloutUserInterface $user = null, array $requestParameters = array()) |
220
|
|
|
{ |
221
|
|
|
if (null == $user) { |
222
|
|
|
return $this->isParamInRequestParams($requestParameters) |
223
|
|
|
|| $this->percentage == 100 |
224
|
|
|
|| $this->isInActiveGroup($rollout); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->isParamInRequestParams($requestParameters) || |
228
|
|
|
$this->isUserInPercentage($user) || |
229
|
|
|
$this->isUserInActiveUsers($user) || |
230
|
|
|
$this->isInActiveGroup($rollout, $user); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function toArray() |
237
|
|
|
{ |
238
|
|
|
return array( |
239
|
|
|
'percentage' => $this->percentage, |
240
|
|
|
'groups' => $this->groups, |
241
|
|
|
'users' => $this->users, |
242
|
|
|
'requestParam' => $this->requestParam, |
243
|
|
|
'data'=> $this->data |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param array $requestParameters |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
private function isParamInRequestParams(array $requestParameters) |
252
|
|
|
{ |
253
|
|
|
$param = explode('=', $this->requestParam); |
254
|
|
|
$key = array_shift($param); |
255
|
|
|
$value = array_shift($param); |
256
|
|
|
|
257
|
|
|
return $key && array_key_exists($key, $requestParameters) && |
258
|
|
|
(empty($value) || $requestParameters[$key] == $value); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param RolloutUserInterface $user |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
|
|
private function isUserInPercentage(RolloutUserInterface $user) |
266
|
|
|
{ |
267
|
|
|
return abs(crc32($user->getRolloutIdentifier()) % 100) < $this->percentage; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param RolloutUserInterface $user |
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
private function isUserInActiveUsers(RolloutUserInterface $user) |
275
|
|
|
{ |
276
|
|
|
return in_array($user->getRolloutIdentifier(), $this->users); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param Rollout $rollout |
281
|
|
|
* @param RolloutUserInterface|null $user |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
|
|
private function isInActiveGroup(Rollout $rollout, RolloutUserInterface $user = null) |
285
|
|
|
{ |
286
|
|
|
foreach ($this->groups as $group) { |
287
|
|
|
if ($rollout->isActiveInGroup($group, $user)) { |
288
|
|
|
return true; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: