Skip to content

Commit

Permalink
fix linter issue
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Poignant <[email protected]>
  • Loading branch information
thomaspoignant committed Aug 8, 2024
1 parent cb5035f commit 48b1ede
Show file tree
Hide file tree
Showing 18 changed files with 353 additions and 289 deletions.
1 change: 1 addition & 0 deletions providers/GoFeatureFlag/.php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.0.30","version":"3.57.1","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"tests\/unit\/controller\/OfrepApiTest.php":"ffc7e3ab94b4c58eb9b413fef1bef9de","tests\/unit\/GoFeatureFlagProviderTest.php":"062d381339c3bb95bd08b9fa05d1462a","tests\/TestCase.php":"efc0a05509f7101e640d29ec146c0aff","src\/util\/Validator.php":"9732fd68e5ebbac5b3139995077ce8de","src\/config\/Config.php":"eb1e5935642272c77c474adcbbcb18dd","src\/controller\/OfrepApi.php":"2c01a1ada0d53bb066b8c910d0d0de7d","src\/GoFeatureFlagProvider.php":"5dd7f706ef1705395a1f881b90744736","src\/model\/OfrepApiResponse.php":"b34e7d13b252e0c6a722dbaefcbf2862","src\/exception\/RateLimitedException.php":"09d2000c11a7ac0fd523c4d1f52d2f7c","src\/exception\/BaseGoffException.php":"4c7285b637c8ad99d1adcae77a823dd7","src\/exception\/UnknownOfrepException.php":"418a19d2eedd16d46712c524a3f49183","src\/exception\/BaseOfrepException.php":"272845dff81eba1807364a76ae2059d8","src\/exception\/UnauthorizedException.php":"07831e991ffb4fcd213c20a47de8d412","src\/exception\/InvalidConfigException.php":"9f3bf6f27ac4ba2b95e993cf0f45e636","src\/exception\/ParseException.php":"b641c7a21d0c498616bd904a1e0b0745","src\/exception\/InvalidContextException.php":"48cb0b86fa5fb2094a55fb058a5fb176","src\/exception\/FlagNotFoundException.php":"5ab85e04a92c770a86c5a4d0afb69292"}}
2 changes: 1 addition & 1 deletion providers/GoFeatureFlag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $api->setProvider($provider);
$client = $api->getClient();
$evaluationContext = new MutableEvaluationContext(
"214b796a-807b-4697-b3a3-42de0ec10a37",
new Attributes(["email" => "[email protected]"])
new Attributes(["email" => '[email protected]'])
);

$value = $client->getBooleanDetails('integer_key', false, $evaluationContext);
Expand Down
43 changes: 28 additions & 15 deletions providers/GoFeatureFlag/src/GoFeatureFlagProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace OpenFeature\Providers\GoFeatureFlag;

use OpenFeature\Providers\GoFeatureFlag\config\Config;
use OpenFeature\Providers\GoFeatureFlag\controller\OfrepApi;
use OpenFeature\Providers\GoFeatureFlag\exception\BaseOfrepException;
use OpenFeature\Providers\GoFeatureFlag\exception\InvalidConfigException;
use OpenFeature\Providers\GoFeatureFlag\util\Validator;
use OpenFeature\implementation\common\Metadata;
use OpenFeature\implementation\provider\AbstractProvider;
use OpenFeature\implementation\provider\ResolutionDetailsBuilder;
Expand All @@ -13,15 +18,16 @@
use OpenFeature\interfaces\provider\Provider;
use OpenFeature\interfaces\provider\Reason;
use OpenFeature\interfaces\provider\ResolutionDetails;
use OpenFeature\Providers\GoFeatureFlag\config\Config;
use OpenFeature\Providers\GoFeatureFlag\controller\OfrepApi;
use OpenFeature\Providers\GoFeatureFlag\exception\BaseOfrepException;
use OpenFeature\Providers\GoFeatureFlag\exception\InvalidConfigException;
use OpenFeature\Providers\GoFeatureFlag\util\Validator;
use Throwable;

use function array_key_exists;
use function gettype;
use function implode;
use function is_array;

