Completed
Push — 2.0 ( ace65e...4344f7 )
by Marco
09:00 queued 06:46
created

Item   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 2
dl 0
loc 162
ccs 40
cts 48
cp 0.8333
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getKey() 0 5 1
A get() 0 5 2
A isHit() 0 6 1
A set() 0 7 1
A getRaw() 0 5 1
A getTtl() 0 13 4
A getExpiration() 0 5 1
A __toString() 0 5 1
A expiresAt() 0 13 3
A expiresAfter() 0 19 4
1
<?php namespace Comodojo\Cache;
2
3
use \Psr\Cache\CacheItemInterface;
4
use \Comodojo\Cache\Components\KeyValidator;
5
use \DateTimeInterface;
6
use \DateTime;
7
use \DateInterval;
8
use \Comodojo\Exception\InvalidCacheArgumentException;
9
10
/**
11
 * @package     Comodojo Cache
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class Item implements CacheItemInterface {
27
28
    protected $key;
29
30
    protected $data;
31
32
    protected $hit = false;
33
34
    protected $expiration = 0;
35
36 196
    public function __construct($key, $hit = false) {
37
38 196
        if ( KeyValidator::validateKey($key) === false ) {
39 11
            throw new InvalidCacheArgumentException('Invalid key provided');
40
        }
41
42 185
        $this->key = $key;
43
44 185
        $this->hit = $hit;
45
46 185
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 129
    public function getKey() {
52
53 129
        return $this->key;
54
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 154
    public function get() {
61
62 154
        return $this->isHit() ? $this->data : null;
63
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 164
    public function isHit() {
70
71
        // return !is_null($this->data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72 164
        return $this->hit === true;
73
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 172
    public function set($value) {
80
81 172
        $this->data = $value;
82
83 172
        return $this;
84
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 57
    public function expiresAt($expiration = null) {
91
92 57
        if ( is_null($expiration) ) {
93
            $this->expiration = 0;
94
        }
95
96 57
        if ( $expiration instanceof DateTimeInterface ) {
97 57
            $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 integer 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...
98 57
        }
99
100 57
        return $this;
101
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 25
    public function expiresAfter($time) {
108
109 25
        if ( is_null($time) ) {
110
            $this->expiration = 0;
111
        }
112
113 25
        if ( is_numeric($time) ) {
114 25
            $this->expiration = new DateTime('now +'.$time.' seconds');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime('now +' . $time . ' seconds') of type object<DateTime> is incompatible with the declared type integer 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...
115 25
        }
116
117 25
        if ( $time instanceof DateInterval ) {
118
            $expiration = new DateTime('now');
119
            $expiration->add($time);
120
            $this->expiration = $expiration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $expiration of type object<DateTime> is incompatible with the declared type integer 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...
121
        }
122
123 25
        return $this;
124
125
    }
126
127
    /**
128
     * Returns the raw value, regardless of hit status.
129
     *
130
     * Although not part of the CacheItemInterface, this method is used by
131
     * the pool for extracting information for saving.
132
     *
133
     * @return mixed
134
     *
135
     * @internal
136
     */
137 122
    public function getRaw() {
138
139 122
        return $this->data;
140
141
    }
142
143
    /**
144
     * Get currently (calculated) ttl of cache item
145
     *
146
     * This method is not part of the CacheItemInterface.
147
     *
148
     * @return int
149
     *
150
     * @internal
151
     */
152 170
    public function getTtl() {
153
154 170
        if ( is_null($this->expiration) ) return null;
155
156 170
        if ( $this->expiration === 0 ) return 0;
157
158 96
        $now = new DateTime("now");
159
160 96
        if ( $now > $this->expiration ) return -1;
161
162 47
        return (int) $now->diff($this->expiration)->format("%r%s");
163
164
    }
165
166
    /**
167
     * Get expiration time (absolute)
168
     *
169
     * This method is not part of the CacheItemInterface.
170
     *
171
     * @return int
172
     *
173
     * @internal
174
     */
175 1
    public function getExpiration() {
176
177 1
        return $this->expiration;
178
179
    }
180
181 1
    public function __toString() {
182
183 1
        return serialize($this->get());
184
185
    }
186
187
}
188