Completed
Pull Request — 2.0-dev (#17)
by George
03:12 queued 01:21
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
100
		return true;
101
	}
102
103 1
	/**
104
	 * Method to set a value for a storage entry.
105 1
	 *
106
	 * @param   CacheItemInterface  $item  The cache item to save.
107
	 *
108
	 * @return  boolean
109
	 *
110
	 * @since   1.0
111
	 */
112
	public function save(CacheItemInterface $item)
113
	{
114
		$this->db[$item->getKey()] = $item->get();
115
116
		return true;
117
	}
118
119
	/**
120
	 * Method to determine whether a storage entry has been set for a key.
121
	 *
122
	 * @param   string  $key  The storage entry identifier.
123
	 *
124
	 * @return  boolean
125
	 *
126
	 * @since   1.0
127
	 */
128
	public function hasItem($key)
129
	{
130
		return array_key_exists($key, $this->db);
131
	}
132
133
	/**
134
	 * Test to see if the CacheItemPoolInterface is available
135
	 *
136
	 * @return  boolean  True on success, false otherwise
137
	 *
138
	 * @since   __DEPLOY_VERSION__
139
	 */
140
	public static function isSupported()
141
	{
142
		return true;
143
	}
144
}
145