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

AbstractTemplateFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 31
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
templates() 0 1 ?
A keyOf() 0 10 3
A build() 0 8 2
A get() 0 6 2
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