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

MemoryStore::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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