class GoFeatureFlagProvider extends AbstractProvider implements Provider
{
protected static string $CLIENT_NAME = 'GO Feature Flag Provider';
protected static string $NAME = 'GO Feature Flag Provider';
private OfrepApi $ofrepApi;

/**
Expand All @@ -30,15 +36,15 @@ class GoFeatureFlagProvider extends AbstractProvider implements Provider
public function __construct(Config $config)
{
Validator::validateConfig($config);
if (is_array($config->getCustomHeaders()) && !array_key_exists("Content-Type", $config->getCustomHeaders())) {
$config->getCustomHeaders()["Content-Type"] = "application/json";
if (is_array($config->getCustomHeaders()) && !array_key_exists('Content-Type', $config->getCustomHeaders())) {
$config->getCustomHeaders()['Content-Type'] = 'application/json';
}
$this->ofrepApi = new OfrepApi($config);
}

public function getMetadata(): Metadata
{
return new Metadata(self::$CLIENT_NAME);
return new Metadata(static::$NAME);
}

public function resolveBooleanValue(string $flagKey, bool $defaultValue, ?EvaluationContext $context = null): ResolutionDetails
Expand All @@ -49,7 +55,7 @@ public function resolveBooleanValue(string $flagKey, bool $defaultValue, ?Evalua
/**
* @param array<string> $allowedClasses
*/
private function evaluate(string $flagKey, mixed $defaultValue, array $allowedClasses, EvaluationContext $evaluationContext = null): ResolutionDetails
private function evaluate(string $flagKey, mixed $defaultValue, array $allowedClasses, ?EvaluationContext $evaluationContext = null): ResolutionDetails
{
try {
Validator::validateEvaluationContext($evaluationContext);
Expand All @@ -60,8 +66,9 @@ private function evaluate(string $flagKey, mixed $defaultValue, array $allowedCl
if ($apiResp->isError()) {
$err = new ResolutionError(
$apiResp->getErrorCode() ?? ErrorCode::GENERAL(),
$apiResp->getErrorDetails()
$apiResp->getErrorDetails(),
);

return (new ResolutionDetailsBuilder())
->withValue($defaultValue)
->withError($err)
Expand All @@ -74,39 +81,45 @@ private function evaluate(string $flagKey, mixed $defaultValue, array $allowedCl
->withReason(Reason::ERROR)
->withError(new ResolutionError(
ErrorCode::TYPE_MISMATCH(),
"Invalid type for $flagKey, got " . gettype($apiResp->getValue()) . " expected " . implode(", ", $allowedClasses)))
"Invalid type for $flagKey, got " . gettype($apiResp->getValue()) . ' expected ' . implode(', ', $allowedClasses),
))
->withValue($defaultValue)
->build();
}

return (new ResolutionDetailsBuilder())
->withValue($apiResp->getValue())
->withReason($apiResp->getReason())
->withVariant($apiResp->getVariant())
->build();

} catch (BaseOfrepException $e) {
$err = new ResolutionError($e->getErrorCode(), $e->getMessage());

return (new ResolutionDetailsBuilder())
->withValue($defaultValue)
->withError($err)
->withReason(Reason::ERROR)
->build();
} catch (\Exception $e) {
} catch (Throwable $e) {
return (new ResolutionDetailsBuilder())
->withValue($defaultValue)
->withError(new ResolutionError(ErrorCode::GENERAL(), "An error occurred while evaluating the flag: " . $e->getMessage()))
->withError(new ResolutionError(ErrorCode::GENERAL(), 'An error occurred while evaluating the flag: ' . $e->getMessage()))
->withReason(Reason::ERROR)
->build();
}
}

/**
* @param array<string> $allowedClasses
*/
private function isValidType(mixed $value, array $allowedClasses): bool
{
foreach ($allowedClasses as $class) {
if ($value instanceof $class || gettype($value) === $class) {
return true;
}
}

return false;
}

Expand Down
12 changes: 6 additions & 6 deletions providers/GoFeatureFlag/src/config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
class Config
{
private string $endpoint;
/**
* @var array<string, string>
*/
private array $customHeaders = [];

public function __construct(string $endpoint, ?string $apiKey = '', ?array $custom_headers = [])
public function __construct(string $endpoint, ?string $apiKey = '', ?array $customHeaders = [])
{
$this->endpoint = $endpoint;
$this->customHeaders = $custom_headers;
$this->customHeaders = $customHeaders;
if ($apiKey !== null && $apiKey !== '') {
$this->customHeaders['Authorization'] = 'Bearer ' . $apiKey;
}
}

/**
* @return string
*/
public function getEndpoint(): string
{
return $this->endpoint;
}

/**
* @return array
* @return array<string, string>
*/
public function getCustomHeaders(): array
{
Expand Down
29 changes: 20 additions & 9 deletions providers/GoFeatureFlag/src/controller/OfrepApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace OpenFeature\Providers\GoFeatureFlag\controller;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use OpenFeature\interfaces\flags\EvaluationContext;
use OpenFeature\Providers\GoFeatureFlag\config\Config;
use OpenFeature\Providers\GoFeatureFlag\exception\BaseOfrepException;
use OpenFeature\Providers\GoFeatureFlag\exception\FlagNotFoundException;
Expand All @@ -16,7 +14,17 @@
use OpenFeature\Providers\GoFeatureFlag\exception\UnauthorizedException;
use OpenFeature\Providers\GoFeatureFlag\exception\UnknownOfrepException;
use OpenFeature\Providers\GoFeatureFlag\model\OfrepApiResponse;
use OpenFeature\interfaces\flags\EvaluationContext;
use Psr\Http\Message\ResponseInterface;
use Throwable;

use function array_merge;
use function is_numeric;
use function json_decode;
use function json_encode;
use function rtrim;
use function strtotime;
use function time;

class OfrepApi
{
Expand Down Expand Up @@ -51,10 +59,10 @@ public function evaluate(string $flagKey, EvaluationContext $evaluationContext):
}
}

$base_uri = $this->options->getEndpoint();
$evaluateApiPath = rtrim($base_uri, '/') . "/ofrep/v1/evaluate/flags/{$flagKey}";
$baseUri = $this->options->getEndpoint();
$evaluateApiPath = rtrim($baseUri, '/') . "/ofrep/v1/evaluate/flags/{$flagKey}";
$headers = [
'Content-Type' => 'application/json'
'Content-Type' => 'application/json',
];

if ($this->options->getCustomHeaders() !== null) {
Expand All @@ -63,13 +71,13 @@ public function evaluate(string $flagKey, EvaluationContext $evaluationContext):

$fields = array_merge(
$evaluationContext->getAttributes()->toArray(),
['targetingKey' => $evaluationContext->getTargetingKey()]
['targetingKey' => $evaluationContext->getTargetingKey()],
);

$requestBody = json_encode(['context' => $fields]);
$response = $this->client->post($evaluateApiPath, [
'headers' => $headers,
'body' => $requestBody
'body' => $requestBody,
]);

switch ($response->getStatusCode()) {
Expand All @@ -84,13 +92,14 @@ public function evaluate(string $flagKey, EvaluationContext $evaluationContext):
throw new FlagNotFoundException($flagKey, $response);
case 429:
$this->parseRetryLaterHeader($response);

throw new RateLimitedException($response);
default:
throw new UnknownOfrepException($response);
}
} catch (BaseOfrepException $e) {
throw $e;
} catch (GuzzleException|Exception $e) {
} catch (GuzzleException | Throwable $e) {
throw new UnknownOfrepException(null, $e);
}
}
Expand All @@ -101,6 +110,7 @@ public function evaluate(string $flagKey, EvaluationContext $evaluationContext):
private function parseSuccessResponse(ResponseInterface $response): OfrepApiResponse
{
$parsed = json_decode($response->getBody()->getContents(), true);

return OfrepApiResponse::createSuccessResponse($parsed);
}

Expand All @@ -110,6 +120,7 @@ private function parseSuccessResponse(ResponseInterface $response): OfrepApiResp
private function parseErrorResponse(ResponseInterface $response): OfrepApiResponse
{
$parsed = json_decode($response->getBody()->getContents(), true);

return OfrepApiResponse::createErrorResponse($parsed);
}

Expand All @@ -119,7 +130,7 @@ private function parseRetryLaterHeader(ResponseInterface $response): void
if ($retryAfterHeader) {
if (is_numeric($retryAfterHeader)) {
// Retry-After is in seconds
$this->retryAfter = time() + (int)$retryAfterHeader;
$this->retryAfter = time() + (int) $retryAfterHeader;
} else {
// Retry-After is in HTTP-date format
$this->retryAfter = strtotime($retryAfterHeader);
Expand Down
8 changes: 5 additions & 3 deletions providers/GoFeatureFlag/src/exception/BaseGoffException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

namespace OpenFeature\Providers\GoFeatureFlag\exception;

use Exception;
use OpenFeature\interfaces\provider\ErrorCode;
use Psr\Http\Message\ResponseInterface;
use Throwable;

abstract class BaseGoffException extends \Exception
abstract class BaseGoffException extends Exception
{
private string $customMessage;
private ?ResponseInterface $response;
private ErrorCode $errorCode;

public function __construct(string $message, ErrorCode $errorCode, ?ResponseInterface $response, int $code = 0, \Exception $previous = null)
public function __construct(string $message, ErrorCode $errorCode, ?ResponseInterface $response, int $code = 0, ?Throwable $previous = null)
{
$this->customMessage = $message;
$this->response = $response;
Expand All @@ -35,4 +37,4 @@ public function getErrorCode(): ErrorCode
{
return $this->errorCode;
}
}
}
11 changes: 5 additions & 6 deletions providers/GoFeatureFlag/src/exception/BaseOfrepException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

namespace OpenFeature\Providers\GoFeatureFlag\exception;

use Exception;
use OpenFeature\interfaces\provider\ErrorCode;
use Psr\Http\Message\ResponseInterface;
use Throwable;

abstract class BaseOfrepException extends \Exception
abstract class BaseOfrepException extends Exception
{
private string $customMessage;
private ?ResponseInterface $response;
private ErrorCode $errorCode;

public function __construct(string $message, ErrorCode $errorCode, ?ResponseInterface $response, int $code = 0, \Exception $previous = null)
public function __construct(string $message, ErrorCode $errorCode, ?ResponseInterface $response, int $code = 0, ?Throwable $previous = null)
{
$this->customMessage = $message;
$this->response = $response;
Expand All @@ -31,11 +33,8 @@ public function getResponse(): ?ResponseInterface
return $this->response;
}

/**
* @return ErrorCode
*/
public function getErrorCode(): ErrorCode
{
return $this->errorCode;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace OpenFeature\Providers\GoFeatureFlag\exception;

use OpenFeature\interfaces\provider\ErrorCode;
Expand Down
11 changes: 8 additions & 3 deletions providers/GoFeatureFlag/src/exception/InvalidConfigException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php

declare(strict_types=1);

namespace OpenFeature\Providers\GoFeatureFlag\exception;

class InvalidConfigException extends \Exception
use Exception;
use Throwable;

class InvalidConfigException extends Exception
{
private string $customMessage;

public function __construct(string $message, int $code = 0, \Exception $previous = null)
public function __construct(string $message, int $code = 0, ?Throwable $previous = null)
{
$this->customMessage = $message;
parent::__construct($message, $code, $previous);
Expand All @@ -16,4 +21,4 @@ public function getCustomMessage(): string
{
return $this->customMessage;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace OpenFeature\Providers\GoFeatureFlag\exception;

use OpenFeature\interfaces\provider\ErrorCode;

class InvalidContextException extends BaseOfrepException
{

public function __construct(string $message)
{
$code = 1006;
Expand Down
5 changes: 4 additions & 1 deletion providers/GoFeatureFlag/src/exception/ParseException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace OpenFeature\Providers\GoFeatureFlag\exception;

use OpenFeature\interfaces\provider\ErrorCode;
use Throwable;

class ParseException extends BaseOfrepException
{
public function __construct(string $message, \Exception $previous = null)
public function __construct(string $message, ?Throwable $previous = null)
{
$code = 1005;
parent::__construct($message, ErrorCode::PARSE_ERROR(), null, $code, $previous);
Expand Down
Loading

0 comments on commit 48b1ede

Please sign in to comment.