12
12
use MikeFrancis \LaravelUnleash \Strategies \Contracts \DynamicStrategy ;
13
13
use MikeFrancis \LaravelUnleash \Strategies \Contracts \Strategy ;
14
14
use Symfony \Component \HttpFoundation \Exception \JsonException ;
15
+
15
16
use function GuzzleHttp \json_decode ;
16
17
17
18
class Unleash
18
19
{
19
- const DEFAULT_CACHE_TTL = 15 ;
20
+ public const DEFAULT_CACHE_TTL = 15 ;
20
21
21
22
protected $ client ;
22
23
protected $ cache ;
@@ -39,20 +40,26 @@ public function getFeatures(): array
39
40
return $ this ->features ;
40
41
}
41
42
42
- try {
43
- $ this ->features = $ this ->getCachedFeatures ();
44
-
45
- // Always store the failover cache, in case it is turned on during failure scenarios.
46
- $ this ->cache ->forever ('unleash.features.failover ' , $ this ->features );
43
+ if (!$ this ->config ->get ('unleash.isEnabled ' )) {
44
+ return [];
45
+ }
47
46
48
- return $ this ->features ;
49
- } catch (TransferException | JsonException $ e ) {
47
+ try {
48
+ if ($ this ->config ->get ('unleash.cache.isEnabled ' )) {
49
+ $ data = $ this ->getCachedFeatures ();
50
+ } else {
51
+ $ data = $ this ->fetchFeatures ();
52
+ }
53
+ } catch (TransferException | JsonException ) {
50
54
if ($ this ->config ->get ('unleash.cache.failover ' ) === true ) {
51
- return $ this ->cache ->get ('unleash.features .failover ' , []);
55
+ $ data = $ this ->cache ->get ('unleash.failover ' , []);
52
56
}
53
57
}
54
58
55
- return [];
59
+ $ this ->features = Arr::get ($ data , 'features ' , []);
60
+ $ this ->expires = Arr::get ($ data , 'expires ' , $ this ->getExpires ());
61
+
62
+ return $ this ->features ;
56
63
}
57
64
58
65
public function getFeature (string $ name )
@@ -93,7 +100,7 @@ public function isFeatureEnabled(string $name, ...$args): bool
93
100
if (is_callable ($ allStrategies [$ className ])) {
94
101
$ strategy = $ allStrategies [$ className ]();
95
102
} else {
96
- $ strategy = new $ allStrategies [$ className ];
103
+ $ strategy = new $ allStrategies [$ className ]() ;
97
104
}
98
105
99
106
if (!$ strategy instanceof Strategy && !$ strategy instanceof DynamicStrategy) {
@@ -115,40 +122,34 @@ public function isFeatureDisabled(string $name, ...$args): bool
115
122
return !$ this ->isFeatureEnabled ($ name , ...$ args );
116
123
}
117
124
125
+ public function refreshCache ()
126
+ {
127
+ $ this ->fetchFeatures ();
128
+ }
129
+
118
130
protected function isFresh (): bool
119
131
{
120
132
return $ this ->expires > time ();
121
133
}
122
134
123
135
protected function getCachedFeatures (): array
124
136
{
125
- if (!$ this ->config ->get ('unleash.isEnabled ' )) {
126
- return [];
127
- }
128
-
129
- if ($ this ->config ->get ('unleash.cache.isEnabled ' )) {
130
- $ this ->setExpires ();
131
-
132
- return $ this ->cache ->remember (
133
- 'unleash ' ,
134
- $ this ->getCacheTTL (),
135
- function () {
136
- return $ this ->fetchFeatures ();
137
- }
138
- );
139
- }
140
-
141
- return $ this ->features ?? $ this ->features = $ this ->fetchFeatures ();
137
+ return $ this ->cache ->get ('unleash.cache ' , function () {return $ this ->fetchFeatures ();});
142
138
}
143
139
144
- protected function getCacheTTL (): int
140
+ public function getCacheTTL (): int
145
141
{
146
142
return $ this ->config ->get ('unleash.cache.ttl ' , self ::DEFAULT_CACHE_TTL );
147
143
}
148
144
149
- protected function setExpires (): void
145
+ protected function setExpires (): int
150
146
{
151
- $ this ->expires = $ this ->getCacheTTL () + time ();
147
+ return $ this ->expires = $ this ->getCacheTTL () + time ();
148
+ }
149
+
150
+ public function getExpires (): int
151
+ {
152
+ return $ this ->expires ?? $ this ->setExpires ();
152
153
}
153
154
154
155
protected function fetchFeatures (): array
@@ -161,13 +162,13 @@ protected function fetchFeatures(): array
161
162
throw new JsonException ('Could not decode unleash response body. ' , $ e ->getCode (), $ e );
162
163
}
163
164
164
- $ this ->setExpires ();
165
+ $ data [ ' expires ' ] = $ this ->setExpires ();
165
166
166
- return $ this ->formatResponse ( $ data );
167
- }
167
+ $ this ->cache -> set ( ' unleash.cache ' , $ data, $ this -> getCacheTTL () );
168
+ $ this -> cache -> forever ( ' unleash.failover ' , $ data );
168
169
169
- protected function formatResponse ($ data): array
170
- {
171
- return Arr:: get ( $ data, ' features ' , []) ;
170
+ $ this -> features = Arr:: get ($ data, ' features ' , []);
171
+
172
+ return $ data ;
172
173
}
173
174
}
0 commit comments