Passed
Pull Request — master (#97)
by Alexander
11:12
created

DoctrineCacheAdapter::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata\Cache;
6
7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\Common\Cache\ClearableCache;
9
use Metadata\ClassMetadata;
10
11
/**
12
 * @author Henrik Bjornskov <[email protected]>
13
 */
14
class DoctrineCacheAdapter implements CacheInterface, ClearableCacheInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $prefix;
20
    /**
21
     * @var Cache
22
     */
23
    private $cache;
24 2
25
    public function __construct(string $prefix, Cache $cache)
26 2
    {
27 2
        $this->prefix = $prefix;
28 2
        $this->cache = $cache;
29
    }
30 2
31
    public function load(string $class): ?ClassMetadata
32 2
    {
33
        $cache = $this->cache->fetch($this->prefix . $class);
34 2
35
        return false === $cache ? null : $cache;
36
    }
37 2
38
    public function put(ClassMetadata $metadata): void
39 2
    {
40 2
        $this->cache->save($this->prefix . $metadata->name, $metadata);
41
    }
42 2
43
    public function evict(string $class): void
44 2
    {
45 2
        $this->cache->delete($this->prefix . $class);
46
    }
47
48
    public function clear(): bool
49
    {
50
        if ($this->cache instanceof ClearableCache) {
51
            return $this->cache->deleteAll();
52
        }
53
54
        return false;
55
    }
56
}
57