getObjectMapping()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 8
c 1
b 0
f 1
nc 6
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 4
rs 10
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