NormalizerObjectMappingRegistry   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 13
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
c 3
b 1
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addObjectMapping() 0 3 1
A getObjectMapping() 0 15 3
A __construct() 0 5 2
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