-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDefaultUnleash.php
executable file
·208 lines (181 loc) · 7.52 KB
/
DefaultUnleash.php
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
<?php
namespace Unleash\Client;
use Unleash\Client\Client\RegistrationService;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\DTO\DefaultFeatureEnabledResult;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\FeatureEnabledResult;
use Unleash\Client\DTO\Strategy;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Enum\ImpressionDataEventType;
use Unleash\Client\Event\FeatureToggleDisabledEvent;
use Unleash\Client\Event\FeatureToggleMissingStrategyHandlerEvent;
use Unleash\Client\Event\FeatureToggleNotFoundEvent;
use Unleash\Client\Event\ImpressionDataEvent;
use Unleash\Client\Event\UnleashEvents;
use Unleash\Client\Helper\Uuid;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\UnleashRepository;
use Unleash\Client\Strategy\StrategyHandler;
use Unleash\Client\Variant\VariantHandler;
final class DefaultUnleash implements Unleash
{
/**
* @param iterable<StrategyHandler> $strategyHandlers
*/
public function __construct(
private readonly iterable $strategyHandlers,
private readonly UnleashRepository $repository,
private readonly RegistrationService $registrationService,
private readonly UnleashConfiguration $configuration,
private readonly MetricsHandler $metricsHandler,
private readonly VariantHandler $variantHandler,
) {
if ($configuration->isAutoRegistrationEnabled()) {
$this->register();
}
}
public function isEnabled(string $featureName, ?Context $context = null, bool $default = false): bool
{
$context ??= $this->configuration->getContextProvider()->getContext();
$feature = $this->findFeature($featureName, $context);
if ($feature !== null) {
if (method_exists($feature, 'hasImpressionData') && $feature->hasImpressionData()) {
$event = new ImpressionDataEvent(
ImpressionDataEventType::IS_ENABLED,
Uuid::v4(),
clone $this->configuration,
clone $context,
clone $feature,
null,
);
$this->configuration->getEventDispatcherOrNull()?->dispatch($event, UnleashEvents::IMPRESSION_DATA);
}
}
return $this->isFeatureEnabled($feature, $context, $default)->isEnabled();
}
public function getVariant(string $featureName, ?Context $context = null, ?Variant $fallbackVariant = null): Variant
{
$fallbackVariant ??= $this->variantHandler->getDefaultVariant();
$context ??= $this->configuration->getContextProvider()->getContext();
$feature = $this->findFeature($featureName, $context);
$enabledResult = $this->isFeatureEnabled($feature, $context);
$strategyVariants = $enabledResult->getStrategy()?->getVariants() ?? [];
if ($feature === null || $enabledResult->isEnabled() === false ||
(!count($feature->getVariants()) && empty($strategyVariants))) {
return $fallbackVariant;
}
if (empty($strategyVariants)) {
$variant = $this->variantHandler->selectVariant($feature->getVariants(), $featureName, $context);
} else {
$variant = $this->variantHandler->selectVariant($strategyVariants, $enabledResult->getStrategy()?->getParameters()['groupId'] ?? '', $context);
}
if ($variant !== null) {
$this->metricsHandler->handleMetrics($feature, true, $variant);
if (method_exists($feature, 'hasImpressionData') && $feature->hasImpressionData()) {
$event = new ImpressionDataEvent(
ImpressionDataEventType::GET_VARIANT,
Uuid::v4(),
clone $this->configuration,
clone $context,
clone $feature,
clone $variant,
);
$this->configuration->getEventDispatcherOrNull()?->dispatch($event, UnleashEvents::IMPRESSION_DATA);
}
}
$resolvedVariant = $variant ?? $fallbackVariant;
return $resolvedVariant;
}
public function register(): bool
{
return $this->registrationService->register($this->strategyHandlers);
}
/**
* Finds a feature and posts events if the feature is not found.
*
* @param string $featureName name of the feature to find
* @param Context $context the context to use
*
* @return Feature|null
*/
private function findFeature(string $featureName, Context $context): ?Feature
{
$feature = $this->repository->findFeature($featureName);
if ($feature === null) {
$event = new FeatureToggleNotFoundEvent($context, $featureName);
$this->configuration->getEventDispatcherOrNull()?->dispatch(
$event,
UnleashEvents::FEATURE_TOGGLE_NOT_FOUND,
);
}
return $feature;
}
/**
* Underlying method to check if a feature is enabled.
*
* @param Feature|null $feature the feature to check
* @param Context $context the context to use
* @param bool $default the default value to return if the feature is not found
*/
private function isFeatureEnabled(?Feature $feature, Context $context, bool $default = false): FeatureEnabledResult
{
if ($feature === null) {
return new DefaultFeatureEnabledResult($default);
}
if (!$feature->isEnabled()) {
$event = new FeatureToggleDisabledEvent($feature, $context);
$this->configuration->getEventDispatcherOrNull()?->dispatch(
$event,
UnleashEvents::FEATURE_TOGGLE_DISABLED,
);
$this->metricsHandler->handleMetrics($feature, false);
return new DefaultFeatureEnabledResult();
}
$strategies = $feature->getStrategies();
if (!is_countable($strategies)) {
$strategies = iterator_to_array($strategies);
}
if (!count($strategies)) {
$this->metricsHandler->handleMetrics($feature, true);
return new DefaultFeatureEnabledResult(true);
}
$handlersFound = false;
foreach ($strategies as $strategy) {
$handlers = $this->findStrategyHandlers($strategy);
if (!count($handlers)) {
continue;
}
$handlersFound = true;
foreach ($handlers as $handler) {
if ($handler->isEnabled($strategy, $context)) {
$this->metricsHandler->handleMetrics($feature, true);
return new DefaultFeatureEnabledResult(true, $strategy);
}
}
}
if (!$handlersFound) {
$event = new FeatureToggleMissingStrategyHandlerEvent($context, $feature);
$this->configuration->getEventDispatcherOrNull()?->dispatch(
$event,
UnleashEvents::FEATURE_TOGGLE_MISSING_STRATEGY_HANDLER,
);
}
$this->metricsHandler->handleMetrics($feature, false);
return new DefaultFeatureEnabledResult();
}
/**
* @return array<StrategyHandler>
*/
private function findStrategyHandlers(Strategy $strategy): array
{
$handlers = [];
foreach ($this->strategyHandlers as $strategyHandler) {
if ($strategyHandler->supports($strategy)) {
$handlers[] = $strategyHandler;
}
}
return $handlers;
}
}