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, $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) || $this->percentage == 100; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $this->isParamInRequestParams($requestParameters) || |
196
|
|
|
$this->isUserInPercentage($user) || |
197
|
|
|
$this->isUserInActiveUsers($user) || |
198
|
|
|
$this->isUserInActiveGroup($user, $rollout); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function toArray() |
205
|
|
|
{ |
206
|
|
|
return array( |
207
|
|
|
'percentage' => $this->percentage, |
208
|
|
|
'groups' => $this->groups, |
209
|
|
|
'users' => $this->users, |
210
|
|
|
'requestParam' => $this->requestParam, |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $requestParameters |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
private function isParamInRequestParams(array $requestParameters) |
219
|
|
|
{ |
220
|
|
|
$param = explode('=', $this->requestParam); |
221
|
|
|
$key = array_shift($param); |
222
|
|
|
$value = array_shift($param); |
223
|
|
|
|
224
|
|
|
return $key && array_key_exists($key, $requestParameters) && |
225
|
|
|
(empty($value) || $requestParameters[$key] == $value); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param RolloutUserInterface $user |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
private function isUserInPercentage(RolloutUserInterface $user) |
233
|
|
|
{ |
234
|
|
|
return abs(crc32($user->getRolloutIdentifier()) % 100) < $this->percentage; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param RolloutUserInterface $user |
239
|
|
|
* @return boolean |
240
|
|
|
*/ |
241
|
|
|
private function isUserInActiveUsers(RolloutUserInterface $user) |
242
|
|
|
{ |
243
|
|
|
return in_array($user->getRolloutIdentifier(), $this->users); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param RolloutUserInterface $user |
248
|
|
|
* @param Rollout $rollout |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
private function isUserInActiveGroup(RolloutUserInterface $user, Rollout $rollout) |
252
|
|
|
{ |
253
|
|
|
foreach ($this->groups as $group) { |
254
|
|
|
if ($rollout->isActiveInGroup($group, $user)) { |
255
|
|
|
return true; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return false; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
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: