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 ( c097a6...9587da )
by Steeven
02:27
created

Module::loadModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Containers\Modules\DataStructures;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Spl\DataStructures\SplArrayObject;
19
use O2System\Spl\Info\SplDirectoryInfo;
20
21
/**
22
 * Class Module
23
 *
24
 * @package O2System\Framework\Containers\Modules\DataStructures
25
 */
26
class Module extends SplDirectoryInfo
27
{
28
    /**
29
     * Module::$type
30
     *
31
     * @var string
32
     */
33
    protected $type = 'MODULE';
34
35
    /**
36
     * Module::$namespace
37
     *
38
     * @var string
39
     */
40
    protected $namespace;
41
42
    /**
43
     * Module::$segments
44
     *
45
     * @var string
46
     */
47
    protected $segments;
48
49
    /**
50
     * Module::$parentSegments
51
     *
52
     * @var string
53
     */
54
    protected $parentSegments;
55
56
    /**
57
     * Module::$properties
58
     *
59
     * @var array
60
     */
61
    protected $properties = [];
62
63
    /**
64
     * Module::$presets
65
     *
66
     * @var array
67
     */
68
    protected $presets = [];
69
70
    // ------------------------------------------------------------------------
71
72
    /**
73
     * Module::__construct
74
     *
75
     * @param string $dir
76
     */
77
    public function __construct($dir)
78
    {
79
        parent::__construct($dir);
80
        $this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false);
0 ignored issues
show
Bug introduced by
The constant O2System\Framework\Conta...ataStructures\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
    }
82
83
    // ------------------------------------------------------------------------
84
85
    /**
86
     * Module::getSegments
87
     *
88
     * @param bool $returnArray
89
     *
90
     * @return array|string
91
     */
92
    public function getSegments($returnArray = true)
93
    {
94
        if ($returnArray) {
95
            return explode('/', $this->segments);
96
        }
97
98
        return $this->segments;
99
    }
100
101
    // ------------------------------------------------------------------------
102
103
    /**
104
     * Module::setSegments
105
     *
106
     * @param array|string $segments
107
     *
108
     * @return static
109
     */
110
    public function setSegments($segments)
111
    {
112
        $this->segments = is_array($segments) ? implode('/', $segments) : $segments;
113
114
        return $this;
115
    }
116
117
    // ------------------------------------------------------------------------
118
119
    /**
120
     * Module::getParentSegments
121
     *
122
     * @param bool $returnArray
123
     *
124
     * @return array|string
125
     */
126
    public function getParentSegments($returnArray = true)
127
    {
128
        if ($returnArray) {
129
            return explode('/', $this->parentSegments);
130
        }
131
132
        return $this->parentSegments;
133
    }
134
135
    // ------------------------------------------------------------------------
136
137
    /**
138
     * Module::setParentSegments
139
     *
140
     * @param array|string $parentSegments
141
     *
142
     * @return static
143
     */
144
    public function setParentSegments($parentSegments)
145
    {
146
        $this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments;
147
148
        return $this;
149
    }
150
151
    // ------------------------------------------------------------------------
152
153
    /**
154
     * Module::getCode
155
     *
156
     * @return string
157
     */
158
    public function getCode()
159
    {
160
        return strtoupper(substr(md5(spl_object_hash($this)), 2, 7));
161
    }
162
163
    // ------------------------------------------------------------------------
164
165
    /**
166
     * Module::getChecksum
167
     *
168
     * @return string
169
     */
170
    public function getChecksum()
171
    {
172
        return md5($this->getMTime());
173
    }
174
175
    // ------------------------------------------------------------------------
176
177
    /**
178
     * Module::getProperties
179
     *
180
     * @return \O2System\Spl\DataStructures\SplArrayObject
181
     */
182
    public function getProperties()
183
    {
184
        return new SplArrayObject($this->properties);
185
    }
186
187
    // ------------------------------------------------------------------------
188
189
    /**
190
     * Module::setProperties
191
     *
192
     * @param array $properties
193
     *
194
     * @return static
195
     */
196
    public function setProperties(array $properties)
197
    {
198
        if (isset($properties[ 'presets' ])) {
199
            $this->setPresets($properties[ 'presets' ]);
200
201
            unset($properties[ 'presets' ]);
202
        }
203
204
        $this->properties = $properties;
205
206
        return $this;
207
    }
208
209
    // ------------------------------------------------------------------------
210
211
    /**
212
     * Module::getPresets
213
     *
214
     * @return \O2System\Spl\DataStructures\SplArrayObject
215
     */
216
    public function getPresets()
