Passed
Push — master ( 064bed...d815ea )
by Gerrit
04:18
created

CachedMappingDriver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 29
c 3
b 1
f 0
dl 0
loc 77
ccs 22
cts 22
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadRDMMetadataForClass() 0 35 5
A __construct() 0 10 1
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Mapping\Drivers;
12
13
use Psr\Cache\CacheItemPoolInterface;
14
use Psr\Cache\CacheItemInterface;
15
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
16
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
17
use Addiks\RDMBundle\Mapping\MappingInterface;
18
use Webmozart\Assert\Assert;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
final class CachedMappingDriver implements MappingDriverInterface
22
{
23
24
    const CACHE_KEY_FORMAT = "addiks_rdm_mapping__%s";
25
26
    /**
27
     * @var MappingDriverInterface
28
     */
29
    private $innerMappingDriver;
30
31
    /**
32
     * @var CacheItemPoolInterface
33
     */
34
    private $cacheItemPool;
35
36
    /**
37
     * @var ContainerInterface
38
     */
39
    private $container;
40
41
    /**
42
     * @var array<string, EntityMappingInterface|null>
43
     */
44
    private $internalCachedMappings = array();
45
46
    /**
47
     * @var int
48
     */
49
    private $internalCachedMappingLimit;
50
51 10
    public function __construct(
52
        MappingDriverInterface $innerMappingDriver,
53
        ContainerInterface $container,
54
        CacheItemPoolInterface $cacheItemPool,
55
        int $internalCachedMappingLimit = 100
56
    ) {
57 10
        $this->innerMappingDriver = $innerMappingDriver;
58 10
        $this->container = $container;
59 10
        $this->cacheItemPool = $cacheItemPool;
60 10
        $this->internalCachedMappingLimit = $internalCachedMappingLimit;
61
    }
62
63 9
    public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface
64
    {
65 9
        if (!array_key_exists($className, $this->internalCachedMappings)) {
66
            /** @var EntityMappingInterface|null $mapping */
67 9
            $mapping = null;
68
69
            /** @var CacheItemInterface $cacheItem */
70 9
            $cacheItem = $this->cacheItemPool->getItem(sprintf(
71
                self::CACHE_KEY_FORMAT,
72 9
                preg_replace("/[^a-zA-Z0-9]/is", "_", $className)
73
            ));
74
75 9
            if ($cacheItem->isHit()) {
76 1
                $mapping = unserialize($cacheItem->get());
77
78 1
                if (!is_null($mapping)) {
79 1
                    Assert::isInstanceOf($mapping, EntityMappingInterface::class);
80 1
                    $mapping->wakeUpMapping($this->container);
81
                }
82
83
            } else {
84 8
                $mapping = $this->innerMappingDriver->loadRDMMetadataForClass($className);
85
86 8
                $cacheItem->set(serialize($mapping));
87 8
                $this->cacheItemPool->saveDeferred($cacheItem);
88
            }
89
90 9
            $this->internalCachedMappings[$className] = $mapping;
91
92 9
            if (count($this->internalCachedMappings) > $this->internalCachedMappingLimit) {
93 1
                array_shift($this->internalCachedMappings);
94
            }
95
        }
96
97 9
        return $this->internalCachedMappings[$className];
98
    }
99
100
}
101