Failed Conditions
Push — master ( 28e23e...6697e2 )
by Yo
01:52
created

TemplateRegistry::getRootTemplateDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 6
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Registry;
3
4
use Yoanm\DefaultPhpRepository\Helper\PathHelper;
5
6
/**
7
 * Class TemplateRegistry
8
 */
9
class TemplateRegistry
10
{
11
    private static $rootTemplateDir = null;
12
13
    /**
14
     * @param string $templateName
15
     *
16
     * @return string the template path
17
     *
18
     * @throws \Exception if template not found
19
     */
20
    public function getTemplatePath($templateName)
21
    {
22
        $filename = sprintf(
23
            '%s%s',
24
            self::getRootTemplateDir(),
25
            $templateName
26
        );
27
28
        if (!file_exists($filename)) {
29
            throw new \Exception(sprintf('template "%s" not found !', $filename));
30
        }
31
32
        return realpath($filename);
33
    }
34
35
    public static function getRootTemplateDir()
36
    {
37
        if (null === self::$rootTemplateDir) {
38
            self::$rootTemplateDir = PathHelper::appendPathSeparator(
39
                realpath(
40
                    sprintf(
41
                        '%s../../../../templates',
42
                        PathHelper::appendPathSeparator(__DIR__)
43
                    )
44
                )
45
            );
46
        }
47
        return self::$rootTemplateDir;
48
    }
49
}
50