Completed
Push — master ( fdfecd...3472af )
by Nazar
04:06
created

Cache::del()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
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 60
	protected function construct () {
43 60
		if (!$this->init && $this->state) {
44 60
			$this->engine          = Core::instance()->cache_engine;
45 60
			$engine_class          = "cs\\Cache\\$this->engine";
46 60
			$this->engine_instance = new $engine_class();
47
		}
48 60
	}
49
	/**
50
	 * Returns instance for simplified work with cache, when using common prefix
51
	 *
52
	 * @param string $prefix
53
	 *
54
	 * @return Prefix
55
	 */
56 40
	static function prefix ($prefix) {
57 40
		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 60
	function get ($item, $callback = null) {
70 60
		if (!$this->state) {
71 10
			return is_callable($callback) ? $callback() : false;
72
		}
73 60
		$item = trim($item, '/');
74 60
		$data = $this->engine_instance->get($item);
75 60
		if ($data === false && is_callable($callback)) {
76 36
			$data = $callback();
77 36
			if ($data !== false) {
78 36
				$this->set($item, $data);
79
			}
80
		}
81 60
		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 40
	function set ($item, $data) {
92 40
		$this->engine_instance->del($item);
93 40
		if (!$this->state) {
94 10
			return true;
95
		}
96 40
		$item = trim($item, '/');
97 40
		return $this->engine_instance->set($item, $data);
98
	}
99
	/**
100
	 * Delete item from cache
101
	 *
102
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
103
	 *
104
	 * @return bool
105
	 */
106 34
	function del ($item) {
107 34
		if (empty($item)) {
108 10
			return false;
109
		}
110
		/**
111
		 * Cache cleaning instead of removing when root specified
112
		 */
113 34
		if ($item == '/') {
114 10
			return $this->clean();
115
		}
116 34
		$item = trim($item, '/');
117 34
		return $this->engine_instance->del($item);
118
	}
119
	/**
120
	 * Clean cache by deleting all items
121
	 *
122
	 * @return bool
123
	 */
124 10
	function clean () {
125 10
		return $this->engine_instance->clean();
126
	}
127
	/**
128
	 * Cache state enabled/disabled
129
	 *
130
	 * @return bool
131
	 */
132 10
	function cache_state () {
133 10
		return $this->state;
134
	}
135
	/**
136
	 * Disable cache
137
	 */
138 10
	function disable () {
139 10
		$this->state = false;
140 10
	}
141
	/**
142
	 * Get item from cache
143
	 *
144
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
145
	 *
146
	 * @return false|mixed            Returns item on success of <b>false</b> on failure
147
	 */
148 56
	function __get ($item) {
149 56
		return $this->get($item);
150
	}
151
	/**
152
	 * Put or change data of cache item
153
	 *
154
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
155
	 * @param mixed  $data
156
	 */
157 18
	function __set ($item, $data) {
158 18
		$this->set($item, $data);
159 18
	}
160
	/**
161
	 * Delete item from cache
162
	 *
163
	 * @param string $item May contain "/" symbols for cache structure, for example users/<i>user_id</i>
164
	 */
165 14
	function __unset ($item) {
166 14
		$this->del($item);
167 14
	}
168
}
169