Completed
Push — master ( c1245a...219d8a )
by Hannes
03:55
created

CacheItem::getTTL()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
cc 6
eloc 8
nc 6
nop 0
crap 6

1 Method

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