Skip to content

Commit

Permalink
update LocalEvaluationClient - add getFlagConfigs method, change meth…
Browse files Browse the repository at this point in the history
…od name of 'start' to 'refreshFlagConfigs'
  • Loading branch information
tyiuhc committed Jan 24, 2024
1 parent 48f682a commit 505c633
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ if ($variant) {
$experiment = new \AmplitudeExperiment\Experiment();
$client = $experiment->initializeLocal('<DEPLOYMENT_KEY>');

// (2) Start the local evaluation client.
$client->start();
// (2) Fetch latest flag configurations for the local evaluation client.
$client->refreshFlagConfigs();

// (3) Evaluate a user.
$user = \AmplitudeExperiment\User::builder()
Expand Down
8 changes: 1 addition & 7 deletions src/Flag/FlagConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ public function __construct(FlagConfigFetcher $fetcher, LoggerInterface $logger,
$this->cache = $bootstrap;
}

public function start(): void
{
$this->logger->debug('[Experiment] Flag service - start');
$this->refresh();
}

private function refresh(): void
public function refresh(): void
{
$this->logger->debug('[Experiment] Flag config update');
try {
Expand Down
15 changes: 12 additions & 3 deletions src/Local/LocalEvaluationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function __construct(string $apiKey, ?LocalEvaluationConfig $config = nul
}

/**
* Fetch initial flag configurations.
* Fetch latest flag configurations.
*/
public function start(): void
public function refreshFlagConfigs(): void
{
$this->flagConfigService->start();
$this->flagConfigService->refresh();
}

/**
Expand Down Expand Up @@ -80,6 +80,15 @@ public function evaluate(User $user, array $flagKeys = []): array
return $results;
}


/**
* @return array<string, mixed> flag configurations.
*/
public function getFlagConfigs(): array
{
return $this->flagConfigService->getFlagConfigs();
}

private function initializeAssignmentService(?AssignmentConfig $config): void
{
if ($config) {
Expand Down
12 changes: 11 additions & 1 deletion tests/Local/LocalEvaluationClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()

public function setUp(): void
{
$this->client->start();
$this->client->refreshFlagConfigs();
}

public function testEvaluateAllFlags()
Expand Down Expand Up @@ -70,4 +70,14 @@ public function testEvaluateWithDependenciesVariantHeldOut()
$this->assertEquals(null, $variant->payload);
$this->assertTrue($variant->metadata["default"]);
}

public function testGetFlagConfigs()
{
$flagConfigs = $this->client->getFlagConfigs();
$bootstrapClient = new LocalEvaluationClient('', LocalEvaluationConfig::builder()->bootstrap($flagConfigs)->build());
$variants = $bootstrapClient->evaluate($this->testUser);
$variant = $variants['sdk-local-evaluation-ci-test'];
$this->assertEquals("on", $variant->key);
$this->assertEquals("payload", $variant->payload);
}
}

0 comments on commit 505c633

Please sign in to comment.