Passed
Push — master ( 2cdd54...356755 )
by Gerrit
03:27
created

CachedMappingDriver::loadRDMMetadataForClass()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 1
crap 4
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
19
final class CachedMappingDriver implements MappingDriverInterface
20
{
21
22
    const CACHE_KEY_FORMAT = "addiks_rdm_mapping__%s";
23
24
    /**
25
     * @var MappingDriverInterface
26
     */
27
    private $innerMappingDriver;
28
29
    /**
30
     * @var CacheItemPoolInterface
31
     */
32
    private $cacheItemPool;
33
34
    /**
35
     * @var array<string, MappingInterface>
36
     */
37
    private $internalCachedMappings = array();
38
39
    /**
40
     * @var int
41
     */
42
    private $internalCachedMappingLimit;
43
44 9
    public function __construct(
45
        MappingDriverInterface $innerMappingDriver,
46
        CacheItemPoolInterface $cacheItemPool,
47
        int $internalCachedMappingLimit = 100
48
    ) {
49 9
        $this->innerMappingDriver = $innerMappingDriver;
50 9
        $this->cacheItemPool = $cacheItemPool;
51 9
        $this->internalCachedMappingLimit = $internalCachedMappingLimit;
52 9
    }
53
54 8
    public function loadRDMMetadataForClass(string $className): ?EntityMappingInterface
55
    {
56 8
        if (!array_key_exists($className, $this->internalCachedMappings)) {
57
            /** @var ?EntityMappingInterface $mapping */
0 ignored issues
show
Documentation introduced by
The doc-type ?EntityMappingInterface could not be parsed: Unknown type name "?EntityMappingInterface" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
58 8
            $mapping = null;
0 ignored issues
show
Unused Code introduced by
$mapping is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
60
            /** @var CacheItemInterface $cacheItem */
61 8
            $cacheItem = $this->cacheItemPool->getItem(sprintf(
62 8
                self::CACHE_KEY_FORMAT,
63 8
                preg_replace("/[^a-zA-Z0-9]/is", "_", $className)
64
            ));
65
66 8
            if ($cacheItem->isHit()) {
67 1
                $mapping = unserialize($cacheItem->get());
68
69
            } else {
70 7
                $mapping = $this->innerMappingDriver->loadRDMMetadataForClass($className);
71
72 7
                $cacheItem->set(serialize($mapping));
73 7
                $this->cacheItemPool->saveDeferred($cacheItem);
74
            }
75
76 8
            $this->internalCachedMappings[$className] = $mapping;
77
78 8
            if (count($this->internalCachedMappings) > $this->internalCachedMappingLimit) {
79 1
                array_shift($this->internalCachedMappings);
80
            }
81
        }
82
83 8
        return $this->internalCachedMappings[$className];
84
    }
85
86
}
87