Passed
Pull Request — master (#11)
by Dominik
02:22
created

DenormalizerObjectMappingRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 48
ccs 0
cts 22
cp 0
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\Doctrine\Denormalizer;
6
7
use Chubbyphp\Deserialization\Denormalizer\DenormalizerObjectMappingRegistryInterface;
8
use Chubbyphp\Deserialization\DeserializerLogicException;
9
use Chubbyphp\Deserialization\Mapping\DenormalizationObjectMappingInterface;
10
use Doctrine\Common\Persistence\Proxy;
11
12
final class DenormalizerObjectMappingRegistry implements DenormalizerObjectMappingRegistryInterface
13
{
14
    /**
15
     * @var DenormalizationObjectMappingInterface[]
16
     */
17
    private $objectMappings;
18
19
    /**
20
     * @param array $objectMappings
21
     */
22
    public function __construct(array $objectMappings)
23
    {
24
        $this->objectMappings = [];
25
        foreach ($objectMappings as $objectMapping) {
26
            $this->addObjectMapping($objectMapping);
27
        }
28
    }
29
30
    /**
31
     * @param DenormalizationObjectMappingInterface $objectMapping
32
     */
33
    private function addObjectMapping(DenormalizationObjectMappingInterface $objectMapping)
34
    {
35
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
36
    }
37
38
    /**
39
     * @param string $class
40
     *
41
     * @return DenormalizationObjectMappingInterface
42
     *
43
     * @throws DeserializerLogicException
44
     */
45
    public function getObjectMapping(string $class): DenormalizationObjectMappingInterface
46
    {
47
        $reflectionClass = new \ReflectionClass($class);
48
49
        if (in_array(Proxy::class, $reflectionClass->getInterfaceNames(), true)) {
50
            $class = ($reflectionClass->getParentClass())->name;
51
        }
52
53
        if (isset($this->objectMappings[$class])) {
54
            return $this->objectMappings[$class];
55
        }
56
57
        throw DeserializerLogicException::createMissingMapping($class);
58
    }
59
}
60