Passed
Push — v5 ( c0775f...b45ef7 )
by Alexey
16:22
created

Page::setModule()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Inji\View;
4
5
use Inji\App;
6
use Inji\Controller;
7
use Inji\Module;
8
use Inji\Tools;
9
use Inji\View\Page\Assets;
10
use Inji\View\Page\Head;
11
12
class Page {
13
    /**
14
     * @var Template
15
     */
16
    public $template;
17
    /**
18
     * @var App
19
     */
20
    public $app;
21
    /**
22
     * @var Module
23
     */
24
    public $module;
25
    /**
26
     * @var Head
27
     */
28
    public $head;
29
    /**
30
     * @var Assets
31
     */
32
    public $assets;
33
    /**
34
     * @var Content
35
     */
36
    public $content;
37
    public $name = '';
38
    public $path = '';
39
    public $title = '';
40
41
42
    function __construct($params, ?App $app = null) {
43
        $this->app = $app ?? App::$cur;
44
        $this->setParams($params);
45
        $this->head = new Head($this);
46
        $this->assets = new Assets($this);
47
        if (empty($this->template->config['noInjects']) && !empty(\Inji::$config['assets']['js'])) {
48
            foreach (\Inji::$config['assets']['js'] as $js) {
49
                $this->customAsset('js', $js);
0 ignored issues
show
Bug introduced by
The method customAsset() does not exist on Inji\View\Page. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                $this->/** @scrutinizer ignore-call */ 
50
                       customAsset('js', $js);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
            }
51
        }
52
    }
53
54
    public function setTitle($title, $add = true) {
55
        if ($add && !empty($this->app->config['site']['name'])) {
56
            if ($title) {
57
                $this->title = $title . ' - ' . $this->app->config['site']['name'];
58
            } else {
59
                $this->title = $this->app->config['site']['name'];
60
            }
61
        } else {
62
            $this->title = $title;
63
        }
64
    }
65
66
    function setParams($params) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
68
        $this->setTemplate($params['template'] ?? 'default');
69
70
        $this->setPage($params['page'] ?? false);
71
72
        $this->setPath($params['pagePath'] ?? false);
73
74
        $this->setModule($params['module'] ?? false);
0 ignored issues
show
Bug introduced by
It seems like $params['module'] ?? false can also be of type false; however, parameter $module of Inji\View\Page::setModule() does only seem to accept Inji\Module, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $this->setModule(/** @scrutinizer ignore-type */ $params['module'] ?? false);
Loading history...
75
76
        $this->setContent([
77
            'name' => $params['content'] ?? false,
78
            'path' => $params['contentPath'] ?? false,
79
            'data' => $params['data'] ?? [],
80
        ]);
81
    }
82
83
    public function setContent($content = false) {
84
        if ($content instanceof Content) {
85
            $this->content = $content;
86
        }
87
        if (!$this->content) {
88
            if ($content && is_string($content)) {
89
                $this->content = new Content(['name' => $content], $this);
90
            } elseif ($content && is_array($content)) {
91
                $this->content = new Content($content, $this);
92
            } elseif (Controller::$cur && Controller::$cur->run) {
93
                $this->content = new Content(['name' => Controller::$cur->method], $this);
94
            }
95
        }
96
    }
97
98
    /**
99
     * Set module for content path finder
100
     *
101
     * @param \Inji\Module $module
102
     */
103
    public function setModule($module = null) {
104
        if (!$module && !$this->module) {
105
            $this->module = \Inji\Module::$cur;
106
        } else {
107
            $this->module = $module;
108
        }
109
        if (is_string($this->module)) {
110
            $this->module = $this->app->{$this->module};
111
        }
112
    }
113
114
    public function setPath($path = '') {
115
        if ($path) {
116
            $this->path = $path;
117
        } elseif (!$this->path) {
118
            $this->path = Tools::pathsResolve($this->possiblePaths($this->name), false);
119
        }
120
        if (!file_exists($this->path)) {
121
            throw new \Exception('Page path not exist');
122
        }
123
    }
124
125
    public function setPage($pageName = '') {
126
        if ($pageName && $pageName != 'current') {
127
            $this->name = $pageName;
128
        } elseif (!$this->name && !empty($this->app->view->config['defaultPage'])) {
0 ignored issues
show
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
            $this->name = $this->app->view->config['defaultPage'];
130
        } else {
131
            $this->name = 'index';
132
        }
133
        return $this->name;
134
    }
135
136
    public function setTemplate($template) {
137
        if ($template instanceof Template) {
138
            $this->template = $template;
139
        } elseif ($template && is_string($template) && $template != 'current') {
140
            $this->template = Template::get($template, $this->app, $this->app->view->templatesPath());
0 ignored issues
show
Bug introduced by
The method templatesPath() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            $this->template = Template::get($template, $this->app, $this->app->view->/** @scrutinizer ignore-call */ templatesPath());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug Best Practice introduced by
The property view does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method templatesPath() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\View or Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            $this->template = Template::get($template, $this->app, $this->app->view->/** @scrutinizer ignore-call */ templatesPath());
Loading history...
141
        } else {
142
            $this->template = Template::get('default', $this->app, $this->app->view->templatesPath());
143
        }
144
        return $this->template;
145
    }
146
147
    function possiblePaths($page) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
        $paths = [
149
            'template' => $this->template->path . '/' . $page . '.html'
150
        ];
151
        foreach (\Inji\Module::getModulePaths('View') as $pathName => $path) {
152
            $paths[$pathName] = $path . '/templatePages/' . $page . '.html';
153
        }
154
        return $paths;
155
    }
156
157
    function send() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
        App::$cur->log->template_parsed = true;
0 ignored issues
show
Bug introduced by
The property template_parsed does not seem to exist on Inji\Module.
Loading history...
Bug Best Practice introduced by
The property log does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
159
        if (file_exists($this->path)) {
160
            $parser = new \Inji\View\Parser\Page($this);
161
            echo $parser->parse();
162
        } else {
163
            $this->content->draw();
0 ignored issues
show
Bug introduced by
The method draw() does not exist on Inji\View\Content. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
            $this->content->/** @scrutinizer ignore-call */ 
164
                            draw();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
        }
165
    }
166
}