Passed
Push — master ( 6692d3...4dd3a5 )
by Sébastien
02:56 queued 15s
created

DoctrineCacheAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bdf\Prime\Cache;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
7
/**
8
 * Adapter for doctrine cache provider
9
 */
10
class DoctrineCacheAdapter implements CacheInterface
11
{
12
    /**
13
     * @var CacheProvider
14
     */
15
    private $driver;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
16
17
    /**
18
     * @var CacheProvider[]
19
     */
20
    private $driverByNamespace = [];
21
22
23
    /**
24
     * DoctrineCacheAdapter constructor.
25
     *
26
     * @param CacheProvider $driver
27
     */
28 18
    public function __construct(CacheProvider $driver)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
29
    {
30 18
        $this->driver = $driver;
31 18
    }
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
34
     * {@inheritdoc}
35
     */
36 18
    public function get(CacheKey $key)
37
    {
38 18
        $data = $this->namespace($key->namespace())->fetch($key->key());
39
40 18
        return $data === false ? null : $data;
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
41
    }
42
43
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
44
     * {@inheritdoc}
45
     */
46 17
    public function set(CacheKey $key, $data)
47
    {
48 17
        $this->namespace($key->namespace())->save($key->key(), $data, $key->lifetime());
49 17
    }
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $key should have a doc-comment as per coding-style.
Loading history...
52
     * {@inheritdoc}
53
     */
54 1
    public function delete(CacheKey $key)
55
    {
56 1
        $this->namespace($key->namespace())->delete($key->key());
57 1
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $namespace should have a doc-comment as per coding-style.
Loading history...
60
     * {@inheritdoc}
61
     */
62 8
    public function flush($namespace)
63
    {
64 8
        $this->namespace($namespace)->deleteAll();
65 8
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function clear()
71
    {
72 1
        $this->driver->flushAll();
73
74 1
        foreach ($this->driverByNamespace as $driver) {
75 1
            $driver->flushAll();
76
        }
77 1
    }
78
79
    /**
80
     * Get the cache provider for the given namespace
81
     *
82
     * @param string $ns
83
     *
84
     * @return CacheProvider
85
     */
86 18
    private function namespace(string $ns): CacheProvider
0 ignored issues
show
Coding Style introduced by
Private method name "DoctrineCacheAdapter::namespace" must be prefixed with an underscore
Loading history...
87
    {
88 18
        if (isset($this->driverByNamespace[$ns])) {
89 17
            return $this->driverByNamespace[$ns];
90
        }
91
92 18
        $driver = clone $this->driver;
93 18
        $driver->setNamespace($ns);
94
95 18
        return $this->driverByNamespace[$ns] = $driver;
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
96
    }
97
}
98