ContentFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getCache() 0 4 1
A getContent() 0 6 2
1
<?php
2
3
namespace SilverStripe\VersionFeed\Filters;
4
5
use SilverStripe\VersionFeed\VersionFeedController;
6
use SilverStripe\Core\Config\Configurable;
7
use Psr\SimpleCache\CacheInterface;
8
use SilverStripe\Core\Injector\Injector;
9
10
/**
11
 * Conditionally executes a given callback, attempting to return the desired results
12
 * of its execution.
13
 */
14
abstract class ContentFilter
15
{
16
17
    use Configurable;
18
    
19
    /**
20
     * Nested content filter
21
     *
22
     * @var ContentFilter
23
     */
24
    protected $nestedContentFilter;
25
26
    /**
27
     * Cache lifetime
28
     *
29
     * @config
30
     * @var int
31
     */
32
    private static $cache_lifetime = 300;
0 ignored issues
show
introduced by
The private property $cache_lifetime is not used, and could be removed.
Loading history...
33
    
34
    public function __construct($nestedContentFilter = null)
35
    {
36
        $this->nestedContentFilter = $nestedContentFilter;
37
    }
38
    
39
    /**
40
     * Gets the cache to use
41
     *
42
     * @return CacheInterface
43
     */
44
    protected function getCache()
45
    {
46
        return Injector::inst()->get(
47
            CacheInterface::class . '.VersionFeedController'
48
        );
49
    }
50
    
51
    /**
52
     * Evaluates the result of the given callback
53
     *
54
     * @param string $key Unique key for this
55
     * @param callable $callback Callback for evaluating the content
56
     * @return mixed Result of $callback()
57
     */
58
    public function getContent($key, $callback)
59
    {
60
        if ($this->nestedContentFilter) {
61
            return $this->nestedContentFilter->getContent($key, $callback);
62
        } else {
63
            return call_user_func($callback);
64
        }
65
    }
66
}
67