2
2
3
3
namespace MikeFrancis \LaravelUnleash ;
4
4
5
- use GuzzleHttp \ClientInterface ;
6
- use GuzzleHttp \Exception \InvalidArgumentException ;
5
+ use GuzzleHttp \Client ;
7
6
use GuzzleHttp \Exception \TransferException ;
8
7
use Illuminate \Contracts \Cache \Repository as Cache ;
9
8
use Illuminate \Contracts \Config \Repository as Config ;
10
9
use Illuminate \Http \Request ;
11
10
use Illuminate \Support \Arr ;
11
+ use JsonException ;
12
12
use MikeFrancis \LaravelUnleash \Strategies \Contracts \DynamicStrategy ;
13
13
use MikeFrancis \LaravelUnleash \Strategies \Contracts \Strategy ;
14
- use Symfony \Component \HttpFoundation \Exception \JsonException ;
15
-
16
- use function GuzzleHttp \json_decode ;
17
14
18
15
class Unleash
19
16
{
@@ -26,7 +23,7 @@ class Unleash
26
23
protected $ features ;
27
24
protected $ expires ;
28
25
29
- public function __construct (ClientInterface $ client , Cache $ cache , Config $ config , Request $ request )
26
+ public function __construct (Client $ client , Cache $ cache , Config $ config , Request $ request )
30
27
{
31
28
$ this ->client = $ client ;
32
29
$ this ->cache = $ cache ;
@@ -109,7 +106,7 @@ public function isFeatureEnabled(string $name, ...$args): bool
109
106
110
107
$ params = Arr::get ($ strategyData , 'parameters ' , []);
111
108
112
- if ($ strategy ->isEnabled ($ params , $ this ->request , ...$ args )) {
109
+ if ($ strategy ->isEnabled ($ params , $ this ->request , ...$ args )) { // @phan-suppress-current-line PhanParamTooManyUnpack
113
110
return true ;
114
111
}
115
112
}
@@ -124,7 +121,9 @@ public function isFeatureDisabled(string $name, ...$args): bool
124
121
125
122
public function refreshCache ()
126
123
{
127
- $ this ->fetchFeatures ();
124
+ if ($ this ->config ->get ('unleash.isEnabled ' ) && $ this ->config ->get ('unleash.cache.isEnabled ' )) {
125
+ $ this ->fetchFeatures ();
126
+ }
128
127
}
129
128
130
129
protected function isFresh (): bool
@@ -156,11 +155,7 @@ protected function fetchFeatures(): array
156
155
{
157
156
$ response = $ this ->client ->get ($ this ->config ->get ('unleash.featuresEndpoint ' ));
158
157
159
- try {
160
- $ data = json_decode ((string )$ response ->getBody (), true , 512 , \JSON_BIGINT_AS_STRING );
161
- } catch (InvalidArgumentException $ e ) {
162
- throw new JsonException ('Could not decode unleash response body. ' , $ e ->getCode (), $ e );
163
- }
158
+ $ data = (array ) json_decode ((string )$ response ->getBody (), true , 512 , JSON_BIGINT_AS_STRING + JSON_THROW_ON_ERROR );
164
159
165
160
$ data ['expires ' ] = $ this ->setExpires ();
166
161
0 commit comments