Passed
Push — master ( 876065...f10f6d )
by Kevin
01:15
created

MemoryStore::status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Zenstruck\Governator\Store;
4
5
use Zenstruck\Governator\Counter;
6
use Zenstruck\Governator\Key;
7
use Zenstruck\Governator\Store;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
final class MemoryStore implements Store
13
{
14
    /** @var array<string, Counter> */
15
    private array $cache = [];
16
17 20
    public function hit(Key $key): Counter
18
    {
19 20
        return $this->cache[(string) $key] = $this->status($key)->addHit();
20
    }
21
22 21
    public function status(Key $key): Counter
23
    {
24 21
        $counter = $this->cache[(string) $key] ?? $key->createCounter();
25
26 21
        if (0 === $counter->resetsIn()) {
27 4
            $counter = $key->createCounter();
28
        }
29
30 21
        return $counter;
31
    }
32
33 12
    public function reset(Key $key): void
34
    {
35 12
        unset($this->cache[(string) $key]);
36 12
    }
37
}
38