Completed
Push — master ( 1e55d9...9d27fa )
by Hannes
02:36
created

CacheItem::getTTL()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 8.8571
cc 6
eloc 8
nc 6
nop 0
crap 6.0493
1
<?php
2
namespace Madewithlove\IlluminatePsrCacheBridge\Laravel;
3
4
use DateInterval;
5
use DateTimeInterface;
6
use Psr\Cache\CacheItemInterface;
7
8
class CacheItem implements CacheItemInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $key;
14
15
    /**
16
     * @var mixed|null
17
     */
18
    private $value;
19
20
    /**
21
     * @var bool
22
     */
23
    private $hit;
24
25
    /**
26
     * @var \DateTimeInterface|\DateInterval|int
27
     */
28
    private $expires;
29
30
    /**
31
     * @param string $key
32
     * @param mixed $value
33
     * @param bool $hit
34
     */
35 31
    public function __construct($key, $value = null, $hit = false)
36
    {
37 31
        $this->key = $key;
38 31
        $this->value = $value;
39 31
        $this->hit = boolval($hit);
40 31
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 18
    public function getKey()
46
    {
47 18
        return $this->key;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 20
    public function get()
54
    {
55 20
        return $this->value;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 15
    public function isHit()
62
    {
63 15
        return $this->hit;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 9
    public function set($value)
70
    {
71 9
        $this->value = $value;
72
73 9
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 5
    public function expiresAt($expiration)
80
    {
81 5
        $this->expires = $expiration;
82
83 5
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 4
    public function expiresAfter($time)
90
    {
91 4
        $this->expires = $time;
92
93 4
        return $this;
94
    }
95
96
    /**
97
     * @return int|null
98
     *   The amount of minutes this item should stay alive. Or null when no expires is given.
99
     */
100 21
    public function getTTL()
101
    {
102 21
        if (is_int($this->expires)) {
103 3
            return floor($this->expires / 60.0);
104
        }
105
106 18
        if ($this->expires instanceof DateTimeInterface) {
107 3
            $diff = (new DateTime())->diff($this->expires);
108
109
            return boolval($diff->invert) ? 0 : $diff->i;
110
        }
111
112 15
        if ($this->expires instanceof DateInterval) {
113 3
            return boolval($this->expires->invert) ? 0 : $this->expires->i;
114
        }
115 12
    }
116
}
117