217
    {
218
        return new SplArrayObject($this->presets);
219
    }
220
221
    // ------------------------------------------------------------------------
222
223
    /**
224
     * Module::setPresets
225
     *
226
     * @param array $presets
227
     *
228
     * @return static
229
     */
230
    public function setPresets(array $presets)
231
    {
232
        $this->presets = $presets;
233
234
        return $this;
235
    }
236
237
    // ------------------------------------------------------------------------
238
239
    /**
240
     * Module::getDefaultControllerClassName
241
     *
242
     * @return string
243
     */
244
    public function getDefaultControllerClassName()
245
    {
246
        return $this->getNamespace() . 'Controllers\\' . $this->getDirName();
247
    }
248
249
    // ------------------------------------------------------------------------
250
251
    /**
252
     * Module::getNamespace
253
     *
254
     * @return string
255
     */
256
    public function getNamespace()
257
    {
258
        return $this->namespace;
259
    }
260
261
    // ------------------------------------------------------------------------
262
263
    /**
264
     * Module::setNamespace
265
     *
266
     * @param string $namespace
267
     *
268
     * @return static
269
     */
270
    public function setNamespace($namespace)
271
    {
272
        $this->namespace = trim($namespace, '\\') . '\\';
273
274
        return $this;
275
    }
276
277
    // ------------------------------------------------------------------------
278
279
    /**
280
     * Module::getThemes
281
     *
282
     * @return array
283
     */
284
    public function getThemes()
285
    {
286
        $directory = new SplDirectoryInfo($this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR);
287
288
        $themes = [];
289
        foreach ($directory->getTree() as $themeName => $themeTree) {
290
            if (($theme = $this->getTheme($themeName)) instanceof Module\Theme) {
291
                $themes[ $themeName ] = $theme;
292
            }
293
        }
294
295
        return $themes;
296
    }
297
298
    // ------------------------------------------------------------------------
299
300
    /**
301
     * Module::getResourcesDir
302
     *
303
     * @param string|null $subDir
304
     *
305
     * @return bool|string
306
     */
307
    public function getResourcesDir($subDir = null)
308
    {
309
        $dirResources = PATH_RESOURCES;
310
311
        $dirPath = str_replace(PATH_APP, '', $this->getRealPath());
312
        $dirPathParts = explode(DIRECTORY_SEPARATOR, $dirPath);
313
314
        if (count($dirPathParts)) {
315
            $dirPathParts = array_map('dash', $dirPathParts);
316
            $dirResources .= implode(DIRECTORY_SEPARATOR, $dirPathParts);
317
        }
318
319
        if (is_null($subDir)) {
320
            return $dirResources;
321
        } elseif (is_dir($dirResources . $subDir . DIRECTORY_SEPARATOR)) {
322
            return $dirResources . $subDir . DIRECTORY_SEPARATOR;
323
        }
324
325
        return false;
326
    }
327
328
    // ------------------------------------------------------------------------
329
330
    /**
331
     * Module::getTheme
332
     *
333
     * @param string $theme
334
     * @param bool   $failover
335
     *
336
     * @return bool|Module\Theme
337
     */
338
    public function getTheme($theme, $failover = true)
339
    {
340
        $theme = dash($theme);
341
342
        if ($failover === false) {
343
            if (is_dir($themePath = $this->getResourcesDir('themes') . $theme . DIRECTORY_SEPARATOR)) {
0 ignored issues
show
Bug introduced by
Are you sure $this->getResourcesDir('themes') of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

343
            if (is_dir($themePath = /** @scrutinizer ignore-type */ $this->getResourcesDir('themes') . $theme . DIRECTORY_SEPARATOR)) {
Loading history...
344
                $themeObject = new Module\Theme($themePath);
345
346
                if ($themeObject->isValid()) {
347
                    return $themeObject;
348
                }
349
            }
350
        } else {
351
            foreach (modules() as $module) {
352
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
353
                    continue;
354
                } elseif ($themeObject = $module->getTheme($theme, false)) {
355
                    return $themeObject;
356
                }
357
            }
358
        }
359
360
        return false;
361
    }
362
363
    // ------------------------------------------------------------------------
364
365
    /**
366
     * Module::getPublicDir
367
     *
368
     * @return string
369
     */
370
    public function getPublicDir()
371
    {
372
        return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath()));
373
    }
374
375
    // ------------------------------------------------------------------------
376
377
    /**
378
     * Module::getType
379
     *
380
     * @return string
381
     */
382
    public function getType()
383
    {
384
        return $this->type;
385
    }
