CacheClassMetadataFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 46
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fetchClassMetadata() 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\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
     * @var string|null
33
     */
34
    private $prefix;
35
36
    /**
37
     * @param ClassMetadataFactoryInterface $factory
38
     * @param CacheItemPoolInterface        $pool
39
     * @param string|null                   $prefix
40
     */
41 12
    public function __construct(ClassMetadataFactoryInterface $factory, CacheItemPoolInterface $pool, $prefix = null)
42
    {
43 12
        $this->factory = $factory;
44 12
        $this->pool = $pool;
45 12
        $this->prefix = $prefix;
46 12
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 8
    protected function fetchClassMetadata($class)
52
    {
53 8
        $item = $this->pool->getItem(strtr($this->prefix.$class, '\\', '_'));
54
55 8
        if ($item->isHit()) {
56 4
            return $item->get();
57
        }
58
59 4
        $classMetadata = $this->factory->getClassMetadata($class);
60 4
        $this->pool->save($item->set($classMetadata));
61
62 4
        return $classMetadata;
63
    }
64
}
65