GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( cf9260...b52382 )
by
unknown
03:19
created

Presenter::setTheme()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 20
nc 20
nop 1
dl 0
loc 41
rs 7.6666
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
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Http;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Framework\Containers\Modules\DataStructures\Module\Theme;
19
use O2System\Psr\Patterns\Structural\Repository\AbstractRepository;
20
use O2System\Spl\DataStructures\SplArrayObject;
21
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait;
22
23
/**
24
 * Class Presenter
25
 *
26
 * @package O2System\Framework\Http
27
 */
28
class Presenter extends AbstractRepository
29
{
30
    use ConfigCollectorTrait;
31
32
    /**
33
     * Presenter::$meta
34
     *
35
     * @var Presenter\Meta
36
     */
37
    public $meta;
38
39
    /**
40
     * Presenter::$page
41
     *
42
     * @var Presenter\Page
43
     */
44
    public $page;
45
46
    /**
47
     * Presenter::$assets
48
     *
49
     * @var Presenter\Assets
50
     */
51
    public $assets;
52
53
    /**
54
     * Presenter::$partials
55
     *
56
     * @var Presenter\Repositories\Partials
57
     */
58
    public $partials;
59
60
    /**
61
     * Presenter::$widgets
62
     *
63
     * @var Presenter\Repositories\Widgets
64
     */
65
    public $widgets;
66
67
    /**
68
     * Presenter::$theme
69
     *
70
     * @var bool|Theme
71
     */
72
    public $theme = false;
73
74
    // ------------------------------------------------------------------------
75
76
    /**
77
     * Presenter::__construct
78
     */
79
    public function __construct()
80
    {
81
        loader()->helper('Url');
82
83
        $this->meta = new Presenter\Meta;
84
        $this->page = new Presenter\Page;
85
        $this->assets = new Presenter\Assets;
86
        $this->partials = new Presenter\Repositories\Partials();
87
        $this->widgets = new Presenter\Repositories\Widgets();
88
    }
89
90
    // ------------------------------------------------------------------------
91
92
    /**
93
     * Presenter::initialize
94
     *
95
     * @param array $config
96
     *
97
     * @return static
98
     */
99
    public function initialize(array $config = [])
100
    {
101
        if (count($config)) {
102
            $this->setConfig($config);
103
        } elseif (false !== ($config = config('view')->presenter)) {
0 ignored issues
show
Bug Best Practice introduced by
The property presenter does not exist on O2System\Framework\Containers\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
            $this->setConfig($config);
105
        }
106
107
        // autoload presenter assets
108
        if (isset($config[ 'assets' ])) {
109
            $this->assets->autoload($config[ 'assets' ]);
110
        }
111
112
        // autoload presenter theme
113
        if(isset($config['theme'])) {
114
            $this->setTheme($config['theme']);
115
        }
116
117
        return $this;
118
    }
119
120
    // ------------------------------------------------------------------------
121
122
    /**
123
     * Presenter::setTheme
124
     *
125
     * @param string $theme
126
     *
127
     * @return static
128
     */
129
    public function setTheme($theme)
130
    {
131
        if (is_bool($theme)) {
0 ignored issues
show
introduced by
The condition is_bool($theme) is always false.
Loading history...
132
            $this->theme = false;
133
        } elseif(($moduleTheme = modules()->top()->getTheme($theme, true)) instanceof Theme) {
0 ignored issues
show
Bug introduced by
The method top() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

133
        } elseif(($moduleTheme = modules()->/** @scrutinizer ignore-call */ top()->getTheme($theme, true)) instanceof Theme) {

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...
134
            $this->theme = $moduleTheme;
135
        } elseif(($appTheme = modules()->first()->getTheme($theme, true)) instanceof Theme) {
0 ignored issues
show
Bug introduced by
The method first() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

135
        } elseif(($appTheme = modules()->/** @scrutinizer ignore-call */ first()->getTheme($theme, true)) instanceof Theme) {

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...
136
            $this->theme = $appTheme;
137
        }
138
139
        if($this->theme) {
140
            if ( ! defined('PATH_THEME')) {
141
                define('PATH_THEME', $this->theme->getRealPath());
142
            }
143
144
            // add theme assets directory
145
            $this->assets->pushFilePath($this->theme->getRealPath());
0 ignored issues
show
Bug introduced by
The method pushFilePath() does not exist on O2System\Framework\Http\Presenter\Assets. ( Ignorable by Annotation )

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

145
            $this->assets->/** @scrutinizer ignore-call */ 
146
                           pushFilePath($this->theme->getRealPath());

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...
146
147
            if(is_dir($themeViewPath = $this->theme->getRealPath() . 'views' . DIRECTORY_SEPARATOR)) {
148
149
                // add theme view directory
150
                view()->addFilePath($this->theme->getRealPath());
151
152
                // add theme output directory
153
                output()->pushFilePath($themeViewPath);
0 ignored issues
show
Bug introduced by
The method pushFilePath() does not exist on O2System\Kernel\Http\Output. ( Ignorable by Annotation )

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

153
                output()->/** @scrutinizer ignore-call */ pushFilePath($themeViewPath);

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 pushFilePath() does not exist on O2System\Kernel\Cli\Output. ( Ignorable by Annotation )

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

153
                output()->/** @scrutinizer ignore-call */ pushFilePath($themeViewPath);

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...
154
155
                $modules = modules()->getArrayCopy();
0 ignored issues
show
Bug introduced by
The method getArrayCopy() does not exist on O2System\Framework\Conta...s\DataStructures\Module. ( Ignorable by Annotation )

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

155
                $modules = modules()->/** @scrutinizer ignore-call */ getArrayCopy();

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...
156
157
                foreach($modules as $module) {
158
                    if ( ! in_array($module->getType(), ['KERNEL', 'FRAMEWORK', 'APP'])) {
159
                        $moduleResourcesPath = str_replace(PATH_RESOURCES, '', $module->getResourcesDir());
160
161
                        if(is_dir($themeViewPath . $moduleResourcesPath)) {
162
                            view()->pushFilePath($themeViewPath . $moduleResourcesPath);
0 ignored issues
show
Bug introduced by
The method pushFilePath() does not exist on O2System\Framework\Http\View. ( Ignorable by Annotation )

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

162
                            view()->/** @scrutinizer ignore-call */ pushFilePath($themeViewPath . $moduleResourcesPath);

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...
163
                        }
164
                    }
165
                }
166
            }
167
        }
168
        
169
        return $this;
170
    }
171
172
    // ------------------------------------------------------------------------
173
174
    /**
175
     * Presenter::store
176
     *
177
     * @param string $offset
178
     * @param mixed  $value
179
     * @param bool   $replace
180
     */
181
    public function store($offset, $value, $replace = false)
182
    {
183
        if ($value instanceof \Closure) {
184
            parent::store($offset, call_user_func($value, $this));
185
        } else {
186
            parent::store($offset, $value);
187
        }
188
    }
189
190
    // ------------------------------------------------------------------------
191
192
    /**
193
     * Presenter::getArrayCopy
194
     *
195
     * @return array
196
     */
197
    public function getArrayCopy()
198
    {
199
        $storage = $this->storage;
200
201
        // Add Properties
202
        $storage[ 'meta' ] = $this->meta;
203
        $storage[ 'page' ] = $this->page;
204
        $storage[ 'assets' ] = new SplArrayObject([
205
            'head' => $this->assets->getHead(),
206
            'body' => $this->assets->getBody(),
207
        ]);
208
        $storage[ 'partials' ] = $this->partials;
209
        $storage[ 'widgets' ] = $this->widgets;
210
        $storage[ 'theme' ] = $this->theme;
211
212
        // Add Services
213
        $storage[ 'config' ] = config();
214
        $storage[ 'language' ] = language();
215
        $storage[ 'session' ] = session();
216
        $storage[ 'presenter' ] = presenter();
217
        $storage[ 'input' ] = input();
218
219
        if (services()->has('csrfProtection')) {
220
            $storage[ 'csrfToken' ] = services()->get('csrfProtection')->getToken();
221
        }
222
223
        return $storage;
224
    }
225
226
    // ------------------------------------------------------------------------
227
228
    /**
229
     * Presenter::get
230
     *
231
     * @param string $property
232
     *
233
     * @return mixed
234
     */
235
    public function get($property)
236
    {
237
        // CodeIgniter property aliasing
238
        if ($property === 'load') {
239
            $property = 'loader';
240
        }
241
242
        if (services()->has($property)) {
243
            return services()->get($property);
244
        } elseif ($property === 'model') {
245
            return models('controller');
246
        } elseif ($property === 'services' || $property === 'libraries') {
247
            return services();
248
        } elseif (method_exists($this, $property)) {
249
            return call_user_func([&$this, $property]);
250
        }
251
252
        return parent::get($property);
253
    }
254
255
    // ------------------------------------------------------------------------
256
257
    /**
258
     * Presenter::__call
259
     *
260
     * @param string $method
261
     * @param array  $args
262
     *
263
     * @return mixed
264
     */
265
    public function __call($method, array $args = [])
266
    {
267
        if (method_exists($this, $method)) {
268
            return call_user_func_array([$this, $method], $args);
269
        }
270
    }
271
}
272