-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDefaultProxyUnleash.php
57 lines (47 loc) · 1.88 KB
/
DefaultProxyUnleash.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
<?php
namespace Unleash\Client;
use Override;
use Unleash\Client\Configuration\Context;
use Unleash\Client\DTO\DefaultFeature;
use Unleash\Client\DTO\DefaultVariant;
use Unleash\Client\DTO\Variant;
use Unleash\Client\Enum\Stickiness;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\ProxyRepository;
final readonly class DefaultProxyUnleash implements Unleash
{
public function __construct(
private ProxyRepository $repository,
private MetricsHandler $metricsHandler,
) {
}
/**
* @codeCoverageIgnore
*/
#[Override]
public function register(): bool
{
//This is a no op, since registration is handled by the proxy/edge, this doesn't need coverage
return false;
}
#[Override]
public function isEnabled(string $featureName, ?Context $context = null, bool $default = false): bool
{
$response = $this->repository->findFeatureByContext($featureName, $context);
$enabled = $response ? $response->isEnabled() : $default;
$this->metricsHandler->handleMetrics(new DefaultFeature($featureName, $enabled, []), $enabled);
return $enabled;
}
#[Override]
public function getVariant(string $featureName, ?Context $context = null, ?Variant $fallbackVariant = null): Variant
{
$variant = $fallbackVariant ?? new DefaultVariant('disabled', false, 0, Stickiness::DEFAULT);
$response = $this->repository->findFeatureByContext($featureName, $context);
if ($response !== null) {
$variant = $response->getVariant();
}
$metricVariant = new DefaultVariant($variant->getName(), $variant->isEnabled(), 0, Stickiness::DEFAULT, $variant->getPayload());
$this->metricsHandler->handleMetrics(new DefaultFeature($featureName, $variant->isEnabled(), []), $variant->isEnabled(), $metricVariant);
return $variant;
}
}