Completed
Push — 2.0-dev ( 392f22...81b595 )
by Michael
04:39 queued 02:38
created

Item::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.4326

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 7
cts 11
cp 0.6364
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3.4326
1
<?php
2
/**
3
 * Part of the Joomla Framework Cache Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Cache\Item;
10
11
/**
12
 * Cache item instance for the Joomla Framework.
13
 *
14
 * @since  1.0
15
 */
16
class Item extends AbstractItem
17
{
18
	/**
19
	 * The time the object expires at
20
	 *
21
	 * @var    \DateTime
22
	 * @since  __DEPLOY_VERSION__
23
	 */
24
	private $expiration;
25
26
	/**
27
	 * The key for the cache item.
28
	 *
29
	 * @var    string
30
	 * @since  1.0
31
	 */
32
	private $key;
33
34
	/**
35
	 * The value of the cache item.
36
	 *
37
	 * @var    mixed
38
	 * @since  1.0
39
	 */
40
	private $value;
41
42
	/**
43
	 * Whether the cache item has been hit.
44
	 *
45
	 * @var    boolean
46
	 * @since  1.0
47
	 */
48
	private $hit = false;
49
50
	/**
51
	 * Class constructor.
52
	 *
53
	 * @param   string                  $key  The key for the cache item.
54
	 * @param   \DateTime|integer|null  $ttl  The expiry time for the cache item in seconds or as a datetime object
55
	 *
56
	 * @since   1.0
57
	 */
58 1
	public function __construct($key, $ttl = null)
59
	{
60 1
		$this->key = $key;
61
62 1
		if (is_int($ttl))
63 1
		{
64
			$this->expiresAfter($ttl);
65
		}
66 1
		elseif ($ttl instanceof \DateTime)
67
		{
68
			$this->expiresAt($ttl);
0 ignored issues
show
Documentation introduced by
$ttl is of type object<DateTime>, but the function expects a object<DateTimeInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
		}
70
		else
71
		{
72 1
			$this->expiresAfter(900);
73
		}
74 1
	}
75
76
	/**
77
	 * Confirms if the cache item exists in the cache.
78
	 *
79
	 * Note: This method MAY avoid retrieving the cached value for performance
80
	 * reasons, which could result in a race condition between exists() and get().
81
	 * To avoid that potential race condition use isHit() instead.
82
	 *
83
	 * @return  boolean
84
	 *
85
	 * @since   __DEPLOY_VERSION__
86
	 */
87
	public function exists()
88
	{
89
		return $this->isHit();
90
	}
91
92
	/**
93
	 * Returns the key for the current cache item.
94
	 *
95
	 * @return  string  The key string for this cache item.
96
	 *
97
	 * @since   1.0
98
	 */
99 1
	public function getKey()
100
	{
101 1
		return $this->key;
102
	}
103
104
	/**
105
	 * Retrieves the value of the item from the cache associated with this object's key.
106
	 *
107
	 * @return  mixed  The value corresponding to this cache item's key, or null if not found.
108
	 *
109
	 * @since   1.0
110
	 */
111 1
	public function get()
112
	{
113 1
		return $this->value;
114
	}
115
116
	/**
117
	 * Sets the value represented by this cache item.
118
	 *
119
	 * If the value is set, we are assuming that there was a valid hit on the cache for the given key.
120
	 *
121
	 * @param   mixed  $value  The serializable value to be stored.
122
	 *
123
	 * @return  $this
124
	 *
125
	 * @since   1.0
126
	 */
127 1
	public function set($value)
128
	{
129 1
		$this->value = $value;
130 1
		$this->hit = true;
131
132 1
		return $this;
133
	}
134
135
	/**
136
	 * Confirms if the cache item lookup resulted in a cache hit.
137
	 *
138
	 * @return  boolean
139
	 *
140
	 * @since   1.0
141
	 */
142 1
	public function isHit()
143
	{
144 1
		return $this->hit;
145
	}
146
147
	/**
148
	 * Sets the expiration time for this cache item.
149
	 *
150
	 * @param   \DateTimeInterface  $expiration  The point in time after which the item MUST be considered expired.
151
	 *                                           If null is passed explicitly, a default value MAY be used. If none is
152
	 *                                           set, the value should be stored permanently or for as long as the
153
	 *                                           implementation allows.
154
	 *
155
	 * @return  $this
156
	 *
157
	 * @since   __DEPLOY_VERSION__
158
	 */
159
	public function expiresAt($expiration)
160
	{
161
		$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...
162
163
		return $this;
164
	}
165
166
	/**
167
	 * Sets the expiration time for this cache item.
168
	 *
169
	 * @param   int|\DateInterval  $time  The period of time from the present after which the item MUST be considered
170
	 *                                    expired. An integer parameter is understood to be the time in seconds until
171
	 *                                    expiration.
172
	 *
173
	 * @return  $this
174
	 *
175
	 * @since   __DEPLOY_VERSION__
176
	 */
177 1
	public function expiresAfter($time)
178
	{
179 1
		if (is_integer($time))
180 1
		{
181 1
			$this->expiration = new \DateTime('now +' . $time . ' seconds');
182 1
		}
183
		elseif($time instanceof \DateInterval)
184
		{
185
			$this->expiration = new \DateTime('now');
186
			$this->expiration->add($time);
187
		}
188
		else
189
		{
190
			$this->expiration = new \DateTime('now + 900 seconds');
191
		}
192
193 1
		return $this;
194
	}
195
196
	/**
197
	 * Returns the expiration time of a not-yet-expired cache item.
198
	 *
199
	 * If this cache item is a Cache Miss, this method MAY return the time at which the item expired or the current time if that is not available.
200
	 *
201
	 * @return  \DateTime  The timestamp at which this cache item will expire.
202
	 *
203
	 * @since   __DEPLOY_VERSION__
204
	 */
205
	public function getExpiration()
206
	{
207
		return $this->expiration;
208
	}
209
}
210