Passed
Push — master ( e64f86...ea3370 )
by Terry
05:34 queued 10s
created

Cache::rebuild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
use function file_exists;
18
use function is_string;
19
use function strtolower;
20
use function ucfirst;
21
22
/**
23
 * The base Cache Adapter class.
24
 */
25
class Cache
26
{
27
    /**
28
     * The cache driver.
29
     *
30
     * @var null|CacheInterface
31
     */
32
    protected $driver;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string|CacheInterface $driver   The cache driver.
38
     * @param array                 $settings The settings.
39
     * 
40
     * @throws CacheException
41
     */
42 30
    public function __construct($driver = '', array $settings = [])
43
    {
44 30
        if ($driver instanceof CacheInterface) {
45 2
            $this->driver = $driver;
46
47 28
        } elseif (is_string($driver)) {
0 ignored issues
show
introduced by
The condition is_string($driver) is always true.
Loading history...
48 28
            $class = ucfirst(strtolower($driver));
49
50 28
            if (file_exists(__DIR__ . '/Driver/' . $class . '.php')) {
51 26
                $class = '\Shieldon\SimpleCache\Driver\\' . $class;
52
53 26
                $this->driver = new $class($settings);
54
            }
55
        }
56
57 26
        if (!$this->driver) {
58 2
            throw new CacheArgumentException(
59 2
                'The data driver is not set correctly.'
60
            );
61
        }
62 24
    }
63
64
    /**
65
     * @inheritDoc CacheInterface
66
     */
67 4
    public function get($key, $default = null)
68
    {
69 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

69
        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...
70
    }
71
72
    /**
73
     * @inheritDoc CacheInterface
74
     */
75 6
    public function set($key, $value, $ttl = null)
76
    {
77 6
        return $this->driver->set($key, $value, $ttl);
78
    }
79
80
    /**
81
     * @inheritDoc CacheInterface
82
     */
83 2
    public function delete($key)
84
    {
85 2
        return $this->driver->delete($key);
86
    }
87
88
    /**
89
     * @inheritDoc CacheInterface
90
     */
91 2
    public function clear()
92
    {
93 2
        return $this->driver->clear();
94
    }
95
96
    /**
97
     * @inheritDoc CacheInterface
98
     */
99 4
    public function has($key)
100
    {
101 4
        return $this->driver->has($key);
102
    }
103
104
    /**
105
     * @inheritDoc CacheInterface
106
     */
107 2
    public function getMultiple($keys, $default = null)
108
    {
109 2
        return $this->driver->getMultiple($keys, $default);
110
    }
111
112
    /**
113
     * @inheritDoc CacheInterface
114
     */
115 6
    public function setMultiple($values, $ttl = null)
116
    {
117 6
        return $this->driver->setMultiple($values, $ttl);
118
    }
119
120
    /**
121
     * @inheritDoc CacheInterface
122
     */
123 4
    public function deleteMultiple($keys)
124
    {
125 4
        return $this->driver->deleteMultiple($keys);
126
    }
127
128
    /**
129
     * Create or rebuid the data schema.
130
     * This method is avaialbe for Mysql and Sqlite drivers.
131
     *
132
     * @return bool
133
     */
134 4
    public function rebuild(): bool
135
    {
136 4
        if (method_exists($this->driver, 'rebuild')) {
137 2
            return $this->driver->rebuild();
138
        }
139
140 2
        return false;
141
    }
142
}