Passed
Push — release-2.1 ( f28109...493a4c )
by Mathias
06:40
created

CacheApi::getCompatibleVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines https://www.simplemachines.org
8
 * @copyright 2020 Simple Machines and individual contributors
9
 * @license https://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 RC3
12
 */
13
14
namespace SMF\Cache;
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
abstract class CacheApi
20
{
21
	const APIS_FOLDER = 'APIs';
22
	const APIS_NAMESPACE = 'SMF\Cache\APIs\\';
23
	const APIS_DEFAULT = 'FileBased';
24
	const APIS_BASENAME = '%s.php';
25
26
	/**
27
	 * @var string The maximum SMF version that this will work with.
28
	 */
29
	protected $version_compatible = '2.1.999';
30
31
	/**
32
	 * @var string The minimum SMF version that this will work with.
33
	 */
34
	protected $min_smf_version = '2.1 RC1';
35
36
	/**
37
	 * @var string The prefix for all keys.
38
	 */
39
	protected $prefix = '';
40
41
	/**
42
	 * @var int The default TTL.
43
	 */
44
	protected $ttl = 120;
45
46
	/**
47
	 * Does basic setup of a cache method when we create the object but before we call connect.
48
	 *
49
	 * @access public
50
	 */
51
	public function __construct()
52
	{
53
		$this->setPrefix();
54
	}
55
56
	/**
57
	 * Checks whether we can use the cache method performed by this API.
58
	 *
59
	 * @access public
60
	 * @param bool $test Test if this is supported or enabled.
61
	 * @return bool Whether or not the cache is supported
62
	 */
63
	public function isSupported($test = false)
64
	{
65
		global $cache_enable;
66
67
		if ($test)
68
			return true;
69
70
		return !empty($cache_enable);
71
	}
72
73
	/**
74
	 * Overrides the default prefix. If left alone, this will use the default key defined in the class.
75
	 *
76
	 * @access public
77
	 * @param string $key The key to use
78
	 * @return bool If this was successful or not.
79
	 */
80
	public function setPrefix($key = '')
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

80
	public function setPrefix(/** @scrutinizer ignore-unused */ $key = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
	{
82
		global $boardurl, $cachedir;
83
84
		// Find a valid good file to do mtime checks on.
85
		if (file_exists($cachedir . '/' . 'index.php'))
86
			$filemtime = $cachedir . '/' . 'index.php';
87
88
		elseif (is_dir($cachedir . '/'))
89
			$filemtime = $cachedir . '/';
90
91
		else
92
			$filemtime = $boardurl . '/index.php';
93
94
		// Set the default if no prefix was specified.
95
		if (empty($prefix))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prefix seems to never exist and therefore empty should always be true.
Loading history...
96
			$this->prefix = md5($boardurl . filemtime($filemtime)) . '-SMF-';
97
98
		else
99
			$this->prefix = $prefix;
100
101
		return true;
102
	}
103
104
	/**
105
	 * Gets the prefix as defined from set or the default.
106
	 *
107
	 * @access public
108
	 * @return string the value of $key.
109
	 */
110
	public function getPrefix()
111
	{
112
		return $this->prefix;
113
	}
114
115
	/**
116
	 * Sets a default Time To Live, if this isn't specified we let the class define it.
117
	 *
118
	 * @access public
119
	 * @param int $ttl The default TTL
120
	 * @return bool If this was successful or not.
121
	 */
122
	public function setDefaultTTL($ttl = 120)
123
	{
124
		$this->ttl = $ttl;
125
126
		return true;
127
	}
128
129
	/**
130
	 * Gets the TTL as defined from set or the default.
131
	 *
132
	 * @access public
133
	 * @return int the value of $ttl.
134
	 */
135
	public function getDefaultTTL()
136
	{
137
		return $this->ttl;
138
	}
139
140
	/**
141
	 * Invalidate all cached data.
142
	 *
143
	 * @return bool Whether or not we could invalidate the cache.
144
	 */
145
	public function invalidateCache()
146
	{
147
		global $cachedir;
148
149
		// Invalidate cache, to be sure!
150
		// ... as long as index.php can be modified, anyway.
151
		if (is_writable($cachedir . '/' . 'index.php'))
152
			@touch($cachedir . '/' . 'index.php');
153
154
		return true;
155
	}
156
157
	/**
158
	 * Closes connections to the cache method.
159
	 *
160
	 * @access public
161
	 * @return bool Whether the connections were closed.
162
	 */
163
	public function quit()
164
	{
165
		return true;
166
	}
167
168
	/**
169
	 * Specify custom settings that the cache API supports.
170
	 *
171
	 * @access public
172
	 * @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
173
	 */
174
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
Unused Code introduced by
The parameter $config_vars is not used and could be removed. ( Ignorable by Annotation )

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

174
	public function cacheSettings(/** @scrutinizer ignore-unused */ array &$config_vars)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
	{
176
	}
177
178
	/**
179
	 * Gets the latest version of SMF this is compatible with.
180
	 *
181
	 * @access public
182
	 * @return string the value of $key.
183
	 */
184
	public function getCompatibleVersion()
185
	{
186
		return $this->version_compatible;
187
	}
188
189
	/**
190
	 * Gets the min version that we support.
191
	 *
192
	 * @access public
193
	 * @return string the value of $key.
194
	 */
195
	public function getMinimumVersion()
196
	{
197
		return $this->min_smf_version;
198
	}
199
200
	/**
201
	 * Gets the Version of the Caching API.
202
	 *
203
	 * @access public
204
	 * @return string the value of $key.
205
	 */
206
	public function getVersion()
207
	{
208
		return $this->min_smf_version;
209
	}
210
211
	/**
212
	 * Run housekeeping of this cache
213
	 * exp. clean up old data or do optimization
214
	 *
215
	 * @access public
216
	 * @return void
217
	 */
218
	public function housekeeping()
219
	{
220
	}
221
222
	/**
223
	 * Gets the class identifier of the current caching API implementation.
224
	 *
225
	 * @access public
226
	 * @return string the unique identifier for the current class implementation.
227
	 */
228
	public function getImplementationClassKeyName()
229
	{
230
		$class_name = get_class($this);
231
232
		if ($position = strrpos($class_name, '\\'))
233
			return substr($class_name, $position + 1);
234
235
		else
236
			return get_class($this);
237
	}
238
}
239
240
?>