44
55namespace OpenFeature \Providers \GoFeatureFlag ;
66
7+ use DateTime ;
8+ use OpenFeature \Providers \GoFeatureFlag \config \Config ;
9+ use OpenFeature \Providers \GoFeatureFlag \controller \OfrepApi ;
10+ use OpenFeature \Providers \GoFeatureFlag \exception \BaseOfrepException ;
11+ use OpenFeature \Providers \GoFeatureFlag \exception \InvalidConfigException ;
12+ use OpenFeature \Providers \GoFeatureFlag \exception \InvalidContextException ;
13+ use OpenFeature \Providers \GoFeatureFlag \model \OfrepApiErrorResponse ;
14+ use OpenFeature \Providers \GoFeatureFlag \util \Validator ;
715use OpenFeature \implementation \common \Metadata ;
816use OpenFeature \implementation \provider \AbstractProvider ;
917use OpenFeature \implementation \provider \ResolutionDetailsBuilder ;
1321use OpenFeature \interfaces \provider \Provider ;
1422use OpenFeature \interfaces \provider \Reason ;
1523use 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 ;
24+ use Throwable ;
25+
26+ use function array_key_exists ;
27+ use function gettype ;
28+ use function implode ;
2129
2230class GoFeatureFlagProvider extends AbstractProvider implements Provider
2331{
24- protected static string $ CLIENT_NAME = 'GO Feature Flag Provider ' ;
32+ protected static string $ NAME = 'GO Feature Flag Provider ' ;
2533 private OfrepApi $ ofrepApi ;
2634
2735 /**
@@ -30,15 +38,15 @@ class GoFeatureFlagProvider extends AbstractProvider implements Provider
3038 public function __construct (Config $ config )
3139 {
3240 Validator::validateConfig ($ config );
33- if (is_array ( $ config -> getCustomHeaders ()) && !array_key_exists (" Content-Type " , $ config ->getCustomHeaders ())) {
34- $ config ->getCustomHeaders ()[ " Content-Type " ] = " application/json " ;
41+ if (!array_key_exists (' Content-Type ' , $ config ->getCustomHeaders ())) {
42+ $ config ->addCustomHeader ( ' Content-Type ' , ' application/json ' ) ;
3543 }
3644 $ this ->ofrepApi = new OfrepApi ($ config );
3745 }
3846
3947 public function getMetadata (): Metadata
4048 {
41- return new Metadata (self ::$ CLIENT_NAME );
49+ return new Metadata (static ::$ NAME );
4250 }
4351
4452 public function resolveBooleanValue (string $ flagKey , bool $ defaultValue , ?EvaluationContext $ context = null ): ResolutionDetails
@@ -47,25 +55,33 @@ public function resolveBooleanValue(string $flagKey, bool $defaultValue, ?Evalua
4755 }
4856
4957 /**
58+ * @param array<mixed>|array<string, mixed>|bool|DateTime|float|int|string|null $defaultValue
5059 * @param array<string> $allowedClasses
5160 */
52- private function evaluate (string $ flagKey , mixed $ defaultValue , array $ allowedClasses , EvaluationContext $ evaluationContext = null ): ResolutionDetails
61+ private function evaluate (string $ flagKey , array | string | bool | DateTime | float | int | null $ defaultValue , array $ allowedClasses , ? EvaluationContext $ evaluationContext = null ): ResolutionDetails
5362 {
5463 try {
55- Validator::validateEvaluationContext ($ evaluationContext );
5664 Validator::validateFlagKey ($ flagKey );
5765
66+ if ($ evaluationContext === null ) {
67+ throw new InvalidContextException ('Evaluation context is null ' );
68+ }
69+ if ($ evaluationContext ->getTargetingKey () === null || $ evaluationContext ->getTargetingKey () === '' ) {
70+ throw new InvalidContextException ('Missing targetingKey in evaluation context ' );
71+ }
72+
5873 $ apiResp = $ this ->ofrepApi ->evaluate ($ flagKey , $ evaluationContext );
5974
60- if ($ apiResp-> isError () ) {
75+ if ($ apiResp instanceof OfrepApiErrorResponse ) {
6176 $ err = new ResolutionError (
62- $ apiResp ->getErrorCode () ?? ErrorCode:: GENERAL () ,
63- $ apiResp ->getErrorDetails ()
77+ $ apiResp ->getErrorCode (),
78+ $ apiResp ->getErrorDetails (),
6479 );
80+
6581 return (new ResolutionDetailsBuilder ())
6682 ->withValue ($ defaultValue )
6783 ->withError ($ err )
68- ->withReason (Reason:: ERROR )
84+ ->withReason ($ apiResp -> getReason () )
6985 ->build ();
7086 }
7187
@@ -74,39 +90,45 @@ private function evaluate(string $flagKey, mixed $defaultValue, array $allowedCl
7490 ->withReason (Reason::ERROR )
7591 ->withError (new ResolutionError (
7692 ErrorCode::TYPE_MISMATCH (),
77- "Invalid type for $ flagKey, got " . gettype ($ apiResp ->getValue ()) . " expected " . implode (", " , $ allowedClasses )))
93+ "Invalid type for $ flagKey, got " . gettype ($ apiResp ->getValue ()) . ' expected ' . implode (', ' , $ allowedClasses ),
94+ ))
7895 ->withValue ($ defaultValue )
7996 ->build ();
8097 }
98+
8199 return (new ResolutionDetailsBuilder ())
82100 ->withValue ($ apiResp ->getValue ())
83101 ->withReason ($ apiResp ->getReason ())
84102 ->withVariant ($ apiResp ->getVariant ())
85103 ->build ();
86-
87104 } catch (BaseOfrepException $ e ) {
88105 $ err = new ResolutionError ($ e ->getErrorCode (), $ e ->getMessage ());
106+
89107 return (new ResolutionDetailsBuilder ())
90108 ->withValue ($ defaultValue )
91109 ->withError ($ err )
92110 ->withReason (Reason::ERROR )
93111 ->build ();
94- } catch (\ Exception $ e ) {
112+ } catch (Throwable $ e ) {
95113 return (new ResolutionDetailsBuilder ())
96114 ->withValue ($ defaultValue )
97- ->withError (new ResolutionError (ErrorCode::GENERAL (), " An error occurred while evaluating the flag: " . $ e ->getMessage ()))
115+ ->withError (new ResolutionError (ErrorCode::GENERAL (), ' An error occurred while evaluating the flag: ' . $ e ->getMessage ()))
98116 ->withReason (Reason::ERROR )
99117 ->build ();
100118 }
101119 }
102120
121+ /**
122+ * @param array<string> $allowedClasses
123+ */
103124 private function isValidType (mixed $ value , array $ allowedClasses ): bool
104125 {
105126 foreach ($ allowedClasses as $ class ) {
106127 if ($ value instanceof $ class || gettype ($ value ) === $ class ) {
107128 return true ;
108129 }
109130 }
131+
110132 return false ;
111133 }
112134
0 commit comments