Test Failed
Push — master ( 8a2b60...8e4848 )
by Dominik
03:00
created

NormalizerObjectMappingRegistry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 42
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A addObjectMapping() 0 4 1
A getObjectMapping() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\SerializerLogicException;
8
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
9
10
final class NormalizerObjectMappingRegistry implements NormalizerObjectMappingRegistryInterface
11
{
12
    /**
13
     * @var NormalizationObjectMappingInterface[]
14
     */
15
    private $objectMappings;
16
17
    /**
18
     * @param array $objectMappings
19
     */
20
    public function __construct(array $objectMappings)
21
    {
22
        $this->objectMappings = [];
23
        foreach ($objectMappings as $objectMapping) {
24
            $this->addObjectMapping($objectMapping);
25
        }
26
    }
27
28
    /**
29
     * @param NormalizationObjectMappingInterface $objectMapping
30
     */
31
    private function addObjectMapping(NormalizationObjectMappingInterface $objectMapping)
32
    {
33
        $this->objectMappings[$objectMapping->getClass()] = $objectMapping;
34
    }
35
36
    /**
37
     * @param string $class
38
     *
39
     * @return NormalizationObjectMappingInterface
40
     *
41
     * @throws SerializerLogicException
42
     */
43
    public function getObjectMapping(string $class): NormalizationObjectMappingInterface
44
    {
45
        if (isset($this->objectMappings[$class])) {
46
            return $this->objectMappings[$class];
47
        }
48
49
        throw SerializerLogicException::createMissingMapping($class);
50
    }
51
}
52