44
55namespace OpenFeature \Providers \GoFeatureFlag ;
66
7+ use OpenFeature \Providers \GoFeatureFlag \config \Config ;
8+ use OpenFeature \Providers \GoFeatureFlag \controller \OfrepApi ;
9+ use OpenFeature \Providers \GoFeatureFlag \exception \BaseOfrepException ;
10+ use OpenFeature \Providers \GoFeatureFlag \exception \InvalidConfigException ;
11+ use OpenFeature \Providers \GoFeatureFlag \util \Validator ;
712use OpenFeature \implementation \common \Metadata ;
813use OpenFeature \implementation \provider \AbstractProvider ;
914use OpenFeature \implementation \provider \ResolutionDetailsBuilder ;
1318use OpenFeature \interfaces \provider \Provider ;
1419use OpenFeature \interfaces \provider \Reason ;
1520use OpenFeature \interfaces \provider \ResolutionDetails ;
16- use OpenFeature \Providers \GoFeatureFlag \config \Config ;
17- use OpenFeature \Providers \GoFeatureFlag \controller \OfrepApi ;
18- use OpenFeature \Providers \GoFeatureFlag \exception \BaseOfrepException ;
19- use OpenFeature \Providers \GoFeatureFlag \exception \InvalidConfigException ;
20- use OpenFeature \Providers \GoFeatureFlag \util \Validator ;
21+ use Throwable ;
22+
23+ use function array_key_exists ;
24+ use function gettype ;
25+ use function implode ;
26+ use function is_array ;
2127
2228class GoFeatureFlagProvider extends AbstractProvider implements Provider
2329{
24- protected static string $ CLIENT_NAME = 'GO Feature Flag Provider ' ;
30+ protected static string $ NAME = 'GO Feature Flag Provider ' ;
2531 private OfrepApi $ ofrepApi ;
2632
2733 /**
@@ -30,15 +36,15 @@ class GoFeatureFlagProvider extends AbstractProvider implements Provider
3036 public function __construct (Config $ config )
3137 {
3238 Validator::validateConfig ($ config );
33- if (is_array ($ config ->getCustomHeaders ()) && !array_key_exists (" Content-Type " , $ config ->getCustomHeaders ())) {
34- $ config ->getCustomHeaders ()[" Content-Type " ] = " application/json " ;
39+ if (is_array ($ config ->getCustomHeaders ()) && !array_key_exists (' Content-Type ' , $ config ->getCustomHeaders ())) {
40+ $ config ->getCustomHeaders ()[' Content-Type ' ] = ' application/json ' ;
3541 }
3642 $ this ->ofrepApi = new OfrepApi ($ config );
3743 }
3844
3945 public function getMetadata (): Metadata
4046 {
41- return new Metadata (self ::$ CLIENT_NAME );
47+ return new Metadata (static ::$ NAME );
4248 }
4349
4450 public function resolveBooleanValue (string $ flagKey , bool $ defaultValue , ?EvaluationContext $ context = null ): ResolutionDetails
@@ -49,7 +55,7 @@ public function resolveBooleanValue(string $flagKey, bool $defaultValue, ?Evalua
4955 /**
5056 * @param array<string> $allowedClasses
5157 */
52- private function evaluate (string $ flagKey , mixed $ defaultValue , array $ allowedClasses , EvaluationContext $ evaluationContext = null ): ResolutionDetails
58+ private function evaluate (string $ flagKey , mixed $ defaultValue , array $ allowedClasses , ? EvaluationContext $ evaluationContext = null ): ResolutionDetails
5359 {
5460 try {
5561 Validator::validateEvaluationContext ($ evaluationContext );
@@ -60,8 +66,9 @@ private function evaluate(string $flagKey, mixed $defaultValue, array $allowedCl
6066 if ($ apiResp ->isError ()) {
6167 $ err = new ResolutionError (
6268 $ apiResp ->getErrorCode () ?? ErrorCode::GENERAL (),
63- $ apiResp ->getErrorDetails ()
69+ $ apiResp ->getErrorDetails (),
6470 );
71+
6572 return (new ResolutionDetailsBuilder ())
6673 ->withValue ($ defaultValue )
6774 ->withError ($ err )
@@ -74,39 +81,45 @@ private function evaluate(string $flagKey, mixed $defaultValue, array $allowedCl
7481 ->withReason (Reason::ERROR )
7582 ->withError (new ResolutionError (
7683 ErrorCode::TYPE_MISMATCH (),
77- "Invalid type for $ flagKey, got " . gettype ($ apiResp ->getValue ()) . " expected " . implode (", " , $ allowedClasses )))
84+ "Invalid type for $ flagKey, got " . gettype ($ apiResp ->getValue ()) . ' expected ' . implode (', ' , $ allowedClasses ),
85+ ))
7886 ->withValue ($ defaultValue )
7987 ->build ();
8088 }
89+
8190 return (new ResolutionDetailsBuilder ())
8291 ->withValue ($ apiResp ->getValue ())
8392 ->withReason ($ apiResp ->getReason ())
8493 ->withVariant ($ apiResp ->getVariant ())
8594 ->build ();
86-
8795 } catch (BaseOfrepException $ e ) {
8896 $ err = new ResolutionError ($ e ->getErrorCode (), $ e ->getMessage ());
97+
8998 return (new ResolutionDetailsBuilder ())
9099 ->withValue ($ defaultValue )
91100 ->withError ($ err )
92101 ->withReason (Reason::ERROR )
93102 ->build ();
94- } catch (\ Exception $ e ) {
103+ } catch (Throwable $ e ) {
95104 return (new ResolutionDetailsBuilder ())
96105 ->withValue ($ defaultValue )
97- ->withError (new ResolutionError (ErrorCode::GENERAL (), " An error occurred while evaluating the flag: " . $ e ->getMessage ()))
106+ ->withError (new ResolutionError (ErrorCode::GENERAL (), ' An error occurred while evaluating the flag: ' . $ e ->getMessage ()))
98107 ->withReason (Reason::ERROR )
99108 ->build ();
100109 }
101110 }
102111
112+ /**
113+ * @param array<string> $allowedClasses
114+ */
103115 private function isValidType (mixed $ value , array $ allowedClasses ): bool
104116 {
105117 foreach ($ allowedClasses as $ class ) {
106118 if ($ value instanceof $ class || gettype ($ value ) === $ class ) {
107119 return true ;
108120 }
109121 }
122+
110123 return false ;
111124 }
112125
0 commit comments