Completed
Pull Request — master (#6648)
by Ingo
08:49
created

DefaultCacheFactory::create()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 48
nop 2
dl 0
loc 32
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Cache;
4
5
use SilverStripe\Core\Injector\Injector;
6
use Symfony\Component\Cache\Simple\FilesystemCache;
7
use Symfony\Component\Cache\Simple\ApcuCache;
8
use Symfony\Component\Cache\Simple\ChainCache;
9
use Symfony\Component\Cache\Simple\PhpFilesCache;
10
use Symfony\Component\Cache\Adapter\ApcuAdapter;
11
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
12
13
/**
14
 * Returns the most performant combination of caches available on the system:
15
 * - `PhpFilesCache` (PHP 7 with opcache enabled)
16
 * - `ApcuCache` (requires APC) with a `FilesystemCache` fallback (for larger cache volumes)
17
 * - `FilesystemCache` if none of the above is available
18
 *
19
 * Modelled after `Symfony\Component\Cache\Adapter\AbstractAdapter::createSystemCache()`
20
 */
21
class DefaultCacheFactory implements CacheFactory
22
{
23
24
    /**
25
     * @var string Absolute directory path
26
     */
27
    protected $directory;
28
29
    /**
30
     * @var string APC version for apcu_add()
31
     */
32
    protected $version;
33
34
    /**
35
     * @param string $directory
36
     * @param string $version
37
     */
38
    public function __construct($directory, $version = null)
39
    {
40
        $this->directory = $directory;
41
        $this->version = $version;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function create($service, array $params = array())
48
    {
49
        $namespace = (isset($args['namespace'])) ? $args['namespace'] : '';
0 ignored issues
show
Bug introduced by
The variable $args seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
50
        $defaultLifetime = (isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0;
51
        $version = $this->version;
52
        $directory = $this->directory;
53
54
        $apcuSupported = null;
55
        $phpFilesSupported = null;
56
57
        if (null === $apcuSupported) {
58
            $apcuSupported = ApcuAdapter::isSupported();
59
        }
60
61
        if (!$apcuSupported && null === $phpFilesSupported) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $apcuSupported of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
62
            $phpFilesSupported = PhpFilesAdapter::isSupported();
63
        }
64
65
        if ($phpFilesSupported) {
66
            $opcache = Injector::inst()->create(PhpFilesCache::class, false, [$namespace, $defaultLifetime, $directory]);
67
            return $opcache;
68
        }
69
70
        $fs = Injector::inst()->create(FilesystemCache::class, false, [$namespace, $defaultLifetime, $directory]);
71
        if (!$apcuSupported) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $apcuSupported of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
72
            return $fs;
73
        }
74
75
        $apcu = Injector::inst()->create(ApcuCache::class, false, [$namespace, (int) $defaultLifetime / 5, $version]);
76
77
        return Injector::inst()->create(ChainCache::class, false, [[$apcu, $fs]]);
78
    }
79
}
80