FileBasedTemplate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since: 2015-10-02
5
 */
6
namespace Net\Bazzline\Component\Template;
7
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
class FileBasedTemplate extends AbstractFileBasedTemplate implements DelimiterInterface
12
{
13
    /** @var string */
14
    private $closingDelimiter;
15
16
    /** @var string */
17
    private $openingDelimiter;
18
19
    /**
20
     * @param array $variables
21
     * @param null|string $filePath
22
     * @param string $openingDelimiter
23
     * @param string $closingDelimiter
24
     * @throws InvalidArgumentException
25
     */
26
    public function __construct($variables = array(), $filePath = null, $openingDelimiter = '{', $closingDelimiter = '}')
27
    {
28
        parent::__construct($variables, $filePath);
29
30
        $this->setPropertiesIfProvided($closingDelimiter, $openingDelimiter);
31
    }
32
33
    /**
34
     * @param array $variables
35
     * @param null $filePath
36
     * @param null|string $openingDelimiter
37
     * @param null|string $closingDelimiter
38
     * @return string
39
     * @throws InvalidArgumentException
40
     * @throws RuntimeException
41
     */
42
    public function __invoke($variables = array(), $filePath = null, $openingDelimiter = null, $closingDelimiter = null)
43
    {
44
        parent::__invoke($variables, $filePath);
45
46
        $this->setPropertiesIfProvided($closingDelimiter, $openingDelimiter);
47
48
        return $this->render();
49
    }
50
51
    /**
52
     * @param string $closingDelimiter
53
     */
54
    public function setClosingDelimiter($closingDelimiter)
55
    {
56
        $this->closingDelimiter = $closingDelimiter;
57
    }
58
59
    /**
60
     * @param string $openingDelimiter
61
     */
62
    public function setOpeningDelimiter($openingDelimiter)
63
    {
64
        $this->openingDelimiter = $openingDelimiter;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 View Code Duplication
    protected function getVariables()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $prefix     = $this->openingDelimiter;
73
        $suffix     = $this->closingDelimiter;
74
        $variables  = parent::getVariables();
75
        $array      = array();  //@todo find a better name
76
77
        foreach ($variables as $key => $value) {
78
            $array[$prefix . $key . $suffix] = $value;
79
        }
80
81
        return $array;
82
    }
83
84
    /**
85
     * @param null|string $closingDelimiter
86
     * @param null|string $openingDelimiter
87
     */
88
    private function setPropertiesIfProvided($closingDelimiter = null, $openingDelimiter = null)
89
    {
90
        if (!is_null($closingDelimiter)) {
91
            $this->setClosingDelimiter($closingDelimiter);
92
        }
93
94
        if (!is_null($openingDelimiter)) {
95
            $this->setOpeningDelimiter($openingDelimiter);
96
        }
97
    }
98
}
99