AggregateThemeUnserializer::addUnserializer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\Serializer;
5
6
7
use TheCodingMachine\CMS\CMSException;
8
use TheCodingMachine\CMS\Theme\ThemeDescriptorInterface;
9
10
class AggregateThemeUnserializer implements ThemeUnserializerInterface
11
{
12
    /**
13
     * @var ThemeUnserializerInterface[]
14
     */
15
    private $unserializers;
16
17
    /**
18
     * @param ThemeUnserializerInterface[] $unserializers
19
     */
20
    public function __construct(array $unserializers = [])
21
    {
22
        $this->unserializers = $unserializers;
23
    }
24
25
    public function addUnserializer(string $type, ThemeUnserializerInterface $unserializer) : void
26
    {
27
        $this->unserializers[$type] = $unserializer;
28
    }
29
30
    public function createFromArray(array $arr): ThemeDescriptorInterface
31
    {
32
        if (!isset($arr['type'])) {
33
            throw new CMSException('Missing type key in theme.');
34
        }
35
        $type = $arr['type'];
36
        if (!isset($this->unserializers[$type])) {
37
            throw new CMSException('Unknown theme type: '.$type);
38
        }
39
40
        return $this->unserializers[$type]->createFromArray($arr);
41
    }
42
}