Skip to content

Commit

Permalink
Change InMemoryStorage to generating new ids
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodp committed May 15, 2024
1 parent 8fac220 commit 7b45d8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/InMemoryStorage.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@

} );

it( 'generates an incremental id every new flag', function() {
$storage = new InMemoryStorage();
$foo = $storage->touch( 'foo' );
expect( $foo->metadata->id )->toBe( 1 );
$bar = $storage->touch( 'bar' );
expect( $bar->metadata->id )->toBe( 2 );
} );

} );
5 changes: 4 additions & 1 deletion src/storages/InMemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class InMemoryStorage implements FlagStorage {
/** @var array<string, FlagData> */
private array $flags = [];

private int $lastId = 0;

public function isEnabled( string $key ): bool {
$flag = $this->get( $key );
return $flag === null ? false : $flag->enabled;
Expand All @@ -21,8 +23,9 @@ public function get( string $key ): ?FlagData {

/** @inheritDoc */
public function touch( string $key, ?bool $enabled = null ): ?FlagData {

$flag = $this->get( $key ) ??
new FlagData( $key, false, new FlagMetadata() );
new FlagData( $key, false, new FlagMetadata( ++$this->lastId ) );

$this->set( $key, $flag );

Expand Down

0 comments on commit 7b45d8d

Please sign in to comment.