ItemAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 3 1
A isHit() 0 3 1
A get() 0 3 2
A __construct() 0 4 1
A set() 0 4 1
A isExpired() 0 8 2
1
<?php
2
3
namespace kalanis\kw_cache_psr\Adapters\PoolCache;
4
5
6
use kalanis\kw_cache_psr\Traits\TClock;
7
use kalanis\kw_cache_psr\Traits\TExpire;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Clock\ClockInterface;
10
11
12
/**
13
 * Class ItemAdapter
14
 * @package kalanis\kw_cache_psr\Adapters\PoolCache
15
 * Item adapter for PSR Cache Interface
16
 */
17
class ItemAdapter implements CacheItemInterface
18
{
19
    use TClock;
20
    use TExpire;
21
22
    /** @var mixed|null */
23
    protected $cache = null;
24
    protected string $key = '';
25
26 10
    public function __construct(string $key, ClockInterface $clock = null)
27
    {
28 10
        $this->key = $key;
29 10
        $this->initTClock($clock);
30 10
    }
31
32 8
    public function getKey()
33
    {
34 8
        return $this->key;
35
    }
36
37 3
    public function get()
38
    {
39 3
        return $this->isHit() ? $this->cache : null;
40
    }
41
42 5
    public function isHit()
43
    {
44 5
        return !$this->isExpired();
45
    }
46
47 3
    public function set($value)
48
    {
49 3
        $this->cache = $value;
50 3
        return $this;
51
    }
52
53 5
    protected function isExpired(): bool
54
    {
55 5
        $expireTime = $this->getExpire();
56 5
        if (is_null($expireTime)) {
57
            // null = never
58 5
            return false;
59
        }
60 2
        return $this->getClocks()->now()->getTimestamp() >= $expireTime;
61
    }
62
}
63