Completed
Push — 2.0 ( a52f91...c9f4d8 )
by Marco
12:16
created

Item   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 2
dl 0
loc 162
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 expiresAt() 0 13 3
A expiresAfter() 0 19 4
A getRaw() 0 5 1
A getTtl() 0 13 4
A getExpiration() 0 5 1
A __toString() 0 5 1
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
 * Item class for everything a poll will return.
12
 *
13
 * @package     Comodojo Spare Parts
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @license     MIT
16
 *
17
 * LICENSE:
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28
class Item implements CacheItemInterface {
29
30
    protected $key;
31
32
    protected $data;
33
34
    protected $hit = false;
35
36
    protected $expiration = 0;
37
38
    public function __construct($key, $hit = false) {
39
40
        if ( KeyValidator::validateKey($key) === false ) {
41
            throw new InvalidCacheArgumentException('Invalid key provided');
42
        }
43
44
        $this->key = $key;
45
46
        $this->hit = $hit;
47
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getKey() {
54
55
        return $this->key;
56
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function get() {
63
64
        return $this->isHit() ? $this->data : null;
65
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function isHit() {
72
73
        // 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...
74
        return $this->hit === true;
75
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function set($value) {
82
83
        $this->data = $value;
84
85
        return $this;
86
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function expiresAt($expiration=null) {
93
94
        if (is_null($expiration)) {
95
            $this->expiration = 0;
96
        }
97
98
        if ( $expiration instanceof DateTimeInterface ) {
99
            $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...
100
        }
101
102
        return $this;
103
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function expiresAfter($time) {
110
111
        if ( is_null($time) ) {
112
            $this->expiration = 0;
113
        }
114
115
        if ( is_numeric($time) ) {
116
            $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...
117
        }
118
119
        if ( $time instanceof DateInterval ) {
120
            $expiration = new DateTime('now');
121
            $expiration->add($time);
122
            $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...
123
        }
124
125
        return $this;
126
127
    }
128
129
    /**
130
     * Returns the raw value, regardless of hit status.
131
     *
132
     * Although not part of the CacheItemInterface, this method is used by
133
     * the pool for extracting information for saving.
134
     *
135
     * @return mixed
136
     *
137
     * @internal
138
     */
139
    public function getRaw() {
140
141
        return $this->data;
142
143
    }
144
145
    /**
146
     * Get currently (calculated) ttl of cache item
147
     *
148
     * This method is not part of the CacheItemInterface.
149
     *
150
     * @return int
151
     *
152
     * @internal
153
     */
154
    public function getTtl() {
155
156
        if (is_null($this->expiration)) return null;
157
158
        if ($this->expiration === 0) return 0;
159
160
        $now = new DateTime("now");
161
162
        if ( $now > $this->expiration ) return -1;
163
164
        return (int) $now->diff($this->expiration)->format("%r%s");
165
166
    }
167
168
    /**
169
     * Get expiration time (absolute)
170
     *
171
     * This method is not part of the CacheItemInterface.
172
     *
173
     * @return int
174
     *
175
     * @internal
176
     */
177
    public function getExpiration() {
178
179
        return $this->expiration;
180
181
    }
182
183
    public function __toString() {
184
185
        return serialize($this->get());
186
187
    }
188
189
}
190