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

MemoryStore   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 24
rs 10
ccs 9
cts 9
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A status() 0 9 2
A reset() 0 3 1
A hit() 0 3 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