Template::__construct()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 16
c 1
b 0
f 1
nc 6
nop 5
dl 0
loc 23
ccs 10
cts 12
cp 0.8333
crap 4.074
rs 8.7972
1
<?php
2
namespace JayaCode\Framework\Core\View\Template;
3
4
use JayaCode\Framework\Core\View\Converter\Converter;
5
use JayaCode\Framework\Core\View\VariableCollector\VariableCollector;
6
7
/**
8
 * Class Template
9
 * @property VariableCollector vars
10
 * @package JayaCode\Framework\Core\View\Template
11
 */
12
abstract class Template
13
{
14
    /**
15
     * @var
16
     */
17
    public static $extension;
18
19
    /**
20
     * @var string
21
     */
22
    protected $locTemplate;
23
    /**
24
     * @var string
25
     */
26
    protected $fileTemplate;
27
28
    /**
29
     * @var string|null
30
     */
31
    protected $script = null;
32
33
    /**
34
     * @var Converter
35
     */
36
    protected $converter;
37
38
    /**
39
     * @var
40
     */
41
    protected $locView;
42
    /**
43
     * @var null|string
44
     */
45
    protected $cacheDir;
46
47
    /**
48
     * @var VariableCollector
49
     */
50
    protected $variableCollector;
51
52
    /**
53
     * Template constructor.
54
     * @param $locView
55
     * @param $fileTemplate
56
     * @param VariableCollector $variableCollector
57
     * @param Converter $converter
58
     * @param null $cacheDir
59
     * @throws \Exception
60
     */
61 3
    public function __construct(
62
        $locView,
63
        $fileTemplate,
64
        VariableCollector $variableCollector,
65
        Converter $converter,
66
        $cacheDir = null
67
    ) {
68 3
        $this->variableCollector = $variableCollector;
69
70 3
        $this->locView = $locView;
71 3
        $this->fileTemplate = $fileTemplate;
72 3
        $this->locTemplate = $this->locView.$fileTemplate;
73 3
        $this->converter = $converter;
74 3
        $this->cacheDir = $cacheDir?rtrim($cacheDir, "/")."/":null;
75
76 3
        if (!file_exists($this->locTemplate)) {
77
            throw new \Exception("not found file or directory {$fileTemplate}");
78
        }
79
80 3
        if (!is_readable($this->locTemplate)) {
81
            throw new \Exception("Template not readable: $this->locTemplate", 4);
82
        }
83 3
    }
84
85
    /**
86
     * @return mixed
87
     */
88
    abstract public function render();
89
90
    /**
91
     * @param $name
92
     * @return VariableCollector|null
93
     */
94 3
    public function __get($name)
95
    {
96 3
        return $name == "vars"?$this->variableCollector:null;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getScript()
103
    {
104
        return $this->script;
105
    }
106
107
    /**
108
     * @param mixed $script
109
     */
110
    public function setScript($script)
111
    {
112
        $this->script = $script;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getLocTemplates()
119
    {
120
        return $this->locTemplate;
121
    }
122
123
    /**
124
     * @param string $locTemplate
125
     */
126
    public function setLocTemplate($locTemplate)
127
    {
128
        $this->locTemplate = $locTemplate;
129
    }
130
}
131