Completed
Push — master ( 14c945...6811c1 )
by Henry
05:37
created

Cache::init()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 2
crap 4.0312
1
<?php
2
namespace Redaxscript\Filesystem;
3
4
use function filemtime;
5
use function filesize;
6
use function implode;
7
use function is_dir;
8
use function is_file;
9
use function mkdir;
10
use function sha1;
11
use function strlen;
12
use function time;
13
14
/**
15
 * parent class to handle cached files
16
 *
17
 * @since 3.0.0
18
 *
19
 * @category Filesystem
20
 * @package Redaxscript
21
 * @author Henry Ruhs
22
 */
23
24
class Cache
25
{
26
	/**
27
	 * directory of the cache
28
	 *
29
	 * @var string
30
	 */
31
32
	protected $_directory = 'cache';
33
34
	/**
35
	 * extension of the cached files
36
	 *
37
	 * @var string
38
	 */
39
40
	protected $_extension = 'cache';
41
42
	/**
43
	 * init the class
44
	 *
45
	 * @since 3.0.0
46
	 *
47
	 * @param string $directory name of the directory
48
	 * @param string $extension extension of the cached files
49
	 *
50
	 * @return self
51
	 */
52
53 5
	public function init(string $directory = null, string $extension = null) : self
54
	{
55 5
		if (strlen($directory))
56
		{
57 5
			$this->_directory = $directory;
58
		}
59 5
		if (strlen($extension))
60
		{
61 5
			$this->_extension = $extension;
62
		}
63 5
		if (!is_dir($this->_directory))
64
		{
65
			mkdir($this->_directory);
66
		}
67 5
		return $this;
68
	}
69
70
	/**
71
	 * get the path
72
	 *
73
	 * @since 3.0.0
74
	 *
75
	 * @param string|array $bundle key or collection of the bundle
76
	 * @param string $separator directory separator
77
	 *
78
	 * @return string
79
	 */
80
81 3
	public function getPath($bundle = null, string $separator = DIRECTORY_SEPARATOR) : string
82
	{
83 3
		return $this->_directory . $separator . $this->_getFile($bundle);
84
	}
85
86
	/**
87
	 * store to cache
88
	 *
89
	 * @since 3.0.0
90
	 *
91
	 * @param string|array $bundle key or collection of the bundle
92
	 * @param string $content content of the bundle
93
	 *
94
	 * @return self
95
	 */
96
97 4
	public function store($bundle = null, string $content = null) : self
98
	{
99 4
		if ($bundle)
100
		{
101 4
			$cacheFile = new File();
102 4
			$cacheFile->init($this->_directory);
103 4
			$cacheFile->writeFile($this->_getFile($bundle), $content);
104
		}
105 4
		return $this;
106
	}
107
108
	/**
109
	 * retrieve from cache
110
	 *
111
	 * @since 3.0.0
112
	 *
113
	 * @param string|array $bundle key or collection of the bundle
114
	 *
115
	 * @return string|null
116
	 */
117
118 2
	public function retrieve($bundle = null) : ?string
119
	{
120 2
		if ($bundle)
121
		{
122 2
			$cacheFile = new File();
123 2
			$cacheFile->init($this->_directory);
124 2
			return $cacheFile->readFile($this->_getFile($bundle));
125
		}
126
		return null;
127
	}
128
129
	/**
130
	 * validate the cache
131
	 *
132
	 * @since 3.0.0
133
	 *
134
	 * @param string|array $bundle key or collection of the bundle
135
	 * @param int $lifetime lifetime of the bundle
136
	 *
137
	 * @return bool
138
	 */
139
140 1
	public function validate($bundle = null, int $lifetime = 3600) : bool
141
	{
142 1
		if ($bundle)
143
		{
144 1
			return $this->_validateFile($this->_getFile($bundle), $lifetime);
145
		}
146
		return false;
147
	}
148
149
	/**
150
	 * clear the cache
151
	 *
152
	 * @since 3.0.0
153
	 *
154
	 * @param string|array $bundle key or collection of the bundle
155
	 *
156
	 * @return self
157
	 */
158
159 1
	public function clear($bundle = null) : self
160
	{
161 1
		$cacheFilesystem = new Directory();
162 1
		$cacheFilesystem->init($this->_directory);
163 1
		if ($bundle)
164
		{
165 1
			$cacheFilesystem->removeFile($this->_getFile($bundle));
166
		}
167
		else
168
		{
169 1
			$cacheFilesystem->clearDirectory();
170
		}
171 1
		return $this;
172
	}
173
174
	/**
175
	 * clear the invalid cache
176
	 *
177
	 * @since 3.0.0
178
	 *
179
	 * @param int $lifetime lifetime of the bundle
180
	 *
181
	 * @return self
182
	 */
183
184 1
	public function clearInvalid(int $lifetime = 3600) : self
185
	{
186 1
		$cacheFile = new File();
187 1
		$cacheFile->init($this->_directory);
188 1
		$cacheFileArray = $cacheFile->getArray();
189
190
		/* process cache */
191
192 1
		foreach ($cacheFileArray as $file)
193
		{
194 1
			if (!$this->_validateFile($file, $lifetime))
195
			{
196 1
				$cacheFile->removeFile($file);
197
			}
198
		}
199 1
		return $this;
200
	}
201
202
	/**
203
	 * get the file
204
	 *
205
	 * @since 3.0.0
206
	 *
207
	 * @param string|array $bundle key or collection of the bundle
208
	 *
209
	 * @return string
210
	 */
211
212 5
	protected function _getFile($bundle = null) : string
213
	{
214 5
		return sha1(implode('-', (array)$bundle)) . '.' . $this->_extension;
215
	}
216
217
	/**
218
	 * validate the file
219
	 *
220
	 * @since 3.2.0
221
	 *
222
	 * @param string $file name of the file
223
	 * @param int $lifetime lifetime of the file
224
	 *
225
	 * @return bool
226
	 */
227
228 2
	protected function _validateFile(string $file = null, int $lifetime = 3600) : bool
229
	{
230 2
		$path = $this->_directory . DIRECTORY_SEPARATOR . $file;
231 2
		return is_file($path) && filesize($path) && filemtime($path) > time() - $lifetime;
232
	}
233
}
234