Passed
Push — master ( 457070...2e278f )
by Dominik
01:56
created

ObjectMappingRegistry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A add() 0 4 1
A getObjectMappingForClass() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Registry;
6
7
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface;
8
9
final class ObjectMappingRegistry implements ObjectMappingRegistryInterface
10
{
11
    /**
12
     * @var ObjectMappingInterface[]
13
     */
14
    private $mappings;
15
16
    /**
17
     * @param ObjectMappingInterface[] $mappings
18
     */
19 2
    public function __construct(array $mappings)
20
    {
21 2
        $this->mappings = [];
22 2
        foreach ($mappings as $mapping) {
23 1
            $this->add($mapping);
24
        }
25 2
    }
26
27
    /**
28
     * @param ObjectMappingInterface $mapping
29
     */
30 1
    private function add(ObjectMappingInterface $mapping)
31
    {
32 1
        $this->mappings[$mapping->getClass()] = $mapping;
33 1
    }
34
35
    /**
36
     * @param string $class
37
     *
38
     * @return ObjectMappingInterface
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42 2
    public function getObjectMappingForClass(string $class): ObjectMappingInterface
43
    {
44 2
        if (isset($this->mappings[$class])) {
45 1
            return $this->mappings[$class];
46
        }
47
48 1
        throw new \InvalidArgumentException(sprintf('There is no mapping for class: %s', $class));
49
    }
50
}
51