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

DoctrineCacheAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A put() 0 3 1
A evict() 0 3 1
A clear() 0 7 2
A load() 0 5 2
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