generated from worksome/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
255 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Exceptions\Bucket; | ||
|
||
use Exception; | ||
|
||
class BucketInvalidResponseException extends Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
'Invalid response from Bucket.' | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Exceptions/Bucket/BucketMissingAccessTokenException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Exceptions\Bucket; | ||
|
||
use Exception; | ||
|
||
final class BucketMissingAccessTokenException extends Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
'Missing access token env variable.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Providers\Bucket; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\BadResponseException; | ||
use GuzzleHttp\HandlerStack; | ||
use Illuminate\Support\Arr; | ||
use Psr\Log\LoggerInterface; | ||
use SensitiveParameter; | ||
use Worksome\FeatureFlags\Exceptions\Bucket\BucketInvalidResponseException; | ||
|
||
class BucketClient | ||
{ | ||
public const string DEFAULT_BASE_URI = 'https://front-eu.bucket.co'; | ||
|
||
private Client $client; | ||
|
||
public function __construct( | ||
private readonly string $baseUri, | ||
#[SensitiveParameter] | ||
private readonly string $sdkKey, | ||
private readonly array $options, | ||
private readonly LoggerInterface $logger, | ||
) { | ||
$stack = HandlerStack::create(); | ||
|
||
$defaults = [ | ||
'headers' => self::defaultHeaders($this->sdkKey, $this->options), | ||
'timeout' => Arr::get($this->options, 'timeout'), | ||
'connect_timeout' => Arr::get($this->options, 'connect_timeout'), | ||
'handler' => $stack, | ||
'debug' => Arr::get($this->options, 'debug', false), | ||
'base_uri' => $this->baseUri, | ||
]; | ||
|
||
$this->client = new Client($defaults); | ||
} | ||
|
||
public function getFeature(string $value, bool $defaultValue, BucketContext|null $context = null): bool | ||
{ | ||
$features = $this->getAllFeatures($context); | ||
|
||
if (! isset($features[$value])) { | ||
$this->logger->warning("BucketClient::getFeature: Feature flag does not exist for key: {$value}"); | ||
|
||
return $defaultValue; | ||
} | ||
|
||
return $features[$value]; | ||
} | ||
|
||
/** @return array<string, bool> */ | ||
public function getAllFeatures(BucketContext|null $context = null): array | ||
{ | ||
$transformedContext = $context?->transform() ?? []; | ||
|
||
try { | ||
$response = $this->client->get('/features/enabled', [ | ||
'query' => $transformedContext, | ||
]); | ||
|
||
$body = $response->getBody(); | ||
|
||
/** @var array{success?: bool, features?: array<string, array{isEnabled: bool}>} $response */ | ||
$response = json_decode($body->getContents(), true); | ||
|
||
throw_unless($response['success'] ?? false, BucketInvalidResponseException::class); | ||
|
||
/** @phpstan-ignore return.type */ | ||
return Arr::mapWithKeys( | ||
$response['features'] ?? [], | ||
fn (array $value, string $key): array => [$key => $value['isEnabled']], | ||
); | ||
} catch (BadResponseException $e) { | ||
$this->logger->warning( | ||
"BucketClient::getAllFeatures: (code {$e->getResponse()->getStatusCode()}) {$e->getMessage()}" | ||
); | ||
|
||
return []; | ||
} | ||
} | ||
|
||
private static function defaultHeaders(string $sdkKey, array $options): array | ||
{ | ||
return [ | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
'Authorization' => "Bearer {$sdkKey}", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Providers\Bucket; | ||
|
||
use Illuminate\Support\Arr; | ||
|
||
readonly class BucketContext | ||
{ | ||
/** @param array<string, mixed> $context */ | ||
public function __construct(public string $id, public string|null $email = null, public array $context = []) | ||
{ | ||
} | ||
|
||
public static function anonymous(): self | ||
{ | ||
return new self('anonymous'); | ||
} | ||
|
||
public function transform(): array | ||
{ | ||
return Arr::dot([ | ||
'context' => [ | ||
'user' => [ | ||
'id' => $this->id, | ||
'email' => $this->email, | ||
], | ||
... $this->context, | ||
], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Worksome\FeatureFlags\Providers\Bucket; | ||
|
||
use Illuminate\Support\Arr; | ||
use Psr\Log\LoggerInterface; | ||
use Worksome\FeatureFlags\Contracts\FeatureFlagEnum; | ||
use Worksome\FeatureFlags\Contracts\FeatureFlagsProvider; | ||
use Worksome\FeatureFlags\FeatureFlagUser; | ||
|
||
class BucketProvider implements FeatureFlagsProvider | ||
{ | ||
private BucketContext|null $context = null; | ||
|
||
private BucketClient|null $client = null; | ||
|
||
public function __construct( | ||
public readonly array $config, | ||
public readonly LoggerInterface $logger, | ||
) { | ||
/** @var string $key */ | ||
$key = Arr::get($config, 'key'); | ||
/** @var string $host */ | ||
$host = Arr::get($config, 'host', BucketClient::DEFAULT_BASE_URI); | ||
/** @var array $options */ | ||
$options = Arr::get($config, 'options'); | ||
|
||
/** @var string|null $key */ | ||
$key = Arr::get($config, 'key'); | ||
|
||
if ($key) { | ||
$this->client = new BucketClient($host, $key, $options, $logger); | ||
} | ||
} | ||
|
||
public function setUser(FeatureFlagUser $user): void | ||
{ | ||
$id = (string) $user->id; | ||
|
||
$this->context = new BucketContext( | ||
id: $id, | ||
email: $user->email, | ||
context: $user->custom, | ||
); | ||
} | ||
|
||
public function setAnonymousUser(): void | ||
{ | ||
$this->context = BucketContext::anonymous(); | ||
} | ||
|
||
public function flag(FeatureFlagEnum $flag): bool | ||
{ | ||
assert(is_string($flag->value)); | ||
|
||
$client = $this->client; | ||
|
||
if ($client === null) { | ||
return false; | ||
} | ||
|
||
if ($this->context === null) { | ||
$this->setAnonymousUser(); | ||
} | ||
|
||
assert($this->context !== null); | ||
|
||
return $this->client->getFeature($flag->value, false, context: $this->context); | ||
} | ||
} |