Test Failed
Pull Request — master (#2)
by Joao
02:20
created

CacheItem::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace ByJG\Cache\Psr6;
4
5
use Psr\Cache\CacheItemInterface;
6
7
class CacheItem implements CacheItemInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $key;
13
    
14
    /**
15
     * @var mixed
16
     */
17
    protected $value;
18
    
19
    /**
20
     * @var boolean
21
     */
22
    protected $hit;
23
    
24
    /**
25
     * @var \DateTime
26
     */
27
    protected $expiration;
28
29
    /**
30
     * CacheItem constructor.
31
     * @param string $key
32
     * @param mixed $value
33
     * @param bool $hit
34
     */
35
    public function __construct($key, $value, $hit = true)
36
    {
37
        $this->key = $key;
38
        $this->value = $value;
39
        $this->hit = $hit;
40
        $this->expiresAt(null);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getKey()
47
    {
48
        return $this->key;
49
    }
50
    
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get()
55
    {
56
        return $this->isHit() ? $this->value : null;
57
    }
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function set($value = null)
62
    {
63
        $this->value = $value;
64
        $this->hit = !is_null($value);
65
        return $this;
66
    }
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function isHit()
71
    {
72
        return $this->hit;
73
    }
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function expiresAt($expiration)
78
    {
79
        if (is_null($expiration)) {
80
            $this->expiration = new \DateTime('now +1 year');
81
        } else {
82
            assert('$expiration instanceof \DateTimeInterface');
83
            $this->expiration = $expiration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expiration of type object<DateTimeInterface> is incompatible with the declared type object<DateTime> of property $expiration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
        }
85
        return $this;
86
    }
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function expiresAfter($time)
91
    {
92
        if (is_null($time)) {
93
            $this->expiration = new \DateTime('now +1 year');
94
        } elseif (is_numeric($time)) {
95
            $this->expiration = new \DateTime('now +' . $time . ' seconds');
96
        } else {
97
            assert('$time instanceof DateInterval');
98
            $expiration = new \DateTime();
99
            $expiration->add($time);
100
            $this->expiration = $expiration;
101
        }
102
        return $this;
103
    }
104
105
    /**
106
     * @return \DateTime
107
     */
108
    public function getExpiresAt()
109
    {
110
        return $this->expiration;
111
    }
112
    
113
    public function getExpiresInSecs()
114
    {
115
        return $this->getExpiresAt()->getTimestamp() - (new \DateTime('now'))->getTimestamp();
116
    }
117
}
118