Completed
Push — master ( b5d125...e1aacd )
by Beñat
02:38
created

ClassMapTemplateFactory::build()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 8.9197
cc 4
eloc 13
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Domain\Model\Template;
13
14
use LIN3S\CMSKernel\Domain\Model\Template\Template;
15
use LIN3S\CMSKernel\Domain\Model\Template\TemplateContent;
16
use LIN3S\CMSKernel\Domain\Model\Template\TemplateFactory;
17
use LIN3S\CMSKernel\Domain\Model\Template\TemplateNameDoesNotExistException;
18
use LIN3S\SharedKernel\Exception\DomainException;
19
20
/**
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class ClassMapTemplateFactory implements TemplateFactory
24
{
25
    private $classMap;
26
27
    public function __construct(array $classMap)
28
    {
29
        $this->classMap = $classMap;
30
    }
31
32
    public function build($name, array $content)
33
    {
34
        foreach ($this->classMap as $key => $template) {
35
            if ($template instanceof Template) {
36
                throw new DomainException(
37
                    sprintf(
38
                        'The given temple must be a %s instance, %s given',
39
                        Template::class,
40
                        get_class($template)
41
                    )
42
                );
43
            }
44
45
            if ($template::name() === $name) {
46
                return forward_static_call_array(
47
                    [$this->classMap[$key], 'fromContent'],
48
                    [new TemplateContent($content)]
49
                );
50
            }
51
        }
52
        throw new TemplateNameDoesNotExistException($name);
53
    }
54
}
55