Completed
Pull Request — master (#5)
by Dominik
05:34
created

getObjectMapping()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
crap 3.0261
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
    /**
18
     * @param array $objectMappings
19
     */
20 2
    public function __construct(array $objectMappings)
21
    {
22 2
        $this->objectMappings = [];
23 2
        foreach ($objectMappings as $objectMapping) {
24 1
            $this->addObjectMapping($objectMapping);
25
        }
26 2
    }
27
28
    /**
29
     * @param DenormalizationObjectMappingInterface $objectMapping
30
     */
31 1
    private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping)
32
    {
33 1
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
34 1
    }
35
36
    /**
37
     * @param string $class
38
     *
39
     * @return DenormalizationObjectMappingInterface
40
     *
41
     * @throws DeserializerLogicException
42
     */
43 2
    public function getObjectMapping(string $class): DenormalizationObjectMappingInterface
44
    {
45 2
        $reflectionClass = new \ReflectionClass($class);
46
47 2
        if (in_array('Doctrine\Common\Persistence\Proxy', $reflectionClass->getInterfaceNames(), true)) {
48
            $class = $reflectionClass->getParentClass()->name;
49
        }
50
51 2
        if (isset($this->objectMappings[$class])) {
52 1
            return $this->objectMappings[$class];
53
        }
54
55 1
        throw DeserializerLogicException::createMissingMapping($class);
56
    }
57
}
58