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 ( 776629...deac27 )
by
unknown
03:03
created

Assets::loadPackage()   D

Complexity

Conditions 19
Paths 21

Size

Total Lines 81
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 53
nc 21
nop 2
dl 0
loc 81
rs 4.5166
c 0
b 0
f 0

How to fix   Long Method    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 PHP 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\Presenter;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class Assets
20
 *
21
 * @package O2System\Framework\Http\Presenter
22
 */
23
class Assets
24
{
25
    /**
26
     * Assets::$head
27
     * 
28
     * @var \O2System\Framework\Http\Presenter\Assets\Positions\Head 
29
     */
30
    protected $head;
31
32
    /**
33
     * Assets::$body
34
     * 
35
     * @var \O2System\Framework\Http\Presenter\Assets\Positions\Body 
36
     */
37
    protected $body;
38
39
    // ------------------------------------------------------------------------
40
41
    /**
42
     * Assets::__construct
43
     */
44
    public function __construct()
45
    {
46
        loader()->addPublicDir('assets', 'assets');
47
        
48
        $this->head = new Assets\Positions\Head();
49
        $this->body = new Assets\Positions\Body();
50
    }
51
52
    // ------------------------------------------------------------------------
53
54
    /**
55
     * Assets::__get
56
     *
57
     * @param string $property
58
     *
59
     * @return \O2System\Framework\Http\Presenter\Assets\Positions\Body|\O2System\Framework\Http\Presenter\Assets\Positions\Head|null
60
     */
61
    public function __get($property)
62
    {
63
        return isset($this->{$property}) ? $this->{$property} : null;
64
    }
65
66
    // ------------------------------------------------------------------------
67
68
    /**
69
     * Assets::autoload
70
     *
71
     * @param array $assets
72
     *
73
     * @return void
74
     */
75
    public function autoload($assets)
76
    {
77
        foreach ($assets as $position => $collections) {
78
            if (property_exists($this, $position)) {
79
80
                if ($collections instanceof \ArrayObject) {
81
                    $collections = $collections->getArrayCopy();
82
                }
83
84
                $this->{$position}->loadCollections($collections);
85
            } elseif ($position === 'packages') {
86
                $this->loadPackages($collections);
87
            } elseif ($position === 'css') {
88
                $this->loadCss($collections);
89
            } elseif ($position === 'js') {
90
                $this->loadJs($collections);
91
            }
92
        }
93
    }
94
95
    // ------------------------------------------------------------------------
96
97
    /**
98
     * Assets::loadPackages
99
     *
100
     * @param array $packages
101
     *
102
     * @return void
103
     */
104
    public function loadPackages($packages)
105
    {
106
        foreach ($packages as $package => $files) {
107
            if (is_string($files)) {
108
                $this->loadPackage($files);
109
            } elseif (is_array($files)) {
110
                $this->loadPackage($package, $files);
111
            } elseif (is_object($files)) {
112
                $this->loadPackage($package, get_object_vars($files));
113
            }
114
        }
115
    }
116
117
    // ------------------------------------------------------------------------
118
119
    /**
120
     * Assets::loadPackage
121
     *
122
     * @param string $package
123
     * @param array  $subPackages
124
     *
125
     * @return void
126
     */
127
    public function loadPackage($package, $subPackages = [])
128
    {
129
        $packageDir = implode(DIRECTORY_SEPARATOR, [
130
                'packages',
131
                $package,
132
            ]) . DIRECTORY_SEPARATOR;
133
134
        if (count($subPackages)) {
135
136
            if (array_key_exists('libraries', $subPackages)) {
137
                foreach ($subPackages[ 'libraries' ] as $subPackageFile) {
138
                    $pluginDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR;
139
                    $pluginName = $subPackageFile;
140
141
                    if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) {
142
                        $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css');
143
                    } else {
144
                        $this->body->loadFile($pluginDir . $pluginName . '.js');
145
                    }
146
                }
147
148
                unset($subPackages[ 'libraries' ]);
149
            }
150
151
            $this->head->loadFile($packageDir . $package . '.css');
152
            $this->body->loadFile($packageDir . $package . '.js');
153
154
            foreach ($subPackages as $subPackage => $subPackageFiles) {
155
                if ($subPackage === 'theme' or $subPackage === 'themes') {
156
                    if (is_string($subPackageFiles)) {
157
                        $subPackageFiles = [$subPackageFiles];
158
                    }
159
160
                    foreach ($subPackageFiles as $themeName) {
161
                        $themeDir = $packageDir . 'themes' . DIRECTORY_SEPARATOR;
162
163
                        if ($this->head->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.css')) {
164
                            $this->body->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.js');
165
                        } else {
166
                            $this->head->loadFile($themeDir . $themeName . '.css');
167
                        }
168
                    }
169
                } elseif ($subPackage === 'plugins') {
170
                    foreach ($subPackageFiles as $subPackageFile) {
171
                        $pluginDir = $packageDir . 'plugins' . DIRECTORY_SEPARATOR;
172
                        $pluginName = $subPackageFile;
173
174
                        $pluginName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $pluginName);
175
                        if (strpos($pluginName, DIRECTORY_SEPARATOR) !== false) {
176
                            $pluginDir .= pathinfo($pluginName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR;
177
                            $pluginName = pathinfo($pluginName, PATHINFO_BASENAME);
178
                        }
179
180
                        if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) {
181
                            $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css');
182
                        } else {
183
                            $this->body->loadFile($pluginDir . $pluginName . '.js');
184
                        }
185
                    }
186
                } elseif ($subPackage === 'libraries') {
187
                    foreach ($subPackageFiles as $subPackageFile) {
188
                        $libraryDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR;
189
                        $libraryName = $subPackageFile;
190
191
                        $libraryName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $libraryName);
192
                        if (strpos($libraryName, DIRECTORY_SEPARATOR) !== false) {
193
                            $libraryDir .= pathinfo($libraryName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR;
194
                            $libraryName = pathinfo($libraryName, PATHINFO_BASENAME);
195
                        }
196
197
                        if ($this->body->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.js')) {
198
                            $this->head->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.css');
199
                        } else {
200
                            $this->body->loadFile($libraryDir . $libraryName . '.js');
201
                        }
202
                    }
203
                }
204
            }
205
        } else {
206
            $this->head->loadFile($packageDir . $package . '.css');
207
            $this->body->loadFile($packageDir . $package . '.js');
208
        }
