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

Template::getContentPaths()   D

Complexity

Conditions 12
Paths 216

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 25
nc 216
nop 1
dl 0
loc 37
rs 4.4061
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Template
5
 *
6
 * Object for template
7
 *
8
 * @author Alexey Krupskiy <[email protected]>
9
 * @link http://inji.ru/
10
 * @copyright 2015 Alexey Krupskiy
11
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
12
 */
13
14
namespace Inji\View;
15
16
use Inji\Config;
17
18
class Template extends \Inji\InjiObject {
19
20
    /**
21
     * App for template
22
     *
23
     * @var \App
24
     */
25
    public $app = null;
26
27
    /**
28
     * Template name
29
     *
30
     * @var string
31
     */
32
    public $name = 'default';
33
34
    /**
35
     * Template path
36
     *
37
     * @var string
38
     */
39
    public $path = '';
40
41
    /**
42
     * Template config path
43
     *
44
     * @var string
45
     */
46
    public $configPath = '';
47
48
    /**
49
     * Template config
50
     *
51
     * @var array
52
     */
53
    public $config = [];
54
55
    /**
56
     * Current template page for rendering
57
     *
58
     * @var string
59
     */
60
    public $page = 'index';
61
62
    /**
63
     * Current template page path for rendering
64
     *
65
     * @var string|boolean
66
     */
67
    public $pagePath = '';
68
69
    /**
70
     * Template module for content path finder
71
     *
72
     * @var \Module
73
     */
74
    public $module = null;
75
76
    /**
77
     * Current content file for rendering
78
     *
79
     * @var string
80
     */
81
    public $content = '';
82
83
    /**
84
     * Current content file path for rendering
85
     *
86
     * @var string|boolean
87
     */
88
    public $contentPath = '';
89
90
    /**
91
     * Setup template object
92
     *
93
     * @param array $params
94
     */
95
    public function __construct($params = []) {
96
        $this->setParams($params);
97
        if (!$this->path) {
98
            $this->path = $this->app->view->templatesPath() . '/' . $this->name;
99
        }
100
        $this->loadConfig();
101
    }
102
103
    /**
104
     * Load template config
105
     *
106
     * @param string $configPath
107
     */
108
    public function loadConfig($configPath = '') {
109
        if (!$configPath) {
110
            $configPath = $this->path . '/config.php';
111
        }
112
        $this->configPath = $configPath;
113
        $this->config = Config::custom($this->configPath);
114
    }
115
116
    /**
117
     * Set params for template
118
     *
119
     * @param array $params
120
     */
121
    public function setParams($params) {
122
        foreach ($params as $param => $value) {
123
            $this->$param = $value;
124
        }
125
    }
126
127
    /**
128
     * Set page and page path for template
129
     *
130
     * @param string $page
131
     */
132
    public function setPage($page = '') {
133
        if (!$page) {
134
            $page = !empty($this->config['defaultPage']) ? $this->config['defaultPage'] : $this->page;
135
        }
136
        $this->page = $page;
137
        if (!$this->pagePath) {
138
            $this->pagePath = $this->path . '/' . $this->page . '.html';
139
        }
140
        $this->pagePath = \Inji\Tools::pathsResolve($this->getPagePaths(), $this->pagePath);
0 ignored issues
show
Bug introduced by
The method getPagePaths() does not exist on Inji\View\Template. ( 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->pagePath = \Inji\Tools::pathsResolve($this->/** @scrutinizer ignore-call */ getPagePaths(), $this->pagePath);

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...
141
    }
142
143
    /**
144
     * Set content file for rendering
145
     *
146
     * @param string $content
147
     */
148
    public function setContent($content = '') {
149
        if ($content) {
150
            $this->content = $content;
151
        }
152
        if (\Inji\Controller::$cur && \Inji\Controller::$cur->run) {
153
            if (!$this->content) {
154
                $this->content = \Inji\Controller::$cur->method;
155
            }
156
            if ((!$this->contentPath || $content) && \Inji\Module::$cur) {
157
                $this->contentPath = \Inji\Module::$cur->path . '/' . \Inji\Module::$cur->app->type . "Controllers/content/{$this->content}.php";
158
            }
159
            $this->contentPath = \Inji\Tools::pathsResolve($this->getContentPaths(), $this->contentPath);
0 ignored issues
show
Bug introduced by
$this->getContentPaths() of type string is incompatible with the type array expected by parameter $paths of Inji\Tools::pathsResolve(). ( Ignorable by Annotation )

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

159
            $this->contentPath = \Inji\Tools::pathsResolve(/** @scrutinizer ignore-type */ $this->getContentPaths(), $this->contentPath);
Loading history...
160
        }
161
    }
162
163
    /**
164
     * Return posible path for content file by name
165
     *
166
     * @param string $content
167
     * @return string
168
     */
169
    public function getContentPaths($content = '') {
170
        if (!$content) {
171
            $content = $this->content;
172
        }
173
        $paths = [];
174
        if ($this->module) {
175
            if (\Inji\Controller::$cur) {
176
                $paths['templateModuleController'] = $this->path . "/modules/{$this->module->name}/" . \Inji\Controller::$cur->name . "/{$content}.php";
177
            }
178
            $paths['templateModule'] = $this->path . "/modules/{$this->module->name}/{$content}.php";
179
        }
180
        if (\Inji\Module::$cur) {
181
            if (\Inji\Controller::$cur) {
182
                $paths['templateCurModuleController'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php";
183
            }
184
            $paths['templateCurModule'] = $this->path . "/modules/" . \Inji\Module::$cur->name . "/{$content}.php";
185
        }
186
        if (\Inji\Controller::$cur) {
187
            $modulePaths = \Inji\Module::getModulePaths(\Inji\Controller::$cur->module->name);
188
            foreach ($modulePaths as $key => $modulePath) {
189
                $paths['module_' . $key . '_appType'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . '/' . \Inji\Controller::$cur->name . "/{$content}.php";
190
                $paths['module_' . $key . '_appType_controllerName'] = $modulePath . '/views/' . \Inji\Controller::$cur->module->app->type . "/{$content}.php";
191
                $paths['module_' . $key] = $modulePath . '/views/' . "/{$content}.php";
192
            }
193
        }
194
195
        if ($this->module) {
196
            if (\Inji\Controller::$cur) {
197
                $paths['customModuleTemplateControllerContentController'] = $this->path . "/modules/" . $this->module->name . "/" . \Inji\Controller::$cur->name . "/{$content}.php";
198
            }
199
            $paths['customModuleTemplateControllerContent'] = $this->path . "/modules/" . $this->module->name . "/{$content}.php";
200
        }
201
        if ($this->module && \Inji\Controller::$cur) {
202
            $paths['customModuleControllerContentController'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/" . \Inji\Controller::$cur->name . "/{$content}.php";
203
            $paths['customModuleControllerContent'] = $this->module->path . '/' . \Inji\Controller::$cur->module->app->type . "Controllers/content/{$content}.php";
204
        }
205
        return $paths;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $paths returns the type string[]|array which is incompatible with the documented return type string.
Loading history...
206
    }
207
208
    /**
209
     * Retrn object of template by template name
210
     *
211
     * @param string $templateName
212
     * @param \Inji\App $app
213
     * @return \Inji\View\Template
214
     */
215
    public static function get($templateName, $app = null, $templatesPath = '') {
216
        if (!$app) {
217
            $app = \Inji\App::$cur;
218
        }
219
        if (!$templatesPath) {
220
            $templatesPath = $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

220
            /** @scrutinizer ignore-call */ 
221
            $templatesPath = $app->view->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 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

220
            /** @scrutinizer ignore-call */ 
221
            $templatesPath = $app->view->templatesPath();
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...
221
        }
222
        return new Template([
223
            'name' => $templateName,
224
            'path' => $templatesPath . '/' . $templateName,
225
            'app' => $app
226
        ]);
227
    }
228
229
}
230