getObjectMapping()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 4
cts 4
cp 1
crap 3
rs 10
c 3
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
8
use Chubbyphp\Serialization\SerializerLogicException;
9
10
final class NormalizerObjectMappingRegistry implements NormalizerObjectMappingRegistryInterface
11
{
12
    /**
13
     * @var NormalizationObjectMappingInterface[]
14
     */
15
    private $objectMappings;
16
17
    public function __construct(array $objectMappings)
18
    {
19
        $this->objectMappings = [];
20 3
        foreach ($objectMappings as $objectMapping) {
21
            $this->addObjectMapping($objectMapping);
22 3
        }
23 3
    }
24 2
25
    /**
26 3
     * @throws SerializerLogicException
27
     */
28
    public function getObjectMapping(string $class): NormalizationObjectMappingInterface
29
    {
30
        $reflectionClass = new \ReflectionClass($class);
31
32
        if (in_array('Doctrine\Common\Persistence\Proxy', $reflectionClass->getInterfaceNames(), true)) {
33
            /** @var \ReflectionClass $reflectionParentClass */
34
            $reflectionParentClass = $reflectionClass->getParentClass();
35 3
            $class = $reflectionParentClass->getName();
36
        }
37 3
38
        if (isset($this->objectMappings[$class])) {
39 3
            return $this->objectMappings[$class];
40 1
        }
41
42
        throw SerializerLogicException::createMissingMapping($class);
43 3
    }
44 2
45
    private function addObjectMapping(NormalizationObjectMappingInterface $objectMapping): void
46
    {
47 1
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
48
    }
49
}
50