Completed
Push — master ( e63bfe...13c6ee )
by Edson
01:15
created

Engine::config()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
use Exception;
6
7
/**
8
 * Class Engine
9
 * @package Sketch\Tpl
10
 */
11
class Engine
12
{
13
    /**
14
     * @var array
15
     */
16
    private $data = [];
17
18
    /**
19
     * @param array $config
20
     * @throws Exception
21
     */
22 2
    public function config($config): void
23
    {
24 2
        $expected = ['environment', 'template_dir', 'cache_dir'];
25
26 2
        foreach ($expected as $exp) {
27 2
            if (count($config) == 3) {
28 2
                if (!array_key_exists($exp, $config)) {
29 2
                    throw new Exception("The $exp configuration is expected");
30
                }
31
            } else {
32 2
                throw new Exception("The configuration expected only tree arguments");
33
            }
34
        }
35
36 2
        Tag::setConfig($config);
37 2
    }
38
39
    /**
40
     * @param string $view
41
     * @param array $data
42
     * @return string
43
     */
44 2
    public function render(string $view, array $data = []): string
45
    {
46
        try {
47 2
            $content = $this->handle(Content::getContent($view, Tag::getConfig()));
48
        } catch (Exception $e) { // @codeCoverageIgnore
49
            return $e->getMessage(); // @codeCoverageIgnore
50
        }
51
52 2
        $this->data = array_merge($this->data, $data);
53
54 2
        $fname = getcwd() . '/' . Tag::getConfig()['cache_dir'] . '/' . $view . '.phtml';
55
56 2
        $file = new File($fname);
57
58 2
        if (Tag::getConfig()['environment'] == 'production') {
59
            $file->open(); // @codeCoverageIgnore
60 2
        } elseif (Tag::getConfig()['environment'] == 'development') {
61 2
            $this->setCache($file, $content);
62
        }
63
64 2
        $content = $file->read($this->data);
65
66 2
        $file->close();
67
68 2
        return $content;
69
    }
70
71
    /**
72
     * @param $content
73
     * @return string
74
     */
75 2
    private function handle($content)
76
    {
77 2
        Tag::setContent($content);
78
79 2
        $this->registerTag([
80 2
            'Inheritance',
81
            'Include',
82
            'Loop',
83
            'Repeat',
84
            'If',
85
            'Func',
86
            'Variable'
87
        ]);
88
89 2
        return Tag::getContent();
90
    }
91
92
    /**
93
     * @param File $file
94
     * @param $content
95
     */
96 2
    private function setCache(File $file, $content): void
97
    {
98 2
        $file->create();
99 2
        $file->write($content);
100 2
    }
101
102
    /**
103
     * @param array $tags
104
     */
105 2
    private function registerTag(array $tags): void
106
    {
107 2
        foreach ($tags as $tag) {
108 2
            $tag = "\\Sketch\Tpl\\" . ucfirst($tag) . "Tag";
109 2
            new $tag;
110
        }
111 2
    }
112
}
113