Completed
Pull Request — 2.0-dev (#17)
by George
01:59
created

Runtime   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 9
c 6
b 1
f 1
lcom 1
cbo 3
dl 0
loc 123
ccs 0
cts 29
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A clear() 0 8 1
A getItem() 0 11 2
A deleteItem() 0 10 2
A save() 0 6 1
A hasItem() 0 4 1
A isSupported() 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;
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
	 */
37
	public function __construct($options = array())
38
	{
39
		parent::__construct($options);
40
		$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
		// Replace the db with a new blank array
53
		$clearData = $this->db->exchangeArray(array());
54
		unset($clearData);
55
56
		return true;
57
	}
58
59
	/**
60
	 * Method to get a storage entry value from a key.
61
	 *
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
		{
74
			$item->set($this->db[$key]);
75
		}
76
77
		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
	 * @since   1.0
88
	 */
89
	public function deleteItem($key)
90
	{
91
		if ($this->hasItem($key))
92
		{
93
			$newCache = array_diff_key($this->db->getArrayCopy(), array($key => $key));
94
			$this->db->exchangeArray($newCache);
95
		}
96
97
		return true;
98
	}
99
100
	/**
101
	 * Method to set a value for a storage entry.
102
	 *
103
	 * @param   CacheItemInterface  $item  The cache item to save.
104
	 *
105
	 * @return  boolean
106
	 *
107
	 * @since   1.0
108
	 */
109
	public function save(CacheItemInterface $item)
110
	{
111
		$this->db[$item->getKey()] = $item->get();
112
113
		return true;
114
	}
115
116
	/**
117
	 * Method to determine whether a storage entry has been set for a key.
118
	 *
119
	 * @param   string  $key  The storage entry identifier.
120
	 *
121
	 * @return  boolean
122
	 *
123
	 * @since   1.0
124
	 */
125
	public function hasItem($key)
126
	{
127
		return array_key_exists($key, $this->db);
128
	}
129
130
	/**
131
	 * Test to see if the CacheItemPoolInterface is available
132
	 *
133
	 * @return  boolean  True on success, false otherwise
134
	 *
135
	 * @since   __DEPLOY_VERSION__
136
	 */
137
	public static function isSupported()
138
	{
139
		return true;
140
	}
141
}
142