Completed
Pull Request — master (#61)
by Vladimir
03:02
created

MarkupEngineManager::addMarkupEngine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 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 $engines;
16
17
    public function __construct()
18
    {
19
        $this->engines = [];
20
    }
21
22
    public function addMarkupEngines(/*iterable*/ $markupEngines)
23
    {
24
        foreach ($markupEngines as $markupEngine)
25
        {
26
            $this->addMarkupEngine($markupEngine);
27
        }
28
    }
29
30
    public function addMarkupEngine(MarkupEngine $markupEngine)
31
    {
32
        $extensions = $markupEngine->getExtensions();
33
        $primaryExt = __::first($extensions);
34
35
        foreach ($extensions as $k => $extension)
36
        {
37
            if ($k === 0)
38
            {
39
                $this->engines[$extension] = $markupEngine;
40
            }
41
            else
42
            {
43
                $this->engines[$extension] = &$this->engines[$primaryExt];
44
            }
45
        }
46
    }
47
48
    public function getMarkupEngine($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...
49
    {
50
        if (isset($this->engines[$extension]))
51
        {
52
            return $this->engines[$extension];
53
        }
54
55
        throw new UnsupportedMarkupException($extension, 'There is no support to handle this markup format.');
56
    }
57
}
58