RuntimeContentBasedTemplate::getVariables()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
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 RuntimeContentBasedTemplate extends AbstractTemplate implements DelimiterInterface
12
{
13
    /** @var string */
14
    private $closingDelimiter;
15
16
    /** @var null|string */
17
    private $content;
18
19
    /** @var string */
20
    private $openingDelimiter;
21
22
    /**
23
     * @param array $variables
24
     * @param null|string $content
25
     * @param string $openingDelimiter
26
     * @param string $closingDelimiter
27
     * @throws InvalidArgumentException
28
     */
29
    public function __construct($variables = array(), $content = null, $openingDelimiter = '{', $closingDelimiter = '}')
30
    {
31
        parent::__construct($variables);
32
33
        $this->setPropertiesIfProvided($content, $closingDelimiter, $openingDelimiter);
0 ignored issues
show
Bug introduced by
It seems like $content defined by parameter $content on line 29 can also be of type string; however, Net\Bazzline\Component\T...tPropertiesIfProvided() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
34
    }
35
36
    /**
37
     * @param array $variables
38
     * @param null|string $content
39
     * @param null|string $openingDelimiter
40
     * @param null|string $closingDelimiter
41
     * @return string
42
     * @throws InvalidArgumentException
43
     * @throws RuntimeException
44
     */
45
    public function __invoke($variables = array(), $content = null, $openingDelimiter = null, $closingDelimiter = null)
46
    {
47
        $this->setPropertiesIfProvided($content, $closingDelimiter, $openingDelimiter);
0 ignored issues
show
Bug introduced by
It seems like $content defined by parameter $content on line 45 can also be of type string; however, Net\Bazzline\Component\T...tPropertiesIfProvided() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
49
        return parent::__invoke($variables);
50
    }
51
52
    /**
53
     * @param string $closingDelimiter
54
     */
55
    public function setClosingDelimiter($closingDelimiter)
56
    {
57
        $this->closingDelimiter = $closingDelimiter;
58
    }
59
60
    /**
61
     * @param string $content
62
     * @throws InvalidArgumentException
63
     */
64
    public function setContent($content)
65
    {
66
        $content = (string) $content;
67
68
        if (strlen($content) < 1) {
69
            throw new InvalidArgumentException(
70
                'content must have a string length of at least one character'
71
            );
72
        }
73
74
        $this->content = $content;
75
    }
76
77
    /**
78
     * @param string $openingDelimiter
79
     */
80
    public function setOpeningDelimiter($openingDelimiter)
81
    {
82
        $this->openingDelimiter = $openingDelimiter;
83
    }
84
85
    /**
86
     * @return string
87
     * @throws RuntimeException
88
     */
89
    protected function getContent()
90
    {
91
        if (is_null($this->content)) {
92
            throw new RuntimeException(
93
                'no content provided'
94
            );
95
        }
96
97
        return $this->content;
98
    }
99
100
    /**
101
     * @return array
102
     */
103 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...
104
    {
105
        $prefix     = $this->openingDelimiter;
106
        $suffix     = $this->closingDelimiter;
107
        $variables  = parent::getVariables();
108
        $array      = array();  //@todo find a better name
109
110
        foreach ($variables as $key => $value) {
111
            $array[$prefix . $key . $suffix] = $value;
112
        }
113
114
        return $array;
115
    }
116
117
    /**
118
     * @param null $content
119
     * @param null|string $closingDelimiter
120
     * @param null|string $openingDelimiter
121
     * @throws InvalidArgumentException
122
     */
123
    private function setPropertiesIfProvided($content = null, $closingDelimiter = null, $openingDelimiter = null)
124
    {
125
        if (!is_null($content)) {
126
            $this->setContent($content);
127
        }
128
129
        if (!is_null($closingDelimiter)) {
130
            $this->setClosingDelimiter($closingDelimiter);
131
        }
132
133
        if (!is_null($openingDelimiter)) {
134
            $this->setOpeningDelimiter($openingDelimiter);
135
        }
136
    }
137
}
138