Completed
Pull Request — master (#19)
by Eric
04:48
created

CacheNamingStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A doConvert() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Naming;
13
14
use Psr\Cache\CacheItemPoolInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class CacheNamingStrategy extends AbstractNamingStrategy
20
{
21
    /**
22
     * @var NamingStrategyInterface
23
     */
24
    private $strategy;
25
26
    /**
27
     * @var CacheItemPoolInterface
28
     */
29
    private $cache;
30
31
    /**
32
     * @param NamingStrategyInterface $strategy
33
     * @param CacheItemPoolInterface  $cache
34
     */
35 8
    public function __construct(NamingStrategyInterface $strategy, CacheItemPoolInterface $cache)
36
    {
37 8
        $this->strategy = $strategy;
38 8
        $this->cache = $cache;
39 8
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 8
    protected function doConvert($name)
45
    {
46 8
        $item = $this->cache->getItem($name);
47
48 8
        if ($item->isHit()) {
49 4
            return $item->get();
50
        }
51
52 4
        $result = $this->strategy->convert($name);
53 4
        $this->cache->save($item->set($result));
54
55 4
        return $result;
56
    }
57
}
58