-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathFlags.php
More file actions
215 lines (190 loc) · 5.97 KB
/
Flags.php
File metadata and controls
215 lines (190 loc) · 5.97 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
<?php
namespace Flagsmith\Models;
use Flagsmith\Concerns\HasWith;
use Flagsmith\Engine\Utils\Types\Result\EvaluationResult;
use Flagsmith\Utils\Collections\FlagModelsList;
use Flagsmith\Engine\Utils\Collections\FeatureStateModelList;
use Flagsmith\Exceptions\FlagsmithClientError;
use Flagsmith\Utils\AnalyticsProcessor;
class Flags
{
use HasWith;
public FlagModelsList $flags;
public ?\Closure $default_flag_handler;
public ?AnalyticsProcessor $analytics_processor;
public function __construct()
{
$this->flags = new FlagModelsList([]);
}
/**
* Get the Default Flag Handler
* @return \Closure
*/
public function getDefaultFlagHandler(): ?\Closure
{
return $this->default_flag_handler;
}
/**
* Build with default flag handler
* @param \Closure $default_flag_handler
* @return Flags
*/
public function withDefaultFlagHandler(?\Closure $default_flag_handler): self
{
return $this->with('default_flag_handler', $default_flag_handler);
}
/**
* Get the flags list.
* @return FlagModelsList
*/
public function getFlags(): FlagModelsList
{
return $this->flags;
}
/**
* Build with flags list.
* @param FlagModelsList $flags
* @return Flags
*/
public function withFlags(FlagModelsList $flags): self
{
return $this->with('flags', $flags);
}
/**
* Get the analytics processor.
* @return AnalyticsProcessor
*/
public function getAnalyticsProcessor(): ?AnalyticsProcessor
{
return $this->analytics_processor;
}
/**
* Build with Analytics Processor.
* @param AnalyticsProcessor $analyticsProcessor
* @return Flags
*/
public function withAnalyticsProcessor(?AnalyticsProcessor $analyticsProcessor): self
{
return $this->with('analytics_processor', $analyticsProcessor);
}
/**
* Build Flags from Evaluation Result.
* @param EvaluationResult $evaluationResult
* @param ?AnalyticsProcessor $analyticsProcessor
* @param ?\Closure $defaultFlagHandler
* @return void
*/
public static function fromEvaluationResult(
$evaluationResult,
?AnalyticsProcessor $analyticsProcessor,
?\Closure $defaultFlagHandler,
): Flags {
$flags = [];
foreach ($evaluationResult->flags as $flagResult) {
$flag = new Flag();
$flag->feature_name = $flagResult->name;
$flag->feature_id = $flagResult->metadata['id'];
$flag->enabled = $flagResult->enabled;
$flag->value = $flagResult->value;
$flags[$flagResult->name] = $flag;
}
$_this = new self();
$_this->flags = new FlagModelsList($flags);
$_this->default_flag_handler = $defaultFlagHandler;
$_this->analytics_processor = $analyticsProcessor;
return $_this;
}
/**
* Build with Feature State Models.
* @param FeatureStateModelList $featureStateModelsList
* @param AnalyticsProcessor $analyticsProcessor
* @param \Closure $defaultFlagHandler
* @param mixed $identityId
* @return Flags
*/
public static function fromFeatureStateModels(
FeatureStateModelList $featureStateModelsList,
?AnalyticsProcessor $analyticsProcessor,
?\Closure $defaultFlagHandler,
$identityId = null
) {
$flags = [];
foreach ($featureStateModelsList->getArrayCopy() as $featureState) {
$flags[$featureState->getFeature()->getName()] = Flag::fromFeatureStateModel($featureState, $identityId);
}
return (new self())
->withFlags(new FlagModelsList($flags))
->withDefaultFlagHandler($defaultFlagHandler)
->withAnalyticsProcessor($analyticsProcessor);
}
/**
* Build with Flags API response.
* @param array $apiFlags
* @param AnalyticsProcessor $analyticsProcessor
* @param \Closure $defaultFlagHandler
* @param mixed $identityId
* @return Flags
*/
public static function fromApiFlags(
object $apiFlags,
?AnalyticsProcessor $analyticsProcessor,
?\Closure $defaultFlagHandler,
$identityId = null
) {
$flags = [];
foreach ($apiFlags as $apiFlag) {
$flags[$apiFlag->feature->name] = Flag::fromApiFlag($apiFlag, $identityId);
}
return (new self())
->withFlags(new FlagModelsList($flags))
->withDefaultFlagHandler($defaultFlagHandler)
->withAnalyticsProcessor($analyticsProcessor);
}
/**
* Return with list of Flag objects.
* @return array
*/
public function allFlags(): array
{
return array_values($this->flags->getArrayCopy());
}
/**
* Is the feature enabled.
* @param string $featureName
* @return bool
*/
public function isFeatureEnabled(string $featureName): bool
{
return $this->getFlag($featureName)->getEnabled();
}
/**
* Get the value of the feature name.
* @param string $featureName
* @return mixed
*/
public function getFeatureValue(string $featureName)
{
return $this->getFlag($featureName)->getValue();
}
/**
* Get flag object by feature name.
* @param string $featureName
* @return BaseFlag
*/
public function getFlag(string $featureName): BaseFlag
{
$flag = null;
if (isset($this->flags->{$featureName})) {
$flag = $this->flags->{$featureName};
} else {
if (isset($this->default_flag_handler)) {
return $this->default_flag_handler->call($this, $featureName);
}
throw new FlagsmithClientError('Feature does not exist');
}
if (isset($this->analytics_processor) && !empty($flag) && isset($flag->feature_name)) {
$this->analytics_processor->trackFeature($flag->feature_name);
}
return $flag;
}
}