SymfonyValidatorBridge::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\Bridge;
13
14
use Psr\Cache\CacheItemPoolInterface;
15
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
16
use Symfony\Component\Validator\Mapping\ClassMetadata;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
class SymfonyValidatorBridge implements CacheInterface
22
{
23
    /**
24
     * @type CacheItemPoolInterface
25
     */
26
    private $pool;
27
28
    /**
29
     * DoctrineCacheBridge constructor.
30
     *
31
     * @param CacheItemPoolInterface $cachePool
32
     */
33
    public function __construct(CacheItemPoolInterface $cachePool)
34
    {
35
        $this->pool = $cachePool;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function has($class)
42
    {
43
        return $this->pool->hasItem($this->normalizeKey($class));
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function read($class)
50
    {
51
        $item = $this->pool->getItem($this->normalizeKey($class));
52
53
        if (!$item->isHit()) {
54
            return false;
55
        }
56
57
        return $item->get();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function write(ClassMetadata $metadata)
64
    {
65
        $item = $this->pool->getItem($this->normalizeKey($metadata->getClassName()));
66
        $item->set($metadata);
67
        $this->pool->save($item);
68
    }
69
70
    /**
71
     * @param string $key
72
     *
73
     * @return string
74
     */
75
    private function normalizeKey($key)
76
    {
77
        return preg_replace('|[\\\/]|', '.', $key);
78
    }
79
}
80