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

ObjectMappingRegistry::getObjectMappingForClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 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