Test Failed
Branch master (c50bb7)
by Terry
15:06 queued 10:08
created

Cache::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache;
14
15
use Psr\SimpleCache\CacheInterface;
16
use Shieldon\SimpleCache\Exception\CacheArgumentException;
17
18
/**
19
 * The base Cache Adapter class.
20
 */
21
class Cache
22
{
23
    /**
24
     * The cache driver.
25
     *
26
     * @var null|CacheInterface
27
     */
28
    protected $driver;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string|CacheInterface $driver   The cache driver.
34
     * @param array                 $settings The settings.
35
     * 
36
     * @throws CacheException
37
     */
38 26
    public function __construct($driver = '', array $settings = [])
39
    {
40 26
        if ($driver instanceof CacheInterface) {
41 2
            $this->driver = $driver;
42
43 24
        } elseif (is_string($driver)) {
0 ignored issues
show
introduced by
The condition is_string($driver) is always true.
Loading history...
44 24
            $class = ucfirst(strtolower($driver));
45
46 24
            if (file_exists(__DIR__ . '/Driver/' . $class . '.php')) {
47 22
                $class = '\Shieldon\SimpleCache\Driver\\' . $class;
48
49 22
                $this->driver = new $class($settings);
50
            }
51
        }
52
53 22
        if (!$this->driver) {
54 2
            throw new CacheArgumentException(
55 2
                'The data driver is not set correctly.'
56
            );
57
        }
58 20
    }
59
60
    /**
61
     * @inheritDoc CacheInterface
62
     */
63 4
    public function get($key, $default = null)
64
    {
65 4
        return $this->driver->get($key, $default);
1 ignored issue
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

65
        return $this->driver->/** @scrutinizer ignore-call */ get($key, $default);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
    }
67
68
    /**
69
     * @inheritDoc CacheInterface
70
     */
71 6
    public function set($key, $value, $ttl = null)
72
    {
73 6
        return $this->driver->set($key, $value, $ttl);
74
    }
75
76
    /**
77
     * @inheritDoc CacheInterface
78
     */
79 2
    public function delete($key)
80
    {
81 2
        return $this->driver->delete($key);
82
    }
83
84
    /**
85
     * @inheritDoc CacheInterface
86
     */
87 2
    public function clear()
88
    {
89 2
        return $this->driver->clear();
90
    }
91
92
    /**
93
     * @inheritDoc CacheInterface
94
     */
95 4
    public function has($key)
96
    {
97 4
        return $this->driver->has($key);
98
    }
99
100
    /**
101
     * @inheritDoc CacheInterface
102
     */
103 2
    public function getMultiple($keys, $default = null)
104
    {
105 2
        return $this->driver->getMultiple($keys, $default);
106
    }
107
108
    /**
109
     * @inheritDoc CacheInterface
110
     */
111 6
    public function setMultiple($values, $ttl = null)
112
    {
113 6
        return $this->driver->setMultiple($values, $ttl);
114
    }
115
116
    /**
117
     * @inheritDoc CacheInterface
118
     */
119 4
    public function deleteMultiple($keys)
120
    {
121 4
        return $this->driver->deleteMultiple($keys);
122
    }
123
}