DenormalizerObjectMappingRegistry   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addObjectMapping() 0 3 1
A __construct() 0 5 2
A getObjectMapping() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
8
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
9
10
final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface
11
{
12
    /**
13
     * @var DenormalizationObjectMappingInterface[]
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 DeserializerLogicException
27
     */
28
    public function getObjectMapping(string $class): DenormalizationObjectMappingInterface
29
    {
30
        $reflectionClass = new \ReflectionClass($class);
31
32
        if (in_array('Doctrine\Common\Persistence\Proxy', $reflectionClass->getInterfaceNames(), true)) {
33
            $reflectionParentClass = (new \ReflectionClass($class))->getParentClass();
34
            if ($reflectionParentClass instanceof \ReflectionClass) {
35 3
                $class = $reflectionParentClass->getName();
36
            }
37 3
        }
38
39 3
        if (isset($this->objectMappings[$class])) {
40 1
            return $this->objectMappings[$class];
41 1
        }
42 1
43
        throw DeserializerLogicException::createMissingMapping($class);
44
    }
45
46 3
    private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping): void
47 2
    {
48
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
49
    }
50
}
51