Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

EngineRegistry::getSupportedExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
namespace ntentan\honam;
5
6
7
use ntentan\honam\engines\AbstractEngine;
8
use ntentan\honam\exceptions\FactoryException;
9
use ntentan\honam\exceptions\TemplateEngineNotFoundException;
10
use ntentan\honam\factories\EngineFactoryInterface;
11
12
class EngineRegistry
13
{
14
    private $engines = [];
15
    private $extensions = [];
16
17
18 37
    public function registerEngine($extensions, $factory)
19
    {
20 37
        foreach($extensions as $extension) {
21 37
            $this->extensions[$extension] = $factory;
22
        }
23 37
    }
24
25 29
    public function getSupportedExtensions()
26
    {
27 29
        return array_keys($this->extensions);
28
    }
29
30 28
    public function getEngineInstance($templateFile) : AbstractEngine
31
    {
32 28
        foreach($this->extensions as $extension => $factory) {
33 28
            if($extension == substr($templateFile, -strlen($extension))) {
34 28
                if(is_a($factory, EngineFactoryInterface::class)) {
35 28
                    $engine = $factory->create(); //$this->templateRenderer);
36
                } else if (is_callable($factory)) {
37
                    $engine = $factory(); //$this->templateRenderer);
38
                } else {
39
                    throw new FactoryException("There is no factory for creating engines for the {$extension} extension");
40
                }
41
                //$engine->setTemplateRenderer($this->templateRenderer);
42 28
                return $engine;
43
            }
44
        }
45
46
        throw new TemplateEngineNotFoundException("An engine for rendering the {$templateFile} extension was not found.");
47
    }
48
}
49