Psr6CacheStore   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 42
rs 10
ccs 20
cts 20
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 3 1
A status() 0 3 1
A hit() 0 12 1
A getNormalizedItem() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
namespace Zenstruck\Governator\Store;
4
5
use Psr\Cache\CacheItemInterface;
6
use Psr\Cache\CacheItemPoolInterface;
7
use Zenstruck\Governator\Counter;
8
use Zenstruck\Governator\Key;
9
use Zenstruck\Governator\Store;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class Psr6CacheStore implements Store
15
{
16
    private CacheItemPoolInterface $cache;
17
18 12
    public function __construct(CacheItemPoolInterface $cache)
19
    {
20 12
        $this->cache = $cache;
21 12
    }
22
23 10
    public function hit(Key $key): Counter
24
    {
25 10
        $item = $this->getNormalizedItem($key);
26
27
        /** @var Counter $counter */
28 10
        $counter = $item->get()->addHit();
29
30 10
        $item->set($counter);
31 10
        $item->expiresAfter($counter->resetsIn());
32 10
        $this->cache->save($item);
33
34 10
        return $counter;
35
    }
36
37 3
    public function status(Key $key): Counter
38
    {
39 3
        return $this->getNormalizedItem($key)->get();
40
    }
41
42 11
    public function reset(Key $key): void
43
    {
44 11
        $this->cache->deleteItem((string) $key);
45 11
    }
46
47 11
    private function getNormalizedItem(Key $key): CacheItemInterface
48
    {
49 11
        $item = $this->cache->getItem((string) $key);
50
51 11
        if (!$item->get() instanceof Counter) {
52 11
            $item->set($key->createCounter());
53
        }
54
55 11
        return $item;
56
    }
57
}
58