StaticStorage   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 0
dl 0
loc 203
ccs 84
cts 84
cp 1
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 6
A getAll() 0 4 1
A setAll() 0 4 1
A removeAll() 0 4 1
A destroy() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
A __unset() 0 4 1
A __isset() 0 4 1
A count() 0 4 1
A current() 0 4 1
A key() 0 4 1
A next() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A rewind() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A valid() 0 4 1
A _propertize() 0 16 4
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/embedi
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\EmbeDi;
14
15
use ArrayAccess;
16
use Countable;
17
use Iterator;
18
use Maslosoft\EmbeDi\Interfaces\MassAssignedInterface;
19
use ReflectionObject;
20
use ReflectionProperty;
21
use Serializable;
22
23
/**
24
 * Static Storage class
25
 * This stores variables in static var, depending on owner and instance id
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class StaticStorage implements Countable, Iterator, Serializable, ArrayAccess, MassAssignedInterface
30
{
31
32
	/**
33
	 * Namespace, current container class name
34
	 * @var string
35
	 */
36
	private $ns = '';
37
38
	/**
39
	 * Owner Id
40
	 * @var string
41
	 */
42
	private $ownerId = '';
43
44
	/**
45
	 * Instance id
46
	 * @var string
47
	 */
48
	private $instanceId = '';
49
50
	/**
51
	 * Preset ID
52
	 * @var string
53
	 */
54
	private $presetId = '';
55
56
	/**
57
	 * Key for storage
58
	 * @var string
59
	 */
60
	private $key = '';
61
62
	/**
63
	 * Stored values
64
	 * @var mixed[][]
65
	 */
66
	public static $values = [];
67
68
	/**
69
	 *
70
	 * @param object|string $owner
71
	 * @param string $instanceId
72
	 */
73 29
	public function __construct($owner, $instanceId, $presetId = null)
74
	{
75 29
		$this->ns = get_class($this);
76 29
		$this->ownerId = is_object($owner) ? get_class($owner) : $owner;
77 29
		$this->instanceId = $instanceId;
78 29
		$this->presetId = $presetId;
79 29
		if (!empty($presetId))
80 29
		{
81 2
			$this->key = $this->instanceId . '.' . $this->presetId;
82 2
		}
83
		else
84
		{
85 29
			$this->key = $this->instanceId;
86
		}
87
		// Gracefully init - this is required for subsequent constructor calls
88 29
		if (!array_key_exists($this->ns, self::$values))
89 29
		{
90 29
			self::$values[$this->ns] = [];
91 29
		}
92 29
		if (!array_key_exists($this->ownerId, self::$values[$this->ns]))
93 29
		{
94 29
			self::$values[$this->ns][$this->ownerId] = [];
95 29
		}
96 29
		if (!array_key_exists($this->key, self::$values[$this->ns][$this->ownerId]))
97 29
		{
98 29
			self::$values[$this->ns][$this->ownerId][$this->key] = [];
99 29
		}
100 29
		$this->_propertize();
101 29
	}
102
103 1
	public function getAll()
104
	{
105 1
		return self::$values[$this->ns][$this->ownerId][$this->key];
106
	}
107
108 1
	public function setAll($values)
109
	{
110 1
		return self::$values[$this->ns][$this->ownerId][$this->key] = $values;
111
	}
112
113 2
	public function removeAll()
114
	{
115 2
		self::$values[$this->ns][$this->ownerId][$this->key] = [];
116 2
	}
117
118
	/**
119
	 * Destroy all data in all containers
120
	 */
121 27
	public function destroy()
122
	{
123 27
		self::$values = [];
124 27
	}
125
126 21
	public function &__get($name)
127
	{
128 21
		return self::$values[$this->ns][$this->ownerId][$this->key][$name];
129
	}
130
131 28
	public function __set($name, $value)
132
	{
133 28
		self::$values[$this->ns][$this->ownerId][$this->key][$name] = $value;
134 28
	}
135
136 1
	public function __unset($name)
137
	{
138 1
		unset(self::$values[$this->ns][$this->ownerId][$this->key][$name]);
139 1
	}
140
141 3
	public function __isset($name)
142
	{
143 3
		return isset(self::$values[$this->ns][$this->ownerId][$this->key][$name]);
144
	}
145
146
// <editor-fold defaultstate="collapsed" desc="Interfaces implementation">
147
148 1
	public function count($mode = 'COUNT_NORMAL')
149
	{
150 1
		return count(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
151
	}
152
153 1
	public function current()
154
	{
155 1
		return current(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
156
	}
157
158 1
	public function key()
159
	{
160 1
		return key(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
161
	}
162
163 1
	public function next()
164
	{
165 1
		return next(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
166
	}
167
168 4
	public function offsetExists($offset)
169
	{
170 4
		return array_key_exists($offset, self::$values[$this->ns][$this->ownerId][$this->instanceId]);
171
	}
172
173 3
	public function offsetGet($offset)
174
	{
175 3
		return self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset];
176
	}
177
178 2
	public function offsetSet($offset, $value)
179
	{
180 2
		self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset] = $value;
181 2
	}
182
183 2
	public function offsetUnset($offset)
184
	{
185 2
		unset(self::$values[$this->ns][$this->ownerId][$this->instanceId][$offset]);
186 2
	}
187
188 1
	public function rewind()
189
	{
190 1
		reset(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
191 1
	}
192
193 1
	public function serialize()
194
	{
195 1
		return serialize(self::$values[$this->ns][$this->ownerId][$this->instanceId]);
196
	}
197
198 1
	public function unserialize($serialized)
199
	{
200 1
		return self::$values[$this->ns][$this->ownerId][$this->instanceId] = unserialize($serialized);
201
	}
202
203 1
	public function valid()
204
	{
205 1
		return $this->offsetExists($this->key());
206
	}
207
208
// </editor-fold>
209
210
	/**
211
	 * This unsets class fields and turns them into storage-aware properties
212
	 * @return void
213
	 */
214 29
	private function _propertize()
215
	{
216 29
		foreach ((new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property)
217
		{
218
			// http://stackoverflow.com/a/15784768/133408
219 29
			if (!$property->isStatic())
220 29
			{
221 20
				$name = $property->name;
222 20
				if (!array_key_exists($name, self::$values[$this->ns][$this->ownerId][$this->key]))
223 20
				{
224 20
					$this->__set($name, $this->$name);
225 20
				}
226 20
				unset($this->$name);
227 20
			}
228 29
		}
229 29
	}
230
231
}
232