Completed
Push — master ( fb3818...01966b )
by Beñat
02:21
created

AbstractTemplateFactory::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel library.
5
 *
6
 * Copyright (c) 2016 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\Domain\Model\Page\Template;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 */
17
abstract class AbstractTemplateFactory implements TemplateFactory
18
{
19
    abstract public function templates();
20
21
    public function keyOf($aValue)
22
    {
23
        $templates = $this->templates();
24
        while ($value = current($templates)) {
25
            if ($value === get_class($aValue)) {
26
                return key($templates);
27
            }
28
            next($templates);
29
        }
30
    }
31
32
    public function build($name, array $content)
33
    {
34
        if (!array_key_exists($name, $this->templates())) {
35
            throw new TemplateNameDoesNotExistException($name);
36
        }
37
38
        return forward_static_call_array([$this->get($name), 'fromContent'], [new TemplateContent($content)]);
39
    }
40
41
    private function get($name)
42
    {
43
        if (array_key_exists($name, $this->templates())) {
44
            return $this->templates()[$name];
45
        }
46
    }
47
}
48