Completed
Push — master ( e934ec...298fb7 )
by Nazar
04:47
created

Cache::get()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 2
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	cs\Cache\Prefix;
11
12
/**
13
 * @method static $this instance($check = false)
14
 */
15
class Cache {
16
	use Singleton;
17
	/**
18
	 * Cache state
19
	 *
20
	 * @var bool
21
	 */
22
	protected $state = true;
23
	/**
24
	 * Initialization state
25
	 * @var bool
26
	 */
27
	protected $init = false;
28
	/**
29
	 * Name of cache engine
30
	 * @var string
31
	 */
32
	protected $engine;
33
	/**
34
	 * Instance of cache engine object
35
	 *
36
	 * @var Cache\_Abstract
37
	 */
38
	protected $engine_instance;
39
	/**
40
	 * Initialization, creating cache engine instance
41
	 */
42 34
	protected function construct () {
43 34
		if (!$this->init && $this->state) {
44 34
			$this->engine          = Core::instance()->cache_engine;
45 34
			$engine_class          = "cs\\Cache\\$this->engine";
46 34
			$this->engine_instance = new $engine_class();
47
		}
48 34
	}
49
	/**
50
	 * Returns instance for simplified work with cache, when using common prefix
51
	 *
52
	 * @param string $prefix
53
	 *
54
	 * @return Prefix
55
	 */
56
	static function prefix ($prefix) {
57
		return new Prefix($prefix);
58
	}
59
	/**
60
	 * Get item from cache
61
	 *
62
	 * If item not found and $callback parameter specified - closure must return value for item. This value will be set for current item, and returned.
63
	 *
64
	 * @param string        $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
65
	 * @param callable|null $callback
66
	 *
67
	 * @return false|mixed Returns item on success of <b>false</b> on failure
68
	 */
69 34
	function get ($item, $callback = null) {
70 34
		if (!$this->state) {
71 8
			return is_callable($callback) ? $callback() : false;
72
		}
73 34
		$item = trim($item, '/');
74 34
		$data = $this->engine_instance->get($item);
75 34
		if ($data === false && is_callable($callback)) {
76 16
			$data = $callback();
77 16
			if ($data !== false) {
78 16
				$this->set($item, $data);
79
			}
80
		}
81 34
		return $data;
82
	}
83
	/**
84
	 * Put or change data of cache item
85
	 *
86
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
87
	 * @param mixed  $data
88
	 *
89
	 * @return bool
90
	 */
91 18
	function set ($item, $data) {
92 18
		if (is_object($this->engine_instance)) {
93 18
			$this->engine_instance->del($item);
94
		}
95 18
		if (!$this->state) {
96
			return true;
97
		}
98 18
		$item = trim($item, '/');
99 18
		return $this->engine_instance->set($item, $data);
100
	}
101
	/**
102
	 * Delete item from cache
103
	 *
104
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
105
	 *
106
	 * @return bool
107
	 */
108 14
	function del ($item) {
109 14
		if (empty($item)) {
110
			return false;
111
		}
112
		/**
113
		 * Cache cleaning instead of removing when root specified
114
		 */
115 14
		if ($item == '/') {
116
			return $this->clean();
117
		}
118 14
		if (is_object($this->engine_instance)) {
119 14
			$item = trim($item, '/');
120 14
			return $this->engine_instance->del($item);
121
		} else {
122
			return false;
123
		}
124
	}
125
	/**
126
	 * Clean cache by deleting all items
127
	 *
128
	 * @return bool
129
	 */
130
	function clean () {
131
		if (is_object($this->engine_instance)) {
132
			return $this->engine_instance->clean();
133
		} else {
134
			return false;
135
		}
136
	}
137
	/**
138
	 * Cache state enabled/disabled
139
	 *
140
	 * @return bool
141
	 */
142 8
	function cache_state () {
143 8
		return $this->state;
144
	}
145
	/**
146
	 * Disable cache
147
	 */
148 8
	function disable () {
149 8
		$this->state = false;
150 8
	}
151
	/**
152
	 * Get item from cache
153
	 *
154
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
155
	 *
156
	 * @return false|mixed            Returns item on success of <b>false</b> on failure
157
	 */
158 32
	function __get ($item) {
159 32
		return $this->get($item);
160
	}
161
	/**
162
	 * Put or change data of cache item
163
	 *
164
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
165
	 * @param mixed  $data
166
	 */
167
	function __set ($item, $data) {
168
		$this->set($item, $data);
169
	}
170
	/**
171
	 * Delete item from cache
172
	 *
173
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
174
	 */
175
	function __unset ($item) {
176
		$this->del($item);
177
	}
178
}
179