Passed
Push — master ( f9389a...e0bec9 )
by Mihail
04:05
created

CacheItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
dl 0
loc 78
ccs 22
cts 22
cp 1
rs 10
eloc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isHit() 0 3 1
A getExpiresAt() 0 3 1
A get() 0 3 1
A set() 0 5 1
A getKey() 0 3 1
A expiresAt() 0 9 2
A expiresAfter() 0 4 1
A __construct() 0 4 1
1
<?php
2
3
namespace Koded\Caching;
4
5
use Psr\Cache\CacheItemInterface;
6
7
8
abstract class CacheItem implements CacheItemInterface
9
{
10
    /** @var bool */
11
    protected $isHit = false;
12
13
    /** @var string Expects a valid cache key name */
14
    private $key;
15
16
    /** @var mixed */
17
    private $value;
18
    
19
    /** @var int Number of seconds for the expiration time */
20
    private $expiresAt;
21
22
23 483
    public function __construct($key, ?int $ttl)
24
    {
25 483
        $this->key = $key;
26 483
        $this->expiresAt = $ttl;
27 483
    }
28
29
30 237
    public function getKey(): string
31
    {
32 237
        return $this->key;
33
    }
34
35
36 216
    public function get()
37
    {
38 216
        return $this->value;
39
    }
40
41
42 175
    public function isHit(): bool
43
    {
44 175
        return $this->isHit;
45
    }
46
47
48 258
    public function set($value)
49
    {
50 258
        $this->value = $value;
51
52 258
        return $this;
53
    }
54
55
56 29
    public function expiresAfter($time)
57
    {
58
        // The TTL is calculated in the cache client instance
59 29
        return $this->expiresAt($time);
60
    }
61
62
63 57
    public function expiresAt($expiration)
64
    {
65 57
        $this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt);
66
67 57
        if ($this->expiresAt < 1) {
68 28
            $this->isHit = false;
69
        }
70
71 57
        return $this;
72
    }
73
74
    /**
75
     * Returns expiration seconds for the cache item.
76
     * NULL is reserved for clients who do not support expiry
77
     * to implement some custom logic around the TTL.
78
     *
79
     * This method is not part of the PSR-6.
80
     *
81
     * @return int|null
82
     */
83 251
    public function getExpiresAt(): ?int
84
    {
85 251
        return $this->expiresAt;
86
    }
87
}
88