-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathUnleash.php
145 lines (116 loc) Β· 4.27 KB
/
Unleash.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
<?php
namespace MikeFrancis\LaravelUnleash;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\InvalidArgumentException;
use GuzzleHttp\Exception\TransferException;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use MikeFrancis\LaravelUnleash\Strategies\Contracts\DynamicStrategy;
use MikeFrancis\LaravelUnleash\Strategies\Contracts\Strategy;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use function GuzzleHttp\json_decode;
class Unleash
{
const DEFAULT_CACHE_TTL = 15;
protected $client;
protected $cache;
protected $config;
protected $request;
protected $features;
public function __construct(ClientInterface $client, Cache $cache, Config $config, Request $request)
{
$this->client = $client;
$this->cache = $cache;
$this->config = $config;
$this->request = $request;
}
public function getFeatures(): array
{
try {
$features = $this->getCachedFeatures();
// Always store the failover cache, in case it is turned on during failure scenarios.
$this->cache->forever('unleash.features.failover', $features);
return $features;
} catch (TransferException | JsonException $e) {
if ($this->config->get('unleash.cache.failover') === true) {
return $this->cache->get('unleash.features.failover', []);
}
}
return [];
}
public function getFeature(string $name)
{
$features = $this->getFeatures();
return Arr::first(
$features,
function (array $unleashFeature) use ($name) {
return $name === $unleashFeature['name'];
}
);
}
public function isFeatureEnabled(string $name, ...$args): bool
{
$feature = $this->getFeature($name);
$isEnabled = Arr::get($feature, 'enabled', false);
if (!$isEnabled) {
return false;
}
$strategies = Arr::get($feature, 'strategies', []);
$allStrategies = $this->config->get('unleash.strategies', []);
foreach ($strategies as $strategyData) {
$className = $strategyData['name'];
if (!array_key_exists($className, $allStrategies)) {
return false;
}
if (is_callable($allStrategies[$className])) {
$strategy = $allStrategies[$className]();
} else {
$strategy = new $allStrategies[$className];
}
if (!$strategy instanceof Strategy && !$strategy instanceof DynamicStrategy) {
throw new \Exception("${$className} does not implement base Strategy/DynamicStrategy.");
}
$params = Arr::get($strategyData, 'parameters', []);
if (!$strategy->isEnabled($params, $this->request, ...$args)) {
return false;
}
}
return $isEnabled;
}
public function isFeatureDisabled(string $name, ...$args): bool
{
return !$this->isFeatureEnabled($name, ...$args);
}
protected function getCachedFeatures(): array
{
if (!$this->config->get('unleash.isEnabled')) {
return [];
}
if ($this->config->get('unleash.cache.isEnabled')) {
return $this->cache->remember(
'unleash',
$this->config->get('unleash.cache.ttl', self::DEFAULT_CACHE_TTL),
function () {
return $this->fetchFeatures();
}
);
}
return $this->features ?? $this->features = $this->fetchFeatures();
}
protected function fetchFeatures(): array
{
$response = $this->client->get($this->config->get('unleash.featuresEndpoint'));
try {
$data = json_decode((string)$response->getBody(), true, 512, \JSON_BIGINT_AS_STRING);
} catch (InvalidArgumentException $e) {
throw new JsonException('Could not decode unleash response body.', $e->getCode(), $e);
}
return $this->formatResponse($data);
}
protected function formatResponse($data): array
{
return Arr::get($data, 'features', []);
}
}