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

Psr16CacheStore::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 Psr\SimpleCache\CacheInterface;
6
use Zenstruck\Governator\Counter;
7
use Zenstruck\Governator\Key;
8
use Zenstruck\Governator\Store;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class Psr16CacheStore implements Store
14
{
15
    private CacheInterface $cache;
16
17 12
    public function __construct(CacheInterface $cache)
18
    {
19 12
        $this->cache = $cache;
20 12
    }
21
22 10
    public function hit(Key $key): Counter
23
    {
24 10
        $counter = $this->status($key)->addHit();
25
26 10
        $this->cache->set((string) $key, $counter, $counter->resetsIn());
27
28 10
        return $counter;
29
    }
30
31 11
    public function status(Key $key): Counter
32
    {
33 11
        $counter = $this->cache->get((string) $key);
34
35 11
        if (!$counter instanceof Counter) {
36 11
            $counter = $key->createCounter();
37
        }
38
39 11
        return $counter;
40
    }
41
42 11
    public function reset(Key $key): void
43
    {
44 11
        $this->cache->delete((string) $key);
45 11
    }
46
}
47