Completed
Push — master ( bf7173...875f6a )
by Franck
13:42
created

Item::normalizedKey()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 2
cts 2
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 2
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 1037
    public function __construct($key, $value = null, $ttl = null, $hit = false)
62
    {
63 1037
        $this->key = self::normalizedKey($key);
64 34
        $this->value = $value;
65
        $this->hit = $hit;
66 34
        $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...
67
    }
68 1037
69 1037
    /**
70 1037
     * Returns a normalised key.
71 1037
     *
72 1037
     * @return string
73
     */
74
    public static function normalizedKey($key)
75
    {
76
        if (!is_string($key) || empty($key) || strpbrk($key, '{}()/\@:') ) {
77 459
            throw new InvalidArgumentException(
78
                'Item key (' . var_export($key, true) . ') is invalid.'
79 459
            );
80
        }
81
82
        return $key;
83
    }
84
85 493
    /**
86
     * {@inheritdoc}
87 493
     */
88
    public function getKey()
89
    {
90
        return $this->key;
91
    }
92
93 425
    /**
94
     * {@inheritdoc}
95 425
     */
96 425
    public function get()
97
    {
98 425
        return $this->hit ? $this->value : null;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 493
    public function set($value = null)
105
    {
106 493
        $this->value = $value;
107
        $this->hit = false;
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function isHit()
116
    {
117
        return  $this->hit && $this->getTtlInSecond() > 0;
118
    }
119
120
    /**
121
     * @deprecated
122
     * @codeCoverageIgnore
123
     */
124
    public function exists()
125
    {
126
        return $this->hit;
127
    }
128
129
    /**
130
     * @deprecated
131
     * @codeCoverageIgnore
132
     */
133
    public function isRegenerating()
134
    {
135
        return false;
136
    }
137
138
    /**
139
     * @deprecated
140
     * @see Item::expiresAt()
141
     * @codeCoverageIgnore
142
     */
143
    public function setExpiration($ttl = null)
144 272
    {
145
        if (is_int($ttl)) {
146 272
            $ttl = new \DateTime('now +' . $ttl . ' seconds');
147
        }
148
149
        return $this->expiresAt($ttl);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154 493
     */
155
    public function getExpiration()
156 493
    {
157
        return $this->expiration;
158
    }
159
160
    /**
161
     * Returns the time to live in second.
162
     *
163
     * @return integer
164
     */
165
    public function getTtlInSecond()
166 425
    {
167
        return $this->expiration->format('U') - time();
168 425
    }
169
170 425
    /**
171
     * Sets the cache hit for this item.
172
     *
173
     * @param  boolean $hit
174
     * @return static The invoked object.
175
     */
176 1037
    public function setHit($hit)
177
    {
178 1037
        $this->hit = (bool) $hit;
179 68
180
        return $this;
181 1037
    }
182 221
183 221
    /**
184 221
     * {@inheritdoc}
185
     */
186 1037 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...
187 1037
    {
188
        if ($expiration instanceof \DateTime) {
189 1037
            $this->expiration = $expiration;
190
191 34
        } elseif (is_int($expiration)) {
192
            $this->expiration = new \DateTime(
193 34
                'now +' . $expiration . ' seconds'
194
            );
195
196 1037
        } elseif (null === $expiration) {
197
            $this->expiration = new \DateTime(self::DEFAULT_EXPIRATION);
198
199
        } else {
200
201
            throw new InvalidArgumentException(
202 170
                'Integer or \DateTime object expected.'
203
            );
204 170
        }
205 34
206 34
        return $this;
207
    }
208 170
209 68
    /**
210
     * {@inheritdoc}
211 136
     */
212 34 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...
213
    {
214 34
        if ($time instanceof \DateInterval) {
215
            $this->expiration = new \DateTime();
216 34
            $this->expiration->add($time);
217
218 34
        } elseif (is_int($time)) {
219
            $this->expiration = new \DateTime('now +' . $time . ' seconds');
220
221 136
        } elseif (null === $time) {
222
            $this->expiration = new \DateTime(self::DEFAULT_EXPIRATION);
223
224
        } else {
225
226
            throw new InvalidArgumentException(
227
                'Integer or \DateInterval object expected.'
228
            );
229
        }
230
231
        return $this;
232
    }
233
234
    /**
235
     * Returns the item value.
236
     *
237
     * @return string
238
     */
239
    public function __toString()
240
    {
241
        return $this->value;
242
    }
243
244
}
245