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

DenormalizerObjectMappingRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 48
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

3 Methods

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