Test Failed
Pull Request — dev (#1)
by Paweł
12:17
created

MagicView   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 33
eloc 74
c 3
b 0
f 0
dl 0
loc 280
rs 9.76

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharset() 0 3 1
A render() 0 12 1
A getCssBlocks() 0 3 1
B registerDefaultTriggers() 0 31 6
A getJsBlocks() 0 3 1
A callTrigger() 0 4 2
A addJsFile() 0 3 1
A loadLanguage() 0 6 3
A getCssFiles() 0 3 1
A asset() 0 6 2
A setTitle() 0 4 2
A setCharset() 0 3 1
A registerTrigger() 0 3 1
A addCssBlock() 0 3 1
A getTitle() 0 3 1
A clear() 0 7 1
A extend() 0 4 1
A addHeadTag() 0 3 1
A addCssFile() 0 3 1
A getJsFiles() 0 3 1
A __construct() 0 4 1
A getHeadTags() 0 3 1
A addJsBlock() 0 3 1
1
<?php
2
3
namespace pjpawel\Magis\View;
4
5
use pjpawel\Magis\Exception\TemplateException;
6
use pjpawel\Magis\Helper\AppContainerInterface;
7
use pjpawel\Magis\Helper\Script;
8
use pjpawel\Magis\Helper\Style;
9
use pjpawel\Magis\Helper\Tag;
10
use pjpawel\Magis\Template;
11
12
/**
13
 * @author Paweł Podgórski <[email protected]>
14
 */
15
class MagicView extends AbstractView
16
{
17
18
    /**
19
     * Content of html
20
     * @var string
21
     */
22
    private string $content = '';
23
    /**
24
     * Registered events
25
     * @var array<string, callable>
26
     */
27
    private array $events = [];
28
    /**
29
     * @var list<Tag>
0 ignored issues
show
Bug introduced by
The type pjpawel\Magis\View\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     */
31
    private array $headTags = [];
32
    /**
33
     * @var list<string>
34
     */
35
    private array $jsFiles = [];
36
    /**
37
     * @var list<string>
38
     */
39
    private array $cssFiles = [];
40
    /**
41
     * @var list<Script>
42
     */
43
    private array $jsBlocks = [];
44
    /**
45
     * @var list<Style>
46
     */
47
    private array $cssBlocks = [];
48
    private string $language = 'en';
49
    private string $charset = 'UTF-8';
50
    private string $title = '';
51
52
53
    public function __construct(string $templateDir)
54
    {
55
        parent::__construct($templateDir);
56
        $this->registerDefaultTriggers();
57
    }
58
59
    protected function registerDefaultTriggers()
60
    {
61
        $this->registerTrigger(
62
            Event::BeforeRun,
63
            function (MagicView $view) {
64
                $view->content .= '<!DOCTYPE html><html lang="' . $view->loadLanguage() . '">';
65
                $view->content .= '<head><title>' . $view->getTitle() . '</title><meta charset="' . $view->getCharset() . '">';
66
                foreach ($view->getHeadTags() as $tag) {
67
                    $view->content .= $tag->show() . PHP_EOL;
68
                }
69
                foreach ($view->getCssBlocks() as $css) {
70
                    $view->content .= $css->show() . PHP_EOL;
71
                }
72
                $view->content .= '</head>';
73
                $view->content .= '<body>';
74
                foreach ($view->getCssFiles() as $css) {
75
                    $view->content .= $css . PHP_EOL;
76
                }
77
            }
78
        );
79
        $this->registerTrigger(
80
            Event::AfterRun,
81
            function (MagicView $view) {
82
                foreach ($view->getJsFiles() as $js) {
83
                    $view->content .= $js . PHP_EOL;
84
                }
85
                foreach ($view->getJsBlocks() as $js) {
86
                    $view->content .= $js->show() . PHP_EOL;
87
                }
88
                $view->content .= '</body>';
89
                $view->content .= '</html>';
90
            }
91
        );
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function render(string $template, array $params = []): string
98
    {
99
        $template = Template::resolveTemplatePath($this->templateDir, $template);
100
        $this->params = array_merge_recursive($params, $this->params);
101
        $content = $this->renderPhpFile($template, $this->params);
102
103
        $this->callTrigger(Event::BeforeRun);
104
        $this->content .= $content;
105
        $this->callTrigger(Event::AfterRun);
106
        $this->callTrigger(Event::AfterRender);
107
108
        return $this->content;
109
    }
110
111
    public function extend(string $template, array $params = []): string
112
    {
113
        $template = Template::resolveTemplatePath($this->templateDir, $template);
114
        return $this->renderPhpFile($template, $params);
115
    }
116
117
    /**
118
     * @param Event $event
119
     * @param callable $function
120
     * @return void
121
     */
122
    public function registerTrigger(Event $event, callable $function): void
123
    {
124
        $this->events[$event->name] = $function;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on pjpawel\Magis\View\Event.
Loading history...
125
    }
126
127
    public function callTrigger(Event $event): void
128
    {
129
        if (array_key_exists($event->name, $this->events)) {
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on pjpawel\Magis\View\Event.
Loading history...
130
            call_user_func($this->events[$event->name], $this);
131
        }
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function loadLanguage(): string
138
    {
139
        if (isset($this->app) && is_subclass_of($this->app, AppContainerInterface::class)) {
140
            $this->language = $this->app->get('request')->getLocale();
141
        }
142
        return $this->language;
143
    }
144
145
    /**
146
     * @return list<Tag>
147
     */
148
    public function getHeadTags(): array
149
    {
150
        return $this->headTags;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headTags returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
151
    }
152
153
    /**
154
     * @param Tag $tag
155
     */
156
    public function addHeadTag(Tag $tag): void
157
    {
158
        $this->headTags[] = $tag;
159
    }
160
161
    /**
162
     * This will check if title is set,
163
     * WARNING! This will not override title that was set!
164
     *
165
     * @param string $title
166
     * @return void
167
     */
168
    public function setTitle(string $title): void
169
    {
170
        if (!isset($this->title)) {
171
            $this->title = $title;
172
        }
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function getTitle(): string
179
    {
180
        return $this->title;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getCharset(): string
187
    {
188
        return $this->charset;
189
    }
190
191
    /**
192
     * Clears workspace
193
     *
194
     * @return void
195
     */
196
    public function clear(): void
197
    {
198
        $this->headTags = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type pjpawel\Magis\View\list of property $headTags.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
199
        $this->jsFiles = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type pjpawel\Magis\View\list of property $jsFiles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
200
        $this->cssFiles = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type pjpawel\Magis\View\list of property $cssFiles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
201
        $this->jsBlocks = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type pjpawel\Magis\View\list of property $jsBlocks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
202
        $this->cssBlocks = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type pjpawel\Magis\View\list of property $cssBlocks.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
203
    }
204
205
    /**
206
     * Should be injected to the view as PackageInterface
207
     *
208
     * @param $path
209
     * @return string
210
     * @throws TemplateException
211
     * @deprecated
212
     */
213
    public function asset($path): string
214
    {
215
        if (!isset($this->asset)) {
216
            throw new TemplateException();
217
        }
218
        return $this->asset->getUrl($path);
219
    }
220
221
    /**
222
     * @return list<string>
223
     */
224
    public function getCssFiles(): array
225
    {
226
        return $this->cssFiles;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cssFiles returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
227
    }
228
229
    /**
230
     * @return list<Style>
231
     */
232
    public function getCssBlocks(): array
233
    {
234
        return $this->cssBlocks;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cssBlocks returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
235
    }
236
237
    /**
238
     * @return list<string>
239
     */
240
    public function getJsFiles(): array
241
    {
242
        return $this->jsFiles;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->jsFiles returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
243
    }
244
245
    /**
246
     * @return list<Script>
247
     */
248
    public function getJsBlocks(): array
249
    {
250
        return $this->jsBlocks;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->jsBlocks returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
251
    }
252
253
    /**
254
     * @param string $charset
255
     */
256
    public function setCharset(string $charset): void
257
    {
258
        $this->charset = $charset;
259
    }
260
261
    /**
262
     * @param string $cssFile
263
     * @return void
264
     */
265
    public function addCssFile(string $cssFile): void
266
    {
267
        $this->cssFiles[] = $cssFile;
268
    }
269
270
    /**
271
     * @param string $cssFile
272
     * @return void
273
     */
274
    public function addJsFile(string $cssFile): void
275
    {
276
        $this->jsFiles[] = $cssFile;
277
    }
278
279
    /**
280
     * @param Style $cssBlock
281
     * @return void
282
     */
283
    public function addCssBlock(Style $cssBlock): void
284
    {
285
        $this->cssBlocks[] = $cssBlock;
286
    }
287
288
    /**
289
     * @param Script $jsBlock
290
     * @return void
291
     */
292
    public function addJsBlock(Script $jsBlock): void
293
    {
294
        $this->jsBlocks[] = $jsBlock;
295
    }
296
297
298
}