Completed
Push — 2.0-dev ( 05b98e...923a4b )
by Michael
03:35
created

Item   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 61.53%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
lcom 2
cbo 2
dl 0
loc 206
ccs 24
cts 39
cp 0.6153
rs 10
c 2
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A exists() 0 4 1
A getKey() 0 4 1
A get() 0 4 1
A set() 0 7 1
A isHit() 0 4 1
A expiresAt() 0 17 4
A expiresAfter() 0 18 3
A getExpiration() 0 4 1
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
use Joomla\Cache\Exception\InvalidArgumentException;
12
13
/**
14
 * Cache item instance for the Joomla Framework.
15
 *
16
 * @since  1.0
17
 */
18
class Item extends AbstractItem
19
{
20
	/**
21
	 * The time the object expires at
22
	 *
23
	 * @var    \DateTimeInterface
24
	 * @since  __DEPLOY_VERSION__
25
	 */
26
	private $expiration;
27
28
	/**
29
	 * The key for the cache item.
30
	 *
31
	 * @var    string
32
	 * @since  1.0
33
	 */
34
	private $key;
35
36
	/**
37
	 * The value of the cache item.
38
	 *
39
	 * @var    mixed
40
	 * @since  1.0
41
	 */
42
	private $value;
43
44
	/**
45
	 * Whether the cache item has been hit.
46
	 *
47
	 * @var    boolean
48
	 * @since  1.0
49
	 */
50
	private $hit = false;
51
52
	/**
53
	 * Class constructor.
54
	 *
55
	 * @param   string                                         $key  The key for the cache item.
56
	 * @param   \DateInterval|\DateTimeInterface|integer|null  $ttl  The expiry time for the cache item in seconds or as a datetime object
57
	 *
58
	 * @since   1.0
59
	 */
60 54
	public function __construct($key, $ttl = null)
61
	{
62 54
		$this->key = $key;
63
64 54
		if (is_int($ttl) || ($ttl instanceof \DateInterval))
65
		{
66
			$this->expiresAfter($ttl);
67
		}
68 54
		elseif ($ttl instanceof \DateTimeInterface)
69
		{
70
			$this->expiresAt($ttl);
71
		}
72
		else
73
		{
74 54
			$this->expiresAfter(900);
75
		}
76 54
	}
77
78
	/**
79
	 * Confirms if the cache item exists in the cache.
80
	 *
81
	 * Note: This method MAY avoid retrieving the cached value for performance
82
	 * reasons, which could result in a race condition between exists() and get().
83
	 * To avoid that potential race condition use isHit() instead.
84
	 *
85
	 * @return  boolean
86
	 *
87
	 * @since   __DEPLOY_VERSION__
88
	 */
89
	public function exists()
90
	{
91
		return $this->isHit();
92
	}
93
94
	/**
95
	 * Returns the key for the current cache item.
96
	 *
97
	 * @return  string  The key string for this cache item.
98
	 *
99
	 * @since   1.0
100
	 */
101 39
	public function getKey()
102
	{
103 39
		return $this->key;
104
	}
105
106
	/**
107
	 * Retrieves the value of the item from the cache associated with this object's key.
108
	 *
109
	 * @return  mixed  The value corresponding to this cache item's key, or null if not found.
110
	 *
111
	 * @since   1.0
112
	 */
113 41
	public function get()
114
	{
115 41
		return $this->value;
116
	}
117
118
	/**
119
	 * Sets the value represented by this cache item.
120
	 *
121
	 * If the value is set, we are assuming that there was a valid hit on the cache for the given key.
122
	 *
123
	 * @param   mixed  $value  The serializable value to be stored.
124
	 *
125
	 * @return  $this
126
	 *
127
	 * @since   1.0
128
	 */
129 49
	public function set($value)
130
	{
131 49
		$this->value = $value;
132 49
		$this->hit = true;
133
134 49
		return $this;
135
	}
136
137
	/**
138
	 * Confirms if the cache item lookup resulted in a cache hit.
139
	 *
140
	 * @return  boolean  True if the request resulted in a cache hit. False otherwise.
141
	 *
142
	 * @since   1.0
143
	 */
144 33
	public function isHit()
145
	{
146 33
		return $this->hit;
147
	}
148
149
	/**
150
	 * Sets the expiration time for this cache item.
151
	 *
152
	 * @param   \DateTimeInterface|null  $expiration  The point in time after which the item MUST be considered expired.
153
	 *                                                If null is passed explicitly, a default value MAY be used. If none is
154
	 *                                                set, the value should be stored permanently or for as long as the
155
	 *                                                implementation allows.
156
	 *
157
	 * @return  $this
158
	 *
159
	 * @since   __DEPLOY_VERSION__
160
	 * @throws  InvalidArgumentException
161
	 */
162
	public function expiresAt($expiration)
163
	{
164
		if ($expiration !== null && !($expiration instanceof \DateTimeInterface))
165
		{
166
			throw new InvalidArgumentException(
167
				sprintf(
168
					'Argument 1 passed to %s::expiresAt() must be an instance of DateTimeInterface; %s given',
169
					get_class($this),
170
					is_object($expiration) ? get_class($expiration) : gettype($expiration)
171
				)
172
			);
173
		}
174
175
		$this->expiration = $expiration;
176
177
		return $this;
178
	}
179
180
	/**
181
	 * Sets the expiration time for this cache item.
182
	 *
183
	 * @param   int|\DateInterval|null  $time  The period of time from the present after which the item MUST be considered
184
	 *                                         expired. An integer parameter is understood to be the time in seconds until
185
	 *                                         expiration.
186
	 *
187
	 * @return  $this
188
	 *
189
	 * @since   __DEPLOY_VERSION__
190
	 */
191 54
	public function expiresAfter($time)
192
	{
193 54
		if (is_integer($time))
194
		{
195 54
			$this->expiration = new \DateTime('now +' . $time . ' seconds');
196
		}
197 31
		elseif ($time instanceof \DateInterval)
198
		{
199
			$this->expiration = new \DateTime('now');
200
			$this->expiration->add($time);
201
		}
202
		else
203
		{
204 31
			$this->expiration = new \DateTime('now + 900 seconds');
205
		}
206
207 54
		return $this;
208
	}
209
210
	/**
211
	 * Returns the expiration time of a not-yet-expired cache item.
212
	 *
213
	 * 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.
214
	 *
215
	 * @return  \DateTimeInterface  The timestamp at which this cache item will expire.
216
	 *
217
	 * @since   __DEPLOY_VERSION__
218
	 */
219 21
	public function getExpiration(): \DateTimeInterface
220
	{
221 21
		return $this->expiration;
222
	}
223
}
224