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

Runtime   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.1%

Importance

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