PhpTemplate   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 2
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setTemplateFile() 0 5 1
A addVariables() 0 4 1
A render() 0 15 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Template;
26
27
use Brickoo\Component\Template\Exception\RenderingException;
28
use Brickoo\Component\Common\Assert;
29
30
/**
31
 * PhpTemplate
32
 *
33
 * Implements a PHP based template.
34
 * @author Celestino Diaz <[email protected]>
35
 */
36
class PhpTemplate implements Template {
37
38
    /** @var string */
39
    protected $templateFile;
40
41
    /** @var array */
42
    protected $templateVars;
43
44
    /**
45
     * Class constructor.
46
     * @param string $templateFile the php template to use
47
     * @param array $templateVars the template variables to make accessible
48
     * @throws \InvalidArgumentException if an argument is not valid
49
     */
50 2
    public function __construct($templateFile, array $templateVars = []) {
51 2
        Assert::isString($templateFile);
52 1
        $this->templateFile = $templateFile;
53 1
        $this->templateVars = $templateVars;
54 1
    }
55
56
    /**
57
     * Set the template filename.
58
     * @param string $templateFile
59
     * @return \Brickoo\Component\Template\PhpTemplate
60
     */
61 1
    public function setTemplateFile($templateFile) {
62 1
        Assert::isString($templateFile);
63 1
        $this->templateFile = $templateFile;
64 1
        return $this;
65
    }
66
67
    /**
68
     * Add template variables.
69
     * Duplicate keys will be overridden.
70
     * @param array $templateVars
71
     * @return \Brickoo\Component\Template\PhpTemplate
72
     */
73 1
    public function addVariables(array $templateVars) {
74 1
        $this->templateVars = array_merge($this->templateVars, $templateVars);
75 1
        return $this;
76
    }
77
78
    /** {@inheritDoc} */
79 2
    public function render() {
80
        try {
81 2
            ob_start();
82 2
            extract($this->templateVars, EXTR_SKIP);
83 2
            require ($this->templateFile);
84 1
            $output = ob_get_contents();
85 1
            ob_end_clean();
86
        }
87 2
        catch (\Exception $exception) {
88 1
            ob_end_clean();
89 1
            throw new RenderingException($exception);
90
        }
91
92 1
        return $output;
93
    }
94
95
}
96