Passed
Push — master ( 2c97eb...c298b6 )
by Petr
02:20
created

ItemAdapter::getKey()   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 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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