View::setVar()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
c 1
b 0
f 0
ccs 3
cts 6
cp 0.5
crap 2.5
1
<?php
2
declare(strict_types = 1);
3
4
namespace Phauthentic\Presentation\View;
5
6
use Phauthentic\Presentation\View\Exception\ViewException;
7
use RuntimeException;
8
9
/**
10
 * View Data Transfer Object
11
 *
12
 * A Data Transfer Object that is passed from the application layer to a
13
 * renderer to render something.
14
 */
15
class View implements ViewInterface
16
{
17
    /**
18
     * View vars
19
     *
20
     * @var array
21
     */
22
    protected $viewVars = [];
23
24
    /**
25
     * Template Path
26
     *
27
     * @var string
28
     */
29
    protected $templatePath = '';
30
31
    /**
32
     * Layout Path
33
     *
34
     * @var string
35
     */
36
    protected $layoutPath = '';
37
38
    /**
39
     * Template
40
     *
41
     * @var string
42
     */
43
    protected $template = '';
44
45
    /**
46
     * Layout
47
     *
48
     * @var string
49
     */
50
    protected $layout = '';
51
52
    /**
53
     * Template File Extension
54
     *
55
     * @var string
56
     */
57
    protected $templateFileExtension = 'php';
58
59
    /**
60
     * @inheritDoc
61 1
     */
62
    public function setVars(array $vars, bool $merge = true): ViewInterface
63 1
    {
64 1
        if ($merge) {
65
            $this->viewVars = array_merge($this->viewVars, $vars);
66
        } else {
67
            $this->viewVars = $vars;
68
        }
69 1
70
        return $this;
71
    }
72
73
    /**
74
     * @inheritDoc
75 4
     */
76
    public function setVar(string $name, $value): ViewInterface
77 4
    {
78
        if (isset($this->viewVars[$name])) {
79
            trigger_error(
80
                sprintf('A variable `%s` is already set', $name),
81
                E_USER_NOTICE
82
            );
83
        }
84 4
85
        $this->viewVars[$name] = $value;
86 4
87
        return $this;
88
    }
89
90
    /**
91
     * @inheritDoc
92 4
     */
93
    public function setTemplate(string $template): ViewInterface
94 4
    {
95
        $this->template = $template;
96 4
97
        return $this;
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function setLayout(string $layout): ViewInterface
104
    {
105
        $this->layout = $layout;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @inheritDoc
112 1
     */
113
    public function setTemplatePath(string $path): ViewInterface
114 1
    {
115
        $this->templatePath = $path;
116 1
117
        return $this;
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function setLayoutPath(string $path): ViewInterface
124
    {
125
        $this->layoutPath = $path;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @inheritDoc
132 3
     */
133
    public function template(): string
134 3
    {
135
        if (empty($this->template)) {
136
            throw ViewException::noTemplateSet();
137
        }
138 3
139
        return $this->template;
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function layout(): string
146
    {
147
        if (empty($this->template)) {
148
            throw ViewException::noLayoutSet();
149
        }
150
151
        return $this->layout;
152
    }
153
154
    /**
155
     * @inheritDoc
156 2
     */
157
    public function templatePath(): string
158 2
    {
159
        return $this->templatePath;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function layoutPath(): string
166
    {
167
        return $this->layoutPath;
168
    }
169
170
    /**
171
     * @inheritDoc
172 4
     */
173
    public function viewVars(): array
174 4
    {
175
        return $this->viewVars;
176
    }
177
}
178