forked from Unleash/unleash-client-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnleashDecorator.php
73 lines (65 loc) · 2.21 KB
/
UnleashDecorator.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
<?php
namespace Unleash\Client\Bundle\Unleash;
use Generator;
use Unleash\Client\Client\RegistrationService;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\DefaultUnleash;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\UnleashRepository;
use Unleash\Client\Strategy\StrategyHandler;
use Unleash\Client\Unleash;
use Unleash\Client\Variant\VariantHandler;
final class UnleashDecorator implements Unleash
{
private Unleash $proxy;
/**
* @param array<string> $disabledHandlers
* @param iterable<StrategyHandler> $strategyHandlers
*/
public function __construct(
private array $disabledHandlers,
iterable $strategyHandlers,
UnleashRepository $repository,
RegistrationService $registrationService,
UnleashConfiguration $configuration,
MetricsHandler $metricsHandler,
VariantHandler $variantHandler,
) {
$strategyHandlers = $this->filter($strategyHandlers);
$this->proxy = new DefaultUnleash(
iterator_to_array($strategyHandlers),
$repository,
$registrationService,
$configuration,
$metricsHandler,
$variantHandler
);
}
public function isEnabled(string $featureName, ?Context $context = null, bool $default = false): bool
{
return $this->proxy->isEnabled($featureName, $context, $default);
}
public function getVariant(string $featureName, ?Context $context = null, ?Variant $fallbackVariant = null): Variant
{
return $this->proxy->getVariant($featureName, $context, $fallbackVariant);
}
public function register(): bool
{
return $this->proxy->register();
}
/**
* @param iterable<StrategyHandler> $strategyHandlers
*
* @return Generator<StrategyHandler>
*/
private function filter(iterable $strategyHandlers): Generator
{
foreach ($strategyHandlers as $strategyHandler) {
if (!in_array($strategyHandler->getStrategyName(), $this->disabledHandlers, true)) {
yield $strategyHandler;
}
}
}
}