386
387
    // ------------------------------------------------------------------------
388
389
    /**
390
     * Module::setType
391
     *
392
     * @param string $type
393
     *
394
     * @return static
395
     */
396
    public function setType($type)
397
    {
398
        $this->type = strtoupper($type);
399
400
        return $this;
401
    }
402
403
    // ------------------------------------------------------------------------
404
405
    /**
406
     * Module::getDir
407
     *
408
     * @param string $subDir
409
     * @param bool   $psrDir
410
     *
411
     * @return bool|string
412
     */
413
    public function getDir($subDir, $psrDir = false)
414
    {
415
        $subDir = $psrDir === true ? prepare_class_name($subDir) : $subDir;
416
        $subDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $subDir);
417
418
        if (is_dir($dirPath = $this->getRealPath() . $subDir)) {
419
            return $dirPath . DIRECTORY_SEPARATOR;
420
        }
421
422
        return false;
423
    }
424
425
    // ------------------------------------------------------------------------
426
427
    /**
428
     * Module::hasTheme
429
     *
430
     * @param string $theme
431
     *
432
     * @return bool
433
     */
434
    public function hasTheme($theme)
435
    {
436
        if (is_dir($this->getThemesDir() . $theme)) {
437
            return true;
438
        } else {
439
            foreach (modules() as $module) {
440
                if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
441
                    continue;
442
                } elseif (is_dir($module->getResourcesDir('themes') . $theme)) {
443
                    return true;
444
                }
445
            }
446
        }
447
448
        return false;
449
    }
450
451
    // ------------------------------------------------------------------------
452
453
    /**
454
     * Module::getThemesDir
455
     *
456
     * @return string
457
     */
458
    public function getThemesDir()
459
    {
460
        return $this->getResourcesDir('themes');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getResourcesDir('themes') could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
461
    }
462
463
    // ------------------------------------------------------------------------
464
465
    /**
466
     * Module::getParameter
467
     *
468
     * @return string
469
     */
470
    public function getParameter()
471
    {
472
        return snakecase($this->getDirName());
473
    }
474
475
    // ------------------------------------------------------------------------
476
477
    /**
478
     * Module::loadModel
479
     */
480
    public function loadModel()
481
    {
482
        $modelClassName = $this->namespace . 'Models\Base';
483
484
        if (class_exists($modelClassName)) {
485
            models()->load($modelClassName, strtolower($this->type));
0 ignored issues
show
Bug introduced by
The method load() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

485
            models()->/** @scrutinizer ignore-call */ load($modelClassName, strtolower($this->type));
Loading history...
Bug introduced by
The method load() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

485
            models()->/** @scrutinizer ignore-call */ load($modelClassName, strtolower($this->type));

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...
486
        }
487
    }
488
489
    public function getControllers()
490
    {
491
        $controllers = [];
492
493
        if (is_dir($directory = $this->getDir('Controllers'))) {
0 ignored issues
show
Bug introduced by
It seems like $directory = $this->getDir('Controllers') can also be of type false; however, parameter $filename of is_dir() does only seem to accept string, 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

493
        if (is_dir(/** @scrutinizer ignore-type */ $directory = $this->getDir('Controllers'))) {
Loading history...
494
            $namespace = $this->getNamespace() . 'Controllers\\';
495
496
            $iterator = new \RecursiveIteratorIterator(
497
                new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS)
498
            );
499
500
            foreach ($iterator as $file) {
501
                if (pathinfo($file, PATHINFO_EXTENSION) == "php") {
502
                    $controllerClassName = str_replace([
0 ignored issues
show
Bug introduced by
array($directory, '.php'...es\DIRECTORY_SEPARATOR) of type array<integer,false|string> is incompatible with the type string|string[] expected by parameter $search of str_replace(). ( Ignorable by Annotation )

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

502
                    $controllerClassName = str_replace(/** @scrutinizer ignore-type */ [
Loading history...
503
                        $directory,
504
                        '.php',
505
                        '/',
506
                        DIRECTORY_SEPARATOR,
507
                    ], [
508
                        '',
509
                        '',
510
                        '\\',
511
                        '\\',
512
                    ], $file->getRealPath());
513
514
                    $controller = new Module\Controller($namespace . $controllerClassName);
515
516
                    if ( ! empty($controller->name) and ! in_array($controller->getParameter(),
517
                            ['login', 'pages', 'setup', 'license'])) {
518
                        $controllers[ $controller->getParameter() ] = $controller;
519
                    }
520
                }
521
            }
522
        }
523
524
        return $controllers;
525
    }
526
}