Passed
Push — master ( 2c93a3...b8d7dd )
by Jean-Christophe
03:19
created

AbstractDataCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 54.17%

Importance

Changes 0
Metric Value
wmc 10
eloc 21
dl 0
loc 103
ccs 13
cts 24
cp 0.5417
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoot() 0 2 1
A setRoot() 0 2 1
A __construct() 0 3 1
A expired() 0 9 4
A getCacheInfo() 0 2 1
A store() 0 6 2
1
<?php
2
/**
3
 * Inspired by (c) Rasmus Schultz <[email protected]>
4
 * <https://github.com/mindplay-dk/php-annotations>
5
 */
6
namespace Ubiquity\cache\system;
7
8
use Ubiquity\exceptions\CacheException;
9
10
/**
11
 * This class is responsible for storing Arrays in PHP files.
12
 */
13
abstract class AbstractDataCache {
14
	/**
15
	 *
16
	 * @var string The PHP opening tag (used when writing cache files)
17
	 */
18
	const PHP_TAG="<?php\n";
19
20
21
	protected $_root;
22
23
	protected $postfix;
24
25 12
	public function __construct($root, $postfix=""){
26 12
		$this->setRoot($root);
27 12
		$this->postfix=$postfix;
28 12
	}
29
	/**
30
	 * Check if annotation-data for the key has been stored.
31
	 * @param string $key cache key
32
	 * @return string[]|boolean true if data with the given key has been stored; otherwise false
33
	 */
34
	abstract public function exists($key);
35
36
	public function expired($key, $duration) {
37
		if ($this->exists($key)) {
38
			if (\is_int($duration) && $duration!==0) {
39
				return \time() - $this->getTimestamp($key) > $duration;
40
			} else {
41
				return false;
42
			}
43
		} else {
44
			return true;
45
		}
46
	}
47
48
	/**
49
	 * Caches the given data with the given key.
50
	 * @param string $key cache key
51
	 * @param string $code the source-code to be cached
52
	 * @param string $tag the item tag
53
	 * @param boolean $php
54
	 * @throws CacheException if file could not be written
55
	 */
56 1
	public function store($key, $code,$tag=null, $php=true) {
57 1
		$content="";
58 1
		if ($php)
59 1
			$content=self::PHP_TAG;
60 1
		$content.=$code . "\n";
61 1
		$this->storeContent($key, $content,$tag);
62 1
	}
63
64
	public function getRoot() {
65
		return $this->_root;
66
	}
67
	
68
	/**
69
	 * @param mixed $_root
70
	 */
71
	public function setRoot($_root) {
72
		$this->_root = $_root;
73
	}
74
	abstract protected function storeContent($key,$content,$tag);
75
76
	/**
77
	 * Fetches data stored for the given key.
78
	 * @param string $key cache key
79
	 * @return mixed the cached data
80
	 */
81
	abstract public function fetch($key);
82
83
	/**
84
	 * return data stored for the given key.
85
	 * @param string $key cache key
86
	 * @return mixed the cached data
87
	 */
88
	abstract public function file_get_contents($key);
89
90
	/**
91
	 * Returns the timestamp of the last cache update for the given key.
92
	 *
93
	 * @param string $key cache key
94
	 * @return boolean|int unix timestamp
95
	 */
96
	abstract public function getTimestamp($key);
97
98
	/**
99
	 * @param string $key
100
	 */
101
	abstract public function remove($key);
102
103
	/**
104
	 * Clears all cache entries
105
	 */
106
	abstract public function clear();
107
108
	abstract public function getCacheFiles($type);
109
110
	abstract public function clearCache($type);
111
112 2
	public function getCacheInfo(){
113 2
		return "Cache system is an instance of <b>".\get_class($this)."</b>.";
114
	}
115
	abstract public function getEntryKey($key);
116
}
117