Completed
Push — 2.0-dev ( 8297cc...96e0ca )
by George
9s
created

AbstractCacheItemPool::getItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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;
10
11
use Joomla\Cache\Exception\InvalidArgumentException;
12
use Joomla\Cache\Item\HasExpirationDateInterface;
13
use Psr\Cache\CacheItemInterface;
14
15
/**
16
 * Joomla! Caching Class
17
 *
18
 * @since  1.0
19
 */
20
abstract class AbstractCacheItemPool implements CacheItemPoolInterface
21
{
22
	/**
23
	 * The options for the cache object.
24
	 *
25
	 * @var    array|\ArrayAccess
26
	 * @since  1.0
27
	 */
28
	protected $options;
29
30
	/**
31
	 * The deferred items to store
32
	 *
33
	 * @var    \Joomla\Cache\Item\Item[]
34
	 * @since  1.0
35
	 */
36
	private $deferred = [];
37
38
	/**
39
	 * Constructor.
40
	 *
41
	 * @param   array|\ArrayAccess  $options  An options array, or an object that implements \ArrayAccess
42
	 *
43
	 * @since   1.0
44
	 * @throws  \RuntimeException
45
	 */
46 81
	public function __construct($options = [])
47
	{
48 81
		if (!($options instanceof \ArrayAccess || is_array($options)))
49 81
		{
50
			throw new InvalidArgumentException(sprintf('%s requires an options array or an object that implements \\ArrayAccess', __CLASS__));
51
		}
52
53 81
		$this->options = $options;
54 81
	}
55
56
	/**
57
	 * Returns a traversable set of cache items.
58
	 *
59
	 * @param   array  $keys  A list of keys that can obtained in a single operation.
60
	 *
61
	 * @return  CacheItemInterface[]  An associative array of CacheItemInterface objects keyed on the cache key.
62
	 *
63
	 * @since   1.0
64
	 */
65 5
	public function getItems(array $keys = [])
66
	{
67 5
		$result = [];
68
69 5
		foreach ($keys as $key)
70
		{
71 5
			$result[$key] = $this->getItem($key);
72 5
		}
73
74 5
		return $result;
75
	}
76
77
	/**
78
	 * Get an option from the Cache instance.
79
	 *
80
	 * @param   string  $key  The name of the option to get.
81
	 *
82
	 * @return  mixed  The option value.
83
	 *
84
	 * @since   1.0
85
	 */
86 14
	public function getOption($key)
87
	{
88 14
		return isset($this->options[$key]) ? $this->options[$key] : null;
89
	}
90
91
	/**
92
	 * Removes multiple items from the pool.
93
	 *
94
	 * @param   array  $keys  An array of keys that should be removed from the pool.
95
	 *
96
	 * @return  boolean
97
	 *
98
	 * @since   1.0
99
	 */
100 7
	public function deleteItems(array $keys)
101
	{
102 7
		$result = true;
103
104 7
		foreach ($keys as $key)
105
		{
106 7
			if (!$this->deleteItem($key))
107 7
			{
108
				$result = false;
109
			}
110 7
		}
111
112 7
		return $result;
113
	}
114
115
	/**
116
	 * Set an option for the Cache instance.
117
	 *
118
	 * @param   string  $key    The name of the option to set.
119
	 * @param   mixed   $value  The option value to set.
120
	 *
121
	 * @return  $this
122
	 *
123
	 * @since   1.0
124
	 */
125 7
	public function setOption($key, $value)
126
	{
127 7
		$this->options[$key] = $value;
128
129 7
		return $this;
130
	}
131
132
	/**
133
	 * Sets a cache item to be persisted later.
134
	 *
135
	 * @param   CacheItemInterface  $item  The cache item to save.
136
	 *
137
	 * @return  boolean  False if the item could not be queued or if a commit was attempted and failed. True otherwise.
138
	 *
139
	 * @since   __DEPLOY_VERSION__
140
	 */
141 7
	public function saveDeferred(CacheItemInterface $item)
142
	{
143 7
		$this->deferred[$item->getKey()] = $item;
144
145 7
		return true;
146
	}
147
148
	/**
149
	 * Persists any deferred cache items.
150
	 *
151
	 * @return  boolean  True if all not-yet-saved items were successfully saved or there were none. False otherwise.
152
	 *
153
	 * @since   __DEPLOY_VERSION__
154
	 */
155 7
	public function commit()
156
	{
157 7
		$result = true;
158
159 7
		foreach ($this->deferred as $key => $deferred)
160
		{
161 7
			$saveResult = $this->save($deferred);
162
163 7
			if (true === $saveResult)
164 7
			{
165 7
				unset($this->deferred[$key]);
166 7
			}
167
168 7
			$result = $result && $saveResult;
169 7
		}
170
171 7
		return $result;
172
	}
173
174
	/**
175
	 * Converts a DateTime object from the cache item to the expiry time in seconds from the present
176
	 *
177
	 * @param   HasExpirationDateInterface  $item  The cache item
178
	 *
179
	 * @return  integer  The time in seconds until expiry
180
	 *
181
	 * @since   __DEPLOY_VERSION__
182
	 */
183 3
	protected function convertItemExpiryToSeconds(HasExpirationDateInterface $item)
184
	{
185 3
		$itemExpiry   = $item->getExpiration();
186 3
		$itemTimezone = $itemExpiry->getTimezone();
187 3
		$now          = new \DateTime('now', $itemTimezone);
188 3
		$interval     = $now->diff($itemExpiry);
189
190 3
		return (int) $interval->format('%s');
191
	}
192
}
193