Completed
Push — master ( a59dca...9322be )
by Nazar
05:41
created

Cache   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 164
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 4

11 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 7 3
A disable() 0 3 1
B get() 0 14 6
A set() 0 10 3
A del() 0 17 4
A cache_state() 0 3 1
A __get() 0 3 1
A clean() 0 7 2
A prefix() 0 3 1
A __set() 0 3 1
A __unset() 0 3 1
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 42
	protected function construct () {
43 42
		if (!$this->init && $this->state) {
44 42
			$this->engine          = Core::instance()->cache_engine;
45 42
			$engine_class          = "cs\\Cache\\$this->engine";
46 42
			$this->engine_instance = new $engine_class();
47
		}
48 42
	}
49
	/**
50
	 * Returns instance for simplified work with cache, when using common prefix
51
	 *
52
	 * @param string $prefix
53
	 *
54
	 * @return Prefix
55
	 */
56 26
	static function prefix ($prefix) {
57 26
		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 42
	function get ($item, $callback = null) {
70 42
		if (!$this->state) {
71 10
			return is_callable($callback) ? $callback() : false;
72
		}
73 42
		$item = trim($item, '/');
74 42
		$data = $this->engine_instance->get($item);
75 42
		if ($data === false && is_callable($callback)) {
76 24
			$data = $callback();
77 24
			if ($data !== false) {
78 24
				$this->set($item, $data);
79
			}
80
		}
81 42
		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 26
	function set ($item, $data) {
92 26
		if (is_object($this->engine_instance)) {
93 26
			$this->engine_instance->del($item);
94
		}
95 26
		if (!$this->state) {
96
			return true;
97
		}
98 26
		$item = trim($item, '/');
99 26
		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 22
	function del ($item) {
109 22
		if (empty($item)) {
110
			return false;
111
		}
112
		/**
113
		 * Cache cleaning instead of removing when root specified
114
		 */
115 22
		if ($item == '/') {
116
			return $this->clean();
117
		}
118 22
		if (is_object($this->engine_instance)) {
119 22
			$item = trim($item, '/');
120 22
			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 10
	function clean () {
131 10
		if (is_object($this->engine_instance)) {
132 10
			return $this->engine_instance->clean();
133
		} else {
134
			return false;
135
		}
136
	}
137
	/**
138
	 * Cache state enabled/disabled
139
	 *
140
	 * @return bool
141
	 */
142 10
	function cache_state () {
143 10
		return $this->state;
144
	}
145
	/**
146
	 * Disable cache
147
	 */
148 10
	function disable () {
149 10
		$this->state = false;
150 10
	}
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 40
	function __get ($item) {
159 40
		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 14
	function __set ($item, $data) {
168 14
		$this->set($item, $data);
169 14
	}
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 14
	function __unset ($item) {
176 14
		$this->del($item);
177 14
	}
178
}
179