Completed
Pull Request — 2.0-dev (#17)
by George
02:50 queued 54s
created

Runtime::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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;
10
11
use Psr\Cache\CacheItemInterface;
12
use Joomla\Cache\Item\Item;
13
14
/**
15
 * Runtime memory cache driver.
16
 *
17
 * @since  1.0
18
 */
19
class Runtime extends Cache
20
{
21
	/**
22
	 * @var    \ArrayObject  Database of cached items, we use ArrayObject so it can be easily
23
	 *                       passed by reference
24
	 *
25
	 * @since  2.0
26
	 */
27
	private $db;
28
29
	/**
30
	 * Constructor.
31
	 *
32
	 * @param   mixed  $options  An options array, or an object that implements \ArrayAccess
33
	 *
34
	 * @since   2.0
35
	 * @throws  \RuntimeException
36 1
	 */
37
	public function __construct($options = array())
38 1
	{
39
		parent::__construct($options);
40 1
		$this->db = new \ArrayObject;
41
	}
42
43
	/**
44
	 * This will wipe out the entire cache's keys
45
	 *
46
	 * @return  boolean  The result of the clear operation.
47
	 *
48
	 * @since   1.0
49
	 */
50
	public function clear()
51
	{
52 1
		// Replace the db with a new blank array
53
		$clearData = $this->db->exchangeArray(array());
54 1
		unset($clearData);
55
56 1
		return true;
57 1
	}
58 1
59 1
	/**
60
	 * Method to get a storage entry value from a key.
61 1
	 *
62
	 * @param   string  $key  The storage entry identifier.
63
	 *
64
	 * @return  CacheItemInterface
65
	 *
66
	 * @since   1.0
67
	 */
68
	public function getItem($key)
69
	{
70
		$item = new Item($key);
71
72
		if ($this->hasItem($key))
73 1
		{
74
			$item->set($this->db[$key]);
75 1
		}
76
77 1
		return $item;
78
	}
79
80
	/**
81
	 * Method to remove a storage entry for a key.
82
	 *
83
	 * @param   string  $key  The storage entry identifier.
84
	 *
85
	 * @return  boolean
86
	 *
87 1
	 * @since   1.0
88
	 */
89 1
	public function deleteItem($key)
90
	{
91 1
		$oldCache = $this->db->getArrayCopy();
92
93
		if (array_key_exists($key, $oldCache))
94
		{
95
			$keyArray = array($key => $key);
96
			$newCache = array_diff_key($oldCache, $keyArray);
97
			$this->db->exchangeArray($newCache);
98
99
			return true;
100
		}
101
102
		return true;
103 1
	}
104
105 1
	/**
106
	 * Method to set a value for a storage entry.
107
	 *
108
	 * @param   CacheItemInterface  $item  The cache item to save.
109
	 *
110
	 * @return  boolean
111
	 *
112
	 * @since   1.0
113
	 */
114
	public function save(CacheItemInterface $item)
115
	{
116
		$this->db[$item->getKey()] = $item->get();
117
118
		return true;
119
	}
120
121
	/**
122
	 * Method to determine whether a storage entry has been set for a key.
123
	 *
124
	 * @param   string  $key  The storage entry identifier.
125
	 *
126
	 * @return  boolean
127
	 *
128
	 * @since   1.0
129
	 */
130
	public function hasItem($key)
131
	{
132
		return array_key_exists($key, $this->db);
133
	}
134
135
	/**
136
	 * Test to see if the CacheItemPoolInterface is available
137
	 *
138
	 * @return  boolean  True on success, false otherwise
139
	 *
140
	 * @since   __DEPLOY_VERSION__
141
	 */
142
	public static function isSupported()
143
	{
144
		return true;
145
	}
146
}
147