Test Failed
Push — main ( 82933d...ebe982 )
by Rafael
02:28
created

ViewTrait::getTemplatesPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Base\Controller\Trait;
20
21
use Jenssegers\Blade\Blade;
0 ignored issues
show
Bug introduced by
The type Jenssegers\Blade\Blade 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...
22
23
trait ViewTrait
24
{
25
    public static $messages = [];
26
    /**
27
     * Theme name. TODO: Has to be updated according to the configuration.
28
     *
29
     * @var string
30
     */
31
    public $theme;
32
    /**
33
     * Code lang for <html lang> tag
34
     *
35
     * @var string
36
     */
37
    public $lang = 'en';
38
    public $body_class;
39
    public $templatesPath;
40
    public $template;
41
    public $title;
42
    public $alerts;
43
44
    public static function addMessage($message)
45
    {
46
        self::$messages[]['success'] = $message;
47
    }
48
49
    public static function addAdvice($message)
50
    {
51
        self::$messages[]['warning'] = $message;
52
    }
53
54
    public static function addError($message)
55
    {
56
        self::$messages[]['danger'] = $message;
57
    }
58
59
    public function __destruct()
60
    {
61
        if (!isset($this->template)) {
62
            return;
63
        }
64
65
        if (!isset($this->theme)) {
66
            $this->theme = 'alixar';
67
        }
68
69
        if (!isset($this->title)) {
70
            $this->title = 'Alixar';
71
        }
72
73
        $this->alerts = static::getMessages();
74
75
        $vars = ['me' => $this];
76
        $viewPaths = [
77
            BASE_PATH . '/Templates',
0 ignored issues
show
Bug introduced by
The constant Alxarafe\Base\Controller\Trait\BASE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
            BASE_PATH . '/Templates/theme/' . $this->theme,
79
            BASE_PATH . '/Templates/common',
80
        ];
81
82
        if (isset($this->templatesPath)) {
83
            array_unshift($viewPaths, $this->templatesPath);
84
        }
85
86
        $cachePaths = realpath(BASE_PATH . '/..') . '/tmp/blade';
87
        if (!is_dir($cachePaths) && !mkdir($cachePaths, 0777, true) && !is_dir($cachePaths)) {
88
            die('Could not create cache directory for templates: ' . $cachePaths);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
89
        }
90
        $blade = new Blade($viewPaths, $cachePaths);
91
        echo $blade->render($this->template, $vars);
92
    }
93
94
    public static function getMessages()
95
    {
96
        $alerts = [];
97
        foreach (self::$messages as $message) {
98
            foreach ($message as $type => $text) {
99
                $alerts[] = [
100
                    'type' => $type,
101
                    'text' => $text
102
                ];
103
            }
104
        }
105
        self::$messages = [];
106
        return $alerts;
107
    }
108
109
    public function getTemplatesPath(): string
110
    {
111
        return $this->templatesPath;
112
    }
113
114
    public function setTemplatesPath(string $path)
115
    {
116
        $this->templatesPath = $path;
117
    }
118
}
119