Skip to content

Commit

Permalink
Updated in-memory counter
Browse files Browse the repository at this point in the history
  • Loading branch information
denisyukphp committed Sep 8, 2021
1 parent b112067 commit 0a9fbd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/Strategy/InMemoryCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ final class InMemoryCounter implements CounterInterface
/**
* @var int
*/
private $counter;
private $start;
/**
* @var array<string, int>
*/
private $counter = [];

public function __construct(int $counter = 0)
public function __construct(int $start = 0)
{
$this->counter = $counter;
$this->start = $start;
}

public function increment(string $key = 'default'): int
{
return $this->counter++;
if (!isset($this->counter[$key])) {
$this->counter[$key] = $this->start;
}

return $this->counter[$key]++;
}
}
4 changes: 1 addition & 3 deletions tests/Strategy/InMemoryCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public function testIncrement(): void
{
$inMemoryCounter = new InMemoryCounter(5);

for ($i = 5; $i < 10; $i++) {
$this->assertSame($i, $inMemoryCounter->increment());
}
$this->assertSame(5, $inMemoryCounter->increment());
}
}

0 comments on commit 0a9fbd5

Please sign in to comment.