SimpleCacheAdaptor::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/*
4
 * This file is part of the olvlvl/symfony-validator-companion package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace olvlvl\SymfonyValidatorCompanion;
13
14
use Symfony\Component\Validator\Mapping\Cache\CacheInterface as ValidatorCache;
15
use Symfony\Component\Validator\Mapping\ClassMetadata;
16
use Psr\SimpleCache\CacheInterface as SimpleCache;
17
18
class SimpleCacheAdaptor implements ValidatorCache
19
{
20
    /**
21
     * @param string $class
22
     *
23
     * @return string
24
     */
25
    public static function normalizeKey($class)
26
    {
27
        return strtr($class, '\\', '.');
28
    }
29
30
    /**
31
     * @var SimpleCache
32
     */
33
    private $cache;
34
35
    /**
36
     * @param SimpleCache $cache
37
     */
38
    public function __construct(SimpleCache $cache)
39
    {
40
        $this->cache = $cache;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function has($class)
47
    {
48
        return $this->cache->has(self::normalizeKey($class));
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function read($class)
55
    {
56
        $result = $this->cache->get(self::normalizeKey($class));
57
58
        return $result ? $result : false;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function write(ClassMetadata $metadata)
65
    {
66
        $this->cache->set(self::normalizeKey($metadata->getClassName()), $metadata);
67
    }
68
}
69