Completed
Push — master ( b6962a...dbec43 )
by Eric
03:55
created

CacheNamingStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 39
ccs 0
cts 15
cp 0
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
    public function __construct(NamingStrategyInterface $strategy, CacheItemPoolInterface $cache)
36
    {
37
        $this->strategy = $strategy;
38
        $this->cache = $cache;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function doConvert($name)
45
    {
46
        $item = $this->cache->getItem($name);
47
48
        if ($item->isHit()) {
49
            return $item->get();
50
        }
51
52
        $result = $this->strategy->convert($name);
53
        $this->cache->save($item->set($result));
54
55
        return $result;
56
    }
57
}
58