SimpleCacheAdaptor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 5 2
A write() 0 3 1
A __construct() 0 3 1
A has() 0 3 1
A normalizeKey() 0 3 1
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