-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathFeatureStateModel.php
More file actions
256 lines (225 loc) · 7.13 KB
/
FeatureStateModel.php
File metadata and controls
256 lines (225 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace Flagsmith\Engine\Features;
use Flagsmith\Concerns\HasWith;
use Flagsmith\Concerns\JsonSerializer;
use Flagsmith\Engine\Utils\Collections\MultivariateFeatureStateValueModelList;
use Flagsmith\Engine\Utils\HashingTrait;
use Flagsmith\Engine\Utils\UniqueUID;
#[\AllowDynamicProperties]
class FeatureStateModel
{
use HasWith, HashingTrait, JsonSerializer {
JsonSerializer::setValues as setValuesSerializer;
}
public FeatureModel $feature;
public bool $enabled;
private $_value;
public string $featurestate_uuid;
public MultivariateFeatureStateValueModelList $multivariate_feature_state_values;
public ?int $django_id = null;
public ?FeatureSegmentModel $feature_segment = null;
private array $keys = [
'feature' => 'Flagsmith\Engine\Features\FeatureModel',
'multivariate_feature_state_values' => 'Flagsmith\Engine\Utils\Collections\MultivariateFeatureStateValueModelList',
'feature_segment' => 'Flagsmith\Engine\Features\FeatureSegmentModel',
];
public function __construct()
{
$this->featurestate_uuid = UniqueUID::v4();
$this->multivariate_feature_state_values = new MultivariateFeatureStateValueModelList();
}
/**
* Get the django ID.
* @return int
*/
public function getDjangoId(): int
{
return $this->django_id;
}
/**
* Build with django ID.
* @param int $djangoId
* @return FeatureStateModel
*/
public function withDjangoId(int $djangoId): self
{
return $this->with('django_id', $djangoId);
}
/**
* get the feature model.
* @return FeatureModel
*/
public function getFeature(): FeatureModel
{
return $this->feature;
}
/**
* build with the feature model.
* @param FeatureModel $feature
* @return FeatureStateModel
*/
public function withFeature(FeatureModel $feature): self
{
return $this->with('feature', $feature);
}
/**
* get enabled value.
* @return bool
*/
public function getEnabled(): bool
{
return $this->enabled;
}
/**
* build with enabled.
* @param bool $enabled
* @return FeatureStateModel
*/
public function withEnabled(bool $enabled): self
{
return $this->with('enabled', $enabled);
}
/**
* get the multivariate feature state value.
* @return MultivariateFeatureStateValueModelList
*/
public function getMultivariateFeatureStateValues(): MultivariateFeatureStateValueModelList
{
return $this->multivariate_feature_state_values;
}
/**
* build with multi variate feature state value.
* @param MultivariateFeatureStateValueModelList $multivariateFeatureStateValues
* @return FeatureStateModel
*/
public function withMultivariateFeatureStateValues(MultivariateFeatureStateValueModelList $multivariateFeatureStateValues): self
{
return $this->with('multivariate_feature_state_values', $multivariateFeatureStateValues);
}
/**
* get the feature state uuid.
* @return string
*/
public function getFeaturestateUuid(): string
{
return $this->featurestate_uuid;
}
/**
* get the feature state uuid.
* @param string $featurestateUuid
* @return FeatureStateModel
*/
public function withFeaturestateUuid(string $featurestateUuid): self
{
return $this->with('featurestate_uuid', $featurestateUuid);
}
/**
* get the feature ssegment.
* @return FeatureSegmentModel
*/
public function getFeatureSegment(): ?FeatureSegmentModel
{
return $this->feature_segment;
}
/**
* build with a feature segment model.
* @param FeatureSegmentModel $featureSegment
* @return FeatureStateModel
*/
public function withFeatureSegment(?FeatureSegmentModel $featureSegment): self
{
return $this->with('feature_segment', $featureSegment);
}
/**
* get the value.
*/
public function getValue($identityId = null)
{
if ($identityId && count($this->multivariate_feature_state_values) > 0) {
return $this->getMultivariateValue($identityId);
}
return $this->_value;
}
/**
* Get the feature statue value.
*/
public function getFeatureStateValue()
{
return $this->getValue();
}
/**
* get the value from multi variate configuration.
* @param mixed $identityId
* @return mixed
*/
private function getMultivariateValue($identityId)
{
$identityIdArray = [
$this->django_id ?? $this->featurestate_uuid,
$identityId
];
$percentageValue = $this->getHashedPercentageForObjectIds($identityIdArray);
$startPercentage = 0;
$sortedMvFeatureStateValues = $this
->getMultivariateFeatureStateValues()
->getArrayCopy();
usort(
$sortedMvFeatureStateValues,
function (
MultivariateFeatureStateValueModel $mvFeatureState1,
MultivariateFeatureStateValueModel $mvFeatureState2
) {
$id1 = $mvFeatureState1->getId() ?? $mvFeatureState1->getMvFsValueUuid();
$id2 = $mvFeatureState2->getId() ?? $mvFeatureState2->getMvFsValueUuid();
return $id1 <=> $id2;
}
);
foreach ($sortedMvFeatureStateValues as $mvValue) {
$limit = $mvValue->getPercentageAllocation() + $startPercentage;
if ($startPercentage <= $percentageValue && $percentageValue < $limit) {
return $mvValue->getMultivariateFeatureOption()->getValue();
}
$startPercentage = $limit;
}
return $this->_value;
}
/**
* set the value.
* @param mixed $value
* @return void
*/
public function setValue($value)
{
$this->_value = $value;
}
/**
* Set values from keys.
* @param mixed $values
* @return void
*/
protected function setValues($values)
{
$featureStateValue = $values->feature_state_value;
unset($values->feature_state_value);
$this->setValuesSerializer($values);
if (!empty($featureStateValue)) {
$this->_value = $featureStateValue;
}
}
/**
* Another FeatureStateModel is deemed to be higher priority if and only if
* it has a FeatureSegment and either this.FeatureSegment is null or the
* value of other.FeatureSegment.priority is lower than that of
* this.FeatureSegment.priority.
*
* @param FeatureStateModel $other - the other FeatureStateModel to compare priority with
* @return bool - true if `this` is higher priority than `other`
*/
public function isHigherPriority(FeatureStateModel $other): bool
{
if ($this->getFeatureSegment() == null || $other->getFeatureSegment() == null) {
return ($this->getFeatureSegment() != null && $other->getFeatureSegment() == null);
}
return $this->getFeatureSegment()->getPriority() < $other->getFeatureSegment()->getPriority();
}
}