209
    }
210
211
    // ------------------------------------------------------------------------
212
213
    /**
214
     * Assets::loadCss
215
     *
216
     * @param array $files
217
     *
218
     * @return void
219
     */
220
    public function loadCss($files)
221
    {
222
        $files = is_string($files) ? [$files] : $files;
0 ignored issues
show
introduced by
The condition is_string($files) is always false.
Loading history...
223
        $this->head->loadCollections(['css' => $files]);
224
    }
225
226
    // ------------------------------------------------------------------------
227
228
    /**
229
     * Assets::loadJs
230
     *
231
     * @param string|array  $files
232
     * @param string        $position
233
     *
234
     * @return void
235
     */
236
    public function loadJs($files, $position = 'body')
237
    {
238
        $files = is_string($files) ? [$files] : $files;
239
        $this->{$position}->loadCollections(['js' => $files]);
240
    }
241
242
    // ------------------------------------------------------------------------
243
244
    /**
245
     * Assets::loadFiles
246
     *
247
     * @param array $assets
248
     *
249
     * @return void
250
     */
251
    public function loadFiles($assets)
252
    {
253
        foreach ($assets as $type => $item) {
254
            $addMethod = 'load' . ucfirst($type);
255
256
            if (method_exists($this, $addMethod)) {
257
                call_user_func_array([&$this, $addMethod], [$item]);
258
            }
259
        }
260
    }
261
262
    // ------------------------------------------------------------------------
263
264
    /**
265
     * Assets::theme
266
     *
267
     * @param string $path
268
     *
269
     * @return string
270
     */
271
    public function theme($path)
272
    {
273
        $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
274
275
        if (is_file($filePath = PATH_THEME . $path)) {
276
            return path_to_url($filePath);
277
        }
278
    }
279
280
    // ------------------------------------------------------------------------
281
282
    /**
283
     * Assets::file
284
     *
285
     * @param string $file
286
     *
287
     * @return string
288
     */
289
    public function file($file)
290
    {
291
        $filePaths = loader()->getPublicDirs(true);
292
        
293
        foreach ($filePaths as $filePath) {
294
            if (is_file($filePath . $file)) {
295
                return path_to_url($filePath . $file);
296
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
297
            }
298
        }
299
    }
300
301
    // ------------------------------------------------------------------------
302
303
    /**
304
     * Assets::image
305
     *
306
     * @param string $image
307
     *
308
     * @return string
309
     */
310
    public function image($image)
311
    {
312
        $filePaths = loader()->getPublicDirs(true);
313
        $image = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $image);
314
315
        foreach ($filePaths as $filePath) {
316
            $filePath .= 'img' . DIRECTORY_SEPARATOR;
317
318
            if (is_file($filePath . $image)) {
319
                return path_to_url($filePath . $image);
320
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
321
            }
322
323
            unset($filePath);
324
        }
325
    }
326
327
    // ------------------------------------------------------------------------
328
329
    /**
330
     * Assets::media
331
     *
332
     * @param string $media
333
     *
334
     * @return string
335
     */
336
    public function media($media)
337
    {
338
        $filePaths = loader()->getPublicDirs(true);
339
        $media = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $media);
340
341
        foreach ($filePaths as $filePath) {
342
            $filePath .= 'media' . DIRECTORY_SEPARATOR;
343
344
            if (is_file($filePath . $media)) {
345
                return path_to_url($filePath . $media);
346
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
347
            }
348
349
            unset($filePath);
350
        }
351
    }
352
353
    // ------------------------------------------------------------------------
354
355
    /**
356
     * Assets::parseSourceCode
357
     *
358
     * @param string $sourceCode
359
     *
360
     * @return string
361
     */
362
    public function parseSourceCode($sourceCode)
363
    {
364
        $sourceCode = str_replace(
365
            [
366
                '"../assets/',
367
                "'../assets/",
368
                "(../assets/",
369
            ],
370
            [
371
                '"' . base_url() . '/assets/',
372
                "'" . base_url() . '/assets/',
373
                "(" . base_url() . '/assets/',
374
            ],
375
            $sourceCode);
376
377
        if (presenter()->theme->use === true) {
0 ignored issues
show
Bug Best Practice introduced by
The property theme does not exist on O2System\Framework\Http\Presenter. Since you implemented __get, consider adding a @property annotation.
Loading history...
378
            $sourceCode = str_replace(
379
                [
380
                    '"assets/',
381
                    "'assets/",
382
                    "(assets/",
383
384
                    // with dot
385
                    '"./assets/',
386
                    "'./assets/",
387
                    "(./assets/",
388
                ],
389
                [
390
                    '"' . presenter()->theme->active->getUrl('assets/'),
391
                    "'" . presenter()->theme->active->getUrl('assets/'),
392
                    "(" . presenter()->theme->active->getUrl('assets/'),
393
394
                    // with dot
395
                    '"' . presenter()->theme->active->getUrl('assets/'),
396
                    "'" . presenter()->theme->active->getUrl('assets/'),
397
                    "(" . presenter()->theme->active->getUrl('assets/'),
398
                ],
399
                $sourceCode);
400
        }
401
402
        // Valet path fixes
403
        if (isset($_SERVER[ 'SCRIPT_FILENAME' ])) {
404
            $valetPath = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR;
405
        } else {
406
            $PATH_ROOT = $_SERVER[ 'DOCUMENT_ROOT' ];
407
408
            if (isset($_SERVER[ 'PHP_SELF' ])) {
409
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'PHP_SELF' ]) . DIRECTORY_SEPARATOR;
410
            } elseif (isset($_SERVER[ 'DOCUMENT_URI' ])) {
411
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'DOCUMENT_URI' ]) . DIRECTORY_SEPARATOR;
412
            } elseif (isset($_SERVER[ 'REQUEST_URI' ])) {
413
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'REQUEST_URI' ]) . DIRECTORY_SEPARATOR;
414
            } elseif (isset($_SERVER[ 'SCRIPT_NAME' ])) {
415
                $valetPath = $PATH_ROOT . dirname($_SERVER[ 'SCRIPT_NAME' ]) . DIRECTORY_SEPARATOR;
416
            }
417
        }
418
419
        if (isset($valetPath)) {
420
            $sourceCode = str_replace($valetPath, '/', $sourceCode);
421
        }
422
423
        return $sourceCode;
424
    }
425
}
426