Passed
Push — master ( 64da0c...c6d718 )
by Jean-Christophe
11:31
created

ArrayCache::storeContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 9.9666
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4.1755
1
<?php
2
3
namespace Ubiquity\cache\system;
4
5
use Ubiquity\utils\base\UFileSystem;
6
use Ubiquity\exceptions\CacheException;
7
use Ubiquity\cache\CacheFile;
8
9
/**
10
 * This class is responsible for storing Arrays in PHP files.
11
 * Ubiquity\cache\system$ArrayCache
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.2
16
 *
17
 */
18
class ArrayCache extends AbstractDataCache {
19
	/**
20
	 *
21
	 * @var int The file mode used when creating new cache files
22
	 */
23
	private $_fileMode;
24
25
	/**
26
	 * Initializes the file cache-provider
27
	 *
28
	 * @param string $root absolute path to the root-folder where cache-files will be stored
29
	 * @param string $postfix Termination of file names
30
	 * @param array $cacheParams defaults to ["fileMode"=>"0755"]
31
	 */
32 100
	public function __construct($root, $postfix = "", $cacheParams = []) {
33 100
		parent::__construct ( $root, $postfix );
34 100
		$this->_fileMode = (isset ( $cacheParams ["fileMode"] )) ? $cacheParams ["fileMode"] : 0755;
35 100
		if (! is_dir ( $root ))
36 2
			\mkdir ( $root, $this->_fileMode, true );
37 100
	}
38
39
	/**
40
	 * Check if annotation-data for the key has been stored.
41
	 *
42
	 * @param string $key cache key
43
	 * @return boolean true if data with the given key has been stored; otherwise false
44
	 */
45 79
	public function exists($key) {
46 79
		return file_exists ( $this->_getPath ( $key ) );
47
	}
48
49
	/**
50
	 * Caches the given data with the given key.
51
	 *
52
	 * @param string $key cache key
53
	 * @param string $content the source-code to be cached
54
	 * @param string $tag
55
	 * @throws CacheException if file could not be written
56
	 */
57 32
	protected function storeContent($key, $content, $tag) {
58 32
		$path = $this->_getPath ( $key );
59 32
		$dir = pathinfo ( $path, PATHINFO_DIRNAME );
60 32
		if (UFileSystem::safeMkdir ( $dir )) {
61 32
			if (@\file_put_contents ( $path, $content, LOCK_EX ) === false) {
62
				throw new CacheException ( "Unable to write cache file: {$path}" );
63
			}
64 32
			if (@\chmod ( $path, $this->_fileMode ) === false) {
65 22
				throw new CacheException ( "Unable to set permissions of cache file: {$path}" );
66
			}
67
		} else {
68
			throw new CacheException ( "Unable to create folder : {$dir}" );
69
		}
70 32
	}
71
72
	/**
73
	 * Fetches data stored for the given key.
74
	 *
75
	 * @param string $key cache key
76
	 * @return mixed the cached data
77
	 */
78 79
	public function fetch($key) {
79 79
		return include ($this->_getPath ( $key ));
80
	}
81
82
	/**
83
	 * return data stored for the given key.
84
	 *
85
	 * @param string $key cache key
86
	 * @return mixed the cached data
87
	 */
88
	public function file_get_contents($key) {
89
		return \file_get_contents ( $this->_getPath ( $key ) );
90
	}
91
92
	/**
93
	 * Returns the timestamp of the last cache update for the given key.
94
	 *
95
	 * @param string $key cache key
96
	 * @return int unix timestamp
97
	 */
98
	public function getTimestamp($key) {
99
		return \filemtime ( $this->_getPath ( $key ) );
100
	}
101
102
	/**
103
	 * Maps a cache-key to the absolute path of a PHP file
104
	 *
105
	 * @param string $key cache key
106
	 * @return string absolute path of the PHP file
107
	 */
108 86
	private function _getPath($key) {
109 86
		return $this->_root . $key . $this->postfix . '.php';
110
	}
111
112
	/**
113
	 *
114
	 * {@inheritdoc}
115
	 * @see \Ubiquity\cache\system\AbstractDataCache::remove()
116
	 */
117 8
	public function remove($key) {
118 8
		$file = $this->_getPath ( $key );
119 8
		if (\file_exists ( $file ))
120 7
			return \unlink ( $file );
121 4
		return false;
122
	}
123
124
	/**
125
	 *
126
	 * {@inheritdoc}
127
	 * @see \Ubiquity\cache\system\AbstractDataCache::clear()
128
	 */
129
	public function clear($matches = "") {
130
		$files = glob ( $this->_root . $matches . '*' );
131
		foreach ( $files as $file ) {
132
			if (\is_file ( $file ))
133
				\unlink ( $file );
134
		}
135
	}
136
137
	/**
138
	 *
139
	 * {@inheritdoc}
140
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheFiles()
141
	 */
142 1
	public function getCacheFiles($type) {
143
		return CacheFile::initFromFiles ( $this->_root . $type, \ucfirst ( $type ), function ($file) use ($type) {
0 ignored issues
show
Unused Code introduced by
The import $type is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
144 1
			$path = UFileSystem::relativePath ( dirname ( $file ), $this->_root );
145 1
			$file = \basename ( $file );
146 1
			return $path . \DS . substr ( $file, 0, strpos ( $file, $this->postfix . '.php' ) );
147 1
		} );
148
	}
149
150
	/**
151
	 *
152
	 * {@inheritdoc}
153
	 * @see \Ubiquity\cache\system\AbstractDataCache::clearCache()
154
	 */
155
	public function clearCache($type) {
156
		CacheFile::delete ( $this->_root . \strtolower ( $type ) );
157
	}
158
159
	/**
160
	 *
161
	 * {@inheritdoc}
162
	 * @see \Ubiquity\cache\system\AbstractDataCache::getCacheInfo()
163
	 */
164 3
	public function getCacheInfo() {
165 3
		$result = parent::getCacheInfo ();
166 3
		$result .= "\nRoot cache directory is <b>" . UFileSystem::cleanPathname ( $this->_root ) . "</b>.";
167 3
		return $result;
168
	}
169
170
	/**
171
	 *
172
	 * {@inheritdoc}
173
	 * @see \Ubiquity\cache\system\AbstractDataCache::getEntryKey()
174
	 */
175 3
	public function getEntryKey($key) {
176 3
		return UFileSystem::cleanFilePathname ( $this->_getPath ( $key ) );
177
	}
178
179
	/**
180
	 *
181
	 * {@inheritdoc}
182
	 * @see \Ubiquity\cache\system\AbstractDataCache::setRoot()
183
	 */
184 100
	public function setRoot($root) {
185 100
		$this->_root = rtrim ( $root, \DS ) . \DS;
186 100
	}
187
}
188