CacheItem   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 114
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getKey() 0 4 1
A get() 0 4 1
A isHit() 0 4 1
A set() 0 6 1
A expiresAt() 0 12 3
A expiresAfter() 0 18 3
A getExpiresAt() 0 4 1
1
<?php
2
namespace Madewithlove\IlluminatePsrCacheBridge\Laravel;
3
4
use DateInterval;
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use Psr\Cache\CacheItemInterface;
8
9
class CacheItem implements CacheItemInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $key;
15
16
    /**
17
     * @var mixed|null
18
     */
19
    private $value;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hit;
25
26
    /**
27
     * @var \DateTimeInterface
28
     */
29
    private $expires;
30
31
    /**
32
     * @param string $key
33
     * @param mixed $value
34
     * @param bool $hit
35
     */
36 70
    public function __construct($key, $value = null, $hit = false)
37
    {
38 70
        $this->key = $key;
39 70
        $this->hit = boolval($hit);
40 70
        $this->value = $this->hit ? $value : null;
41 70
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 42
    public function getKey()
47
    {
48 42
        return $this->key;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 43
    public function get()
55
    {
56 43
        return $this->value;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 28
    public function isHit()
63
    {
64 28
        return $this->hit;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 42
    public function set($value)
71
    {
72 42
        $this->value = $value;
73
74 42
        return $this;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 19
    public function expiresAt($expires)
81
    {
82 19
        if ($expires instanceof DateTimeInterface && ! $expires instanceof DateTimeImmutable) {
83 5
            $timezone = $expires->getTimezone();
84 5
            $expires = DateTimeImmutable::createFromFormat('U', (string) $expires->getTimestamp(), $timezone);
85 5
            $expires->setTimezone($timezone);
86
        }
87
88 19
        $this->expires = $expires;
89
90 19
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 7
    public function expiresAfter($time)
97
    {
98 7
        if ($time === null) {
99 1
            $this->expires = null;
100
101 1
            return;
102
        }
103
104 6
        $this->expires = new DateTimeImmutable();
105
106 6
        if (! $time instanceof DateInterval) {
107 5
            $time = new DateInterval(sprintf('PT%sS', $time));
108
        }
109
110 6
        $this->expires = $this->expires->add($time);
111
112 6
        return $this;
113
    }
114
115
    /**
116
     * @return \DateTimeInterface
117
     */
118 45
    public function getExpiresAt()
119
    {
120 45
        return $this->expires;
121
    }
122
}
123