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

Psr16CacheStore::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 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