Completed
Push — master ( 1856ca...43d45a )
by Eric
03:58
created

CacheClassMetadataFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\Mapping\Factory;
13
14
use Psr\Cache\CacheItemPoolInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class CacheClassMetadataFactory extends AbstractClassMetadataFactory
20
{
21
    /**
22
     * @var ClassMetadataFactoryInterface
23
     */
24
    private $factory;
25
26
    /**
27
     * @var CacheItemPoolInterface
28
     */
29
    private $pool;
30
31
    /**
32
     * @param ClassMetadataFactoryInterface $factory
33
     * @param CacheItemPoolInterface        $pool
34
     */
35
    public function __construct(ClassMetadataFactoryInterface $factory, CacheItemPoolInterface $pool)
36
    {
37
        $this->factory = $factory;
38
        $this->pool = $pool;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function fetchClassMetadata($class)
45
    {
46
        $item = $this->pool->getItem(strtr($class, '\\', '_'));
47
48
        if ($item->isHit()) {
49
            return $item->get();
50
        }
51
52
        $classMetadata = $this->factory->getClassMetadata($class);
53
        $this->pool->save($item->set($classMetadata));
54
55
        return $classMetadata;
56
    }
57
}
58