Completed
Push — master ( a69dc8...f2f723 )
by Franck
11:05
created

Item::expiresAt()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 22
loc 22
ccs 13
cts 13
cp 1
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Cache\PsrCache;
14
15
use Psr\Cache\CacheItemInterface as ItemInterface;
16
use Psr\Cache\CacheItemPoolInterface as ItemPoolInterface;
17
18
class Item implements ItemInterface
19
{
20
    const DEFAULT_EXPIRATION = 'now +1 year';
21
22
    /**
23
     * The pool that the item belongs to.
24
     * @var ItemPoolInterface
25
     */
26
    // protected $pool;
27
28
    /**
29
     * The cache key for the item.
30
     * @var string
31
     */
32
    protected $key;
33
34
    /**
35
     * The raw (unserialized) cached value.
36
     * @var mixed
37
     */
38
    protected $value;
39
40
    /**
41
     * Wether the item has been saved to the cache yet.
42
     * @var bool
43
     */
44
    protected $hit = false;
45
46
    /**
47
     * The expiration date.
48
     * @var \DateTime
49
     */
50
    protected $expiration;
51
52
    /**
53
     * Constructs a new Item.
54
     * You should never use this directly. It is used internally to create items
55
     * from the pool.
56
     * @param string                 $key   The item key
57
     * @param mixed                  $value The item value (unserialized)
58
     * @param \DateTime|integer|null $ttl
59
     * @param bool                   $hit   Was this item retrived from cache?
60
     */
61 1003
    public function __construct($key, $value = null, $ttl = null, $hit = false)
62
    {
63 1003
        if (strpbrk($key, '{}()/\@:')) {
64 34
            throw new InvalidArgumentException(
65
                'Item key contains an invalide character.' . $key
66 34
            );
67
        }
68 1003
        $this->key = $key;
69 1003
        $this->value = $value;
70 1003
        $this->hit = $hit;
71 1003
        $this->expiresAt($ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl defined by parameter $ttl on line 61 can also be of type integer or object<DateTime>; however, Apix\Cache\PsrCache\Item::expiresAt() does only seem to accept object<DateTimeInterface>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72 1003
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 425
    public function getKey()
78
    {
79 425
        return $this->key;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 459
    public function get()
86
    {
87 459
        return $this->hit ? $this->value : null;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 391
    public function set($value = null)
94
    {
95 391
        $this->value = $value;
96 391
        $this->hit = false; // TODO: check wether we should we do this?
97
98 391
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 459
    public function isHit()
105
    {
106 459
        return $this->hit;
107
    }
108
109
    /**
110
     * @deprecated
111
     * @codeCoverageIgnore
112
     */
113
    public function exists()
114
    {
115
        return $this->hit;
116
    }
117
118
    /**
119
     * @deprecated
120
     * @codeCoverageIgnore
121
     */
122
    public function isRegenerating()
123
    {
124
        return false;
125
    }
126
127
    /**
128
     * @deprecated
129
     * @see Item::expiresAt()
130
     * @codeCoverageIgnore
131
     */
132
    public function setExpiration($ttl = null)
133
    {
134
        if (is_int($expiration)) {
0 ignored issues
show
Bug introduced by
The variable $expiration does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
135
            $ttl = new \DateTime('now +' . $ttl . ' seconds');
136
        }
137
138
        return $this->expiresAt($ttl);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 272
    public function getExpiration()
145
    {
146 272
        return $this->expiration;
147
    }
148
149
    /**
150
     * Returns the time to live in second.
151
     *
152
     * @return integer
153
     */
154 459
    public function getTtlInSecond()
155
    {
156 459
        return $this->expiration->format('U') - time();
157
    }
158
159
    /**
160
     * Sets the cache hit for this item.
161
     *
162
     * @param  boolean $hit
163
     * @return static
164
     *                     The invoked object.
165
     */
166 391
    public function setHit($hit)
167
    {
168 391
        $this->hit = (bool) $hit;
169
170 391
        return $this;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 1003 View Code Duplication
    public function expiresAt($expiration = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178 1003
        if ($expiration instanceof \DateTime) {
179 68
            $this->expiration = $expiration;
180
 
181 1003
        } elseif (is_int($expiration)) {
182 221
            $this->expiration = new \DateTime(
183 221
                'now +' . $expiration . ' seconds'
184 221
            );
185
186 1003
        } elseif (null === $expiration) {
187 1003
            $this->expiration = new \DateTime(self::DEFAULT_EXPIRATION);
188
        
189 1003
        } else {
190
191 34
            throw new InvalidArgumentException(
192
                'Integer or \DateTime object expected.'
193 34
            );
194
        }
195
196 1003
        return $this;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 170 View Code Duplication
    public function expiresAfter($time)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204 170
        if ($time instanceof \DateInterval) {
205 34
            $this->expiration = new \DateTime();
206 34
            $this->expiration->add($time);
207
208 170
        } elseif (is_int($time)) {
209 68
            $this->expiration = new \DateTime('now +' . $time . ' seconds');
210
211 136
        } elseif (null === $time) {
212 34
            $this->expiration = new \DateTime(self::DEFAULT_EXPIRATION);
213
        
214 34
        } else {
215
216 34
            throw new InvalidArgumentException(
217
                'Integer or \DateInterval object expected.'
218 34
            );
219
        }
220
221 136
        return $this;
222
    }
223
224
}
225