Passed
Push — master ( a9f547...49cca7 )
by Raffael
04:18
created

Template   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
dl 0
loc 70
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A __get() 0 6 2
A render() 0 7 1
A translate() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Notification;
13
14
class Template
15
{
16
    /**
17
     * Template path.
18
     *
19
     * @var string
20
     */
21
    protected $template;
22
    /**
23
     * Variables.
24
     *
25
     * @var array
26
     */
27
    private $variables = [];
28
29
    /**
30
     * Current locale i18n data.
31
     *
32
     * @var array
33
     */
34
    private $locale = [];
35
36
    /**
37
     * Constructor.
38
     */
39
    public function __construct(string $path, array $locale, array $variables)
40
    {
41
        $this->locale = $locale;
42
        $this->variables = $variables;
43
44
        if (is_readable($path)) {
45
            $this->template = $path;
46
        } else {
47
            throw new Exception\TemplateNotFound('template '.$path.' not found');
48
        }
49
    }
50
51
    /**
52
     * Get variable by key.
53
     */
54
    public function __get(string $key)
55
    {
56
        if (isset($this->variables[$key])) {
57
            return $this->variables[$key];
58
        }
59
    }
60
61
    /**
62
     * Render template.
63
     */
64
    public function render(): string
65
    {
66
        ob_start();
67
        require $this->template;
68
69
        return ob_get_clean();
70
    }
71
72
    /**
73
     * Trabslate locale string.
74
     */
75
    public function translate(string $key): string
76
    {
77
        if (isset($this->locale[$key])) {
78
            return $this->locale[$key];
79
        }
80
81
        return $key;
82
    }
83
}
84