Passed
Pull Request — master (#42)
by
unknown
04:10
created

CachedContentFilter::getContent()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 8
nop 2
1
<?php
2
3
namespace SilverStripe\VersionFeed\Filters;
4
5
6
use SilverStripe\Core\Config\Config;
7
8
9
10
11
/**
12
 * Caches results of a callback
13
 */
14
class CachedContentFilter extends ContentFilter {
15
	
16
	/**
17
	 * Enable caching
18
	 *
19
	 * @config
20
	 * @var boolean
21
	 */
22
	private static $cache_enabled = true;
0 ignored issues
show
introduced by
The private property $cache_enabled is not used, and could be removed.
Loading history...
23
	
24
	public function getContent($key, $callback) {
25
		$cache = $this->getCache();
26
		
27
		// Return cached value if available
28
		$cacheEnabled = Config::inst()->get(get_class(), 'cache_enabled');
29
		$result = (isset($_GET['flush']) || !$cacheEnabled)
30
			? null
31
			: $cache->get($key);
32
		if($result) return $result;
33
		
34
		// Fallback to generate result
35
		$result = parent::getContent($key, $callback);
36
		$lifetime = Config::inst()->get(ContentFilter::class, 'cache_lifetime') ?: null;
37
		$cache->set($key, $result, $lifetime);
38
		return $result;
39
	}
40
}
41