Passed
Push — master ( b647e5...e2d0b4 )
by Edson
01:29
created

Engine::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
use Exception;
6
7
class Engine extends Content
8
{
9
    private $config;
10
    private $data = [];
11
12
    private $file;
0 ignored issues
show
introduced by
The private property $file is not used, and could be removed.
Loading history...
13
    private $content;
0 ignored issues
show
introduced by
The private property $content is not used, and could be removed.
Loading history...
14
15
    /**
16
     * @param array $config
17
     */
18 2
    public function config(array $config): void
19
    {
20 2
        $this->config = $config;
21 2
    }
22
23
    /**
24
     * @param string $view
25
     * @param array $data
26
     * @return string
27
     */
28 2
    public function render(string $view, array $data = []): string
29
    {
30
        try {
31 2
            $content = $this->handle($this->getContent($view, $this->config));
32
        } catch (Exception $e) {
33
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
34
        }
35
36 2
        $this->data = array_merge($this->data, $data);
37
38 2
        $fname = getcwd() . '/' . $this->config['cache_dir'] . '/' . $view . '.phtml';
39
40 2
        $file = new File($fname);
41
42 2
        if ($this->config['environment'] == 'production') {
43
            $file->open();
44 2
        } elseif ($this->config['environment'] == 'development') {
45 2
            $this->setCache($file, $content);
46
        }
47
48 2
        $content = $file->read($this->data);
49
50 2
        $file->close();
51
52 2
        return $content;
53
    }
54
55 2
    public function getContent(string $view, array $config): string
56
    {
57 2
        return parent::getContent($view, $config);
58
    }
59
60 2
    private function handle($content)
61
    {
62 2
        $content = new Inheritance($content, $this->config);
63 2
        $content = new IncludeTpl($content, $this->config);
64 2
        $content = new ForeachTpl($content);
65 2
        $content = new IfTpl($content);
66 2
        $content = new FuncTpl($content);
67 2
        $content = new VariableTpl($content);
68
69 2
        return $content;
70
    }
71
72 2
    private function setCache(File $file, $content): void
73
    {
74 2
        $file->create();
75 2
        $file->write($content);
76 2
    }
77
}
78