|
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
|
|
|
* @param string $name |
|
40
|
|
|
* @param string|null $settings |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($name, $settings = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->name = $name; |
|
45
|
|
|
if ($settings) { |
|
|
|
|
|
|
46
|
|
|
$settings = explode('|', $settings); |
|
47
|
|
|
if (count($settings) == 4) { |
|
48
|
|
|
$rawRequestParam = array_pop($settings); |
|
49
|
|
|
$this->requestParam = $rawRequestParam; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
list($rawPercentage, $rawUsers, $rawGroups) = $settings; |
|
53
|
|
|
$this->percentage = (int) $rawPercentage; |
|
54
|
|
|
$this->users = !empty($rawUsers) ? explode(',', $rawUsers) : array(); |
|
55
|
|
|
$this->groups = !empty($rawGroups) ? explode(',', $rawGroups) : array(); |
|
56
|
|
|
} else { |
|
57
|
|
|
$this->clear(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getName() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param integer $percentage |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setPercentage($percentage) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->percentage = $percentage; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return integer |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPercentage() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->percentage; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function serialize() |
|
89
|
|
|
{ |
|
90
|
|
|
return implode('|', array( |
|
91
|
|
|
$this->percentage, |
|
92
|
|
|
implode(',', $this->users), |
|
93
|
|
|
implode(',', $this->groups), |
|
94
|
|
|
$this->requestParam, |
|
95
|
|
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param RolloutUserInterface $user |
|
100
|
|
|
*/ |
|
101
|
|
|
public function addUser(RolloutUserInterface $user) |
|
102
|
|
|
{ |
|
103
|
|
|
if (!in_array($user->getRolloutIdentifier(), $this->users)) { |
|
104
|
|
|
$this->users[] = $user->getRolloutIdentifier(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param RolloutUserInterface $user |
|
110
|
|
|
*/ |
|
111
|
|
|
public function removeUser(RolloutUserInterface $user) |
|
112
|
|
|
{ |
|
113
|
|
|
if (($key = array_search($user->getRolloutIdentifier(), $this->users)) !== false) { |
|
114
|
|
|
unset($this->users[$key]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getUsers() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->users; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $group |
|
128
|
|
|
*/ |
|
129
|
|
|
public function addGroup($group) |
|
130
|
|
|
{ |
|
131
|
|
|
if (!in_array($group, $this->groups)) { |
|
132
|
|
|
$this->groups[] = $group; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $group |
|
138
|
|
|
*/ |
|
139
|
|
|
public function removeGroup($group) |
|
140
|
|
|
{ |
|
141
|
|
|
if (($key = array_search($group, $this->groups)) !== false) { |
|
142
|
|
|
unset($this->groups[$key]); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getGroups() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->groups; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return string|null |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getRequestParam() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->requestParam; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string|null $requestParam |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setRequestParam($requestParam) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->requestParam = $requestParam; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Clear the feature of all configuration |
|
172
|
|
|
*/ |
|
173
|
|
|
public function clear() |
|
174
|
|
|
{ |
|
175
|
|
|
$this->groups = array(); |
|
176
|
|
|
$this->users = array(); |
|
177
|
|
|
$this->percentage = 0; |
|
178
|
|
|
$this->requestParam = ''; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Is the feature active? |
|
183
|
|
|
* |
|
184
|
|
|
* @param Rollout $rollout |
|
185
|
|
|
* @param RolloutUserInterface|null $user |
|
186
|
|
|
* @param array $requestParameters |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
|
|
public function isActive(Rollout $rollout, RolloutUserInterface $user = null, array $requestParameters = array()) |
|
190
|
|
|
{ |
|
191
|
|
|
if (null == $user) { |
|
192
|
|
|
return $this->isParamInRequestParams($requestParameters) |
|
193
|
|
|
|| $this->percentage == 100 |
|
194
|
|
|
|| $this->isInActiveGroup($rollout); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->isParamInRequestParams($requestParameters) || |
|
198
|
|
|
$this->isUserInPercentage($user) || |
|
199
|
|
|
$this->isUserInActiveUsers($user) || |
|
200
|
|
|
$this->isInActiveGroup($rollout, $user); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
|
|
public function toArray() |
|
207
|
|
|
{ |
|
208
|
|
|
return array( |
|
209
|
|
|
'percentage' => $this->percentage, |
|
210
|
|
|
'groups' => $this->groups, |
|
211
|
|
|
'users' => $this->users, |
|
212
|
|
|
'requestParam' => $this->requestParam, |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param array $requestParameters |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
|
|
private function isParamInRequestParams(array $requestParameters) |
|
221
|
|
|
{ |
|
222
|
|
|
$param = explode('=', $this->requestParam); |
|
223
|
|
|
$key = array_shift($param); |
|
224
|
|
|
$value = array_shift($param); |
|
225
|
|
|
|
|
226
|
|
|
return $key && array_key_exists($key, $requestParameters) && |
|
227
|
|
|
(empty($value) || $requestParameters[$key] == $value); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param RolloutUserInterface $user |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
private function isUserInPercentage(RolloutUserInterface $user) |
|
235
|
|
|
{ |
|
236
|
|
|
return abs(crc32($user->getRolloutIdentifier()) % 100) < $this->percentage; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param RolloutUserInterface $user |
|
241
|
|
|
* @return boolean |
|
242
|
|
|
*/ |
|
243
|
|
|
private function isUserInActiveUsers(RolloutUserInterface $user) |
|
244
|
|
|
{ |
|
245
|
|
|
return in_array($user->getRolloutIdentifier(), $this->users); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param Rollout $rollout |
|
250
|
|
|
* @param RolloutUserInterface|null $user |
|
251
|
|
|
* @return bool |
|
252
|
|
|
*/ |
|
253
|
|
|
private function isInActiveGroup(Rollout $rollout, RolloutUserInterface $user = null) |
|
254
|
|
|
{ |
|
255
|
|
|
foreach ($this->groups as $group) { |
|
256
|
|
|
if ($rollout->isActiveInGroup($group, $user)) { |
|
257
|
|
|
return true; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: