Completed
Push — master ( 384bb0...f996ab )
by Nicholas
04:03
created

cache::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * Very simple in-memory caching
4
 */
5
6
namespace projectcleverweb\color;
7
8
class cache {
9
	
10
	/**
11
	 * Whether or not caching is active
12
	 * @var bool
13
	 */
14
	public $active;
15
	
16
	/**
17
	 * The cache data
18
	 * @var array
19
	 */
20
	protected $data;
21
	
22
	/**
23
	 * Initialize this class
24
	 */
25
	public function __construct() {
26
		$this->reset();
27
		$this->active = TRUE;
28
	}
29
	
30
	/**
31
	 * Reset the entire cache
32
	 * 
33
	 * @return void
34
	 */
35
	public function reset() {
36
		$this->data = [];
37
	}
38
	
39
	/**
40
	 * Store a value in the cache
41
	 * 
42
	 * @param string $func  The function trying to store data (should be __FUNCTION__)
43
	 * @param string $id    The storage ID
44
	 * @param mixed  $value The value to store
45
	 */
46
	public function set(string $func, string $id, $value) {
47
		if ($this->active) {
48
			$this->data[$func] = [$id => $value];
49
		}
50
	}
51
	
52
	/**
53
	 * Get a value from the cache
54
	 * 
55
	 * @param  string $func The function trying to get data (should be __FUNCTION__)
56
	 * @param  string $id   The storage ID
57
	 * @return mixed        The value stored or NULL
58
	 */
59
	public function get(string $func, string $id) {
60
		if ($this->active && isset($this->data[$func][$id])) {
61
			return $this->data[$func][$id];
62
		}
63
	}
64
}
65
66