Completed
Pull Request — master (#61)
by Vladimir
09:45
created

MarkupEngineManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addMarkupEngines() 0 7 2
A addMarkupEngine() 0 19 3
A getEngineByTag() 0 9 2
A getEngineByExtension() 0 9 2
A getTemplateTags() 0 4 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\MarkupEngine;
9
10
use __;
11
use allejo\stakx\Exception\UnsupportedMarkupException;
12
13
class MarkupEngineManager
14
{
15
    private $enginesByTags = [];
16
    private $enginesByExtension = [];
17
18
    public function addMarkupEngines(/*iterable*/ $markupEngines)
19
    {
20
        foreach ($markupEngines as $markupEngine)
21
        {
22
            $this->addMarkupEngine($markupEngine);
23
        }
24
    }
25
26
    public function addMarkupEngine(MarkupEngine $markupEngine)
27
    {
28
        $extensions = $markupEngine->getExtensions();
29
        $primaryExt = __::first($extensions);
30
31
        foreach ($extensions as $k => $extension)
32
        {
33
            if ($k === 0)
34
            {
35
                $this->enginesByExtension[$extension] = $markupEngine;
36
            }
37
            else
38
            {
39
                $this->enginesByExtension[$extension] = &$this->enginesByExtension[$primaryExt];
40
            }
41
        }
42
43
        $this->enginesByTags[$markupEngine->getTemplateTag()] = &$this->enginesByExtension[$primaryExt];
44
    }
45
46
    public function getEngineByTag($tag)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        if (isset($this->enginesByTags[$tag]))
49
        {
50
            return $this->enginesByTags[$tag];
51
        }
52
53
        throw new UnsupportedMarkupException($tag, 'There is no support to handle this markup format.');
54
    }
55
56
    public function getEngineByExtension($extension)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
57
    {
58
        if (isset($this->enginesByExtension[$extension]))
59
        {
60
            return $this->enginesByExtension[$extension];
61
        }
62
63
        throw new UnsupportedMarkupException($extension, 'There is no support to handle this markup format.');
64
    }
65
66
    public function getTemplateTags()
67
    {
68
        return array_filter(array_keys($this->enginesByTags));
69
    }
70
}
71