Passed
Pull Request — master (#178)
by
unknown
03:26
created

ViewCache::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Quantum\Libraries\ResourceCache;
4
5
use Quantum\Exceptions\ConfigException;
6
use voku\helper\HtmlMin;
7
8
class ViewCache
9
{
10
	/**
11
	 * @var string
12
	 */
13
	private $cacheDir;
14
15
	/**
16
	 * @var string
17
	 */
18
	private $mimeType = '.tmp';
19
20
	/**
21
	 * @param bool $fromCommand
22
	 * @throws ConfigException
23
	 */
24
	public function __construct(bool $fromCommand = false)
25
	{
26
		if (!config()->has('view_cache') && !$fromCommand){
27
			throw ConfigException::configCollision('view_cache');
28
		}
29
30
		$configCacheDir = config()->get('view_cache.cache_dir', 'cache');
31
		$module = strtolower(current_module());
0 ignored issues
show
Bug introduced by
It seems like current_module() can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
		$module = strtolower(/** @scrutinizer ignore-type */ current_module());
Loading history...
32
33
		$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views/';
34
35
		if ($module){
36
			$this->cacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . $module . '/';
37
		}
38
		
39
		if (!is_dir($this->cacheDir) && !$fromCommand) {
40
			mkdir($this->cacheDir, 0777, true);
41
		}
42
	}
43
44
	/**
45
	 * @param string $key
46
	 * @param string $content
47
	 * @param string $sessionId
48
	 * @param int $ttl
49
	 * @return void
50
	 */
51
	public function set(string $key, string $content, string $sessionId, int $ttl = 300): void
52
	{
53
		if (config()->has('view_cache.minify')) {
54
			$content = $this->minify($content);
55
		}
56
57
		$cacheFile = $this->getCacheFile($key, $sessionId);
58
59
		$data = [
60
			'content' => $content,
61
			'expires_at' => time() + $ttl,
62
		];
63
64
		file_put_contents($cacheFile, serialize($data));
65
	}
66
67
	/**
68
	 * @param string $key
69
	 * @param string $sessionId
70
	 * @return mixed|null
71
	 */
72
	public function get(string $key, string $sessionId): ?string
73
	{
74
		$cacheFile = $this->getCacheFile($key, $sessionId);
75
		if (!file_exists($cacheFile)) {
76
			return null;
77
		}
78
79
		$data = unserialize(file_get_contents($cacheFile));
80
		if (time() > $data['expires_at']) {
81
			unlink($cacheFile);
82
			return null;
83
		}
84
85
		return $data['content'];
86
	}
87
88
	/**
89
	 * @param string $key
90
	 * @param string $sessionId
91
	 * @return void
92
	 */
93
	public function delete(string $key, string $sessionId): void
94
	{
95
		$cacheFile = $this->getCacheFile($key, $sessionId);
96
		if (file_exists($cacheFile)) {
97
			unlink($cacheFile);
98
		}
99
	}
100
101
	/**
102
	 * @param string $key
103
	 * @param string $sessionId
104
	 * @return bool
105
	 */
106
	public function exists(string $key, string $sessionId): bool
107
	{		
108
		$cacheFile = $this->getCacheFile($key, $sessionId);
109
110
		if (!file_exists($cacheFile)) {
111
			return false;
112
		}
113
114
		$data = unserialize(file_get_contents($cacheFile));
115
116
		if (time() > $data['expires_at']) {
117
			unlink($cacheFile);
118
			return false;
119
		}
120
121
		return true;
122
	}
123
124
	/**
125
	 * @param string $key
126
	 * @param string $sessionId
127
	 * @return string
128
	 */
129
	private function getCacheFile(string $key, string $sessionId): string
130
	{
131
		return $this->cacheDir . md5($key . $sessionId) . $this->mimeType;
132
	}
133
134
	/**
135
	 * @param string $content
136
	 * @return string
137
	 */
138
	private function minify(string $content): string
139
	{
140
		return (new HtmlMin())->minify($content);
141
	}
142
}