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 ( 86dce3...98cf71 )
by Nur
02:40
created

Reactor::httpHandler()   F

Complexity

Conditions 26
Paths 7688

Size

Total Lines 101
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 59
nc 7688
nop 0
dl 0
loc 101
rs 0
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;
15
16
// ------------------------------------------------------------------------
17
18
/*
19
 * ---------------------------------------------------------------
20
 * ERROR REPORTING
21
 * ---------------------------------------------------------------
22
 *
23
 * Different environments will require different levels of error reporting.
24
 * By default development will show errors but testing and live will hide them.
25
 */
26
switch (strtoupper(ENVIRONMENT)) {
0 ignored issues
show
Bug introduced by
The constant O2System\ENVIRONMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
    case 'DEVELOPMENT':
28
        error_reporting(-1);
29
        ini_set('display_errors', 1);
30
        break;
31
    case 'TESTING':
32
    case 'PRODUCTION':
33
        ini_set('display_errors', 0);
34
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
35
        break;
36
    default:
37
        header('HTTP/1.1 503 Service Unavailable.', true, 503);
38
        echo 'The application environment is not set correctly.';
39
        exit(1); // EXIT_ERROR
40
}
41
42
/*
43
 *---------------------------------------------------------------
44
 * VENDOR PATH
45
 *---------------------------------------------------------------
46
 *
47
 * RealPath to vendor folder.
48
 *
49
 * WITH TRAILING SLASH!
50
 */
51
if ( ! defined('PATH_VENDOR')) {
52
    define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\PATH_ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
}
54
55
/*
56
 *---------------------------------------------------------------
57
 * FRAMEWORK PATH
58
 *---------------------------------------------------------------
59
 *
60
 * RealPath to framework folder.
61
 *
62
 * WITH TRAILING SLASH!
63
 */
64
if ( ! defined('PATH_FRAMEWORK')) {
65
    define('PATH_FRAMEWORK', __DIR__ . DIRECTORY_SEPARATOR);
66
}
67
68
/*
69
 *---------------------------------------------------------------
70
 * APP PATH
71
 *---------------------------------------------------------------
72
 *
73
 * RealPath to application folder.
74
 *
75
 * WITH TRAILING SLASH!
76
 */
77
if ( ! defined('PATH_APP')) {
78
    define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_APP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
}
80
81
/*
82
 *---------------------------------------------------------------
83
 * PUBLIC PATH
84
 *---------------------------------------------------------------
85
 *
86
 * RealPath to public folder.
87
 *
88
 * WITH TRAILING SLASH!
89
 */
90
if ( ! defined('PATH_PUBLIC')) {
91
    define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_PUBLIC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
}
93
94
/*
95
 *---------------------------------------------------------------
96
 * CACHE PATH
97
 *---------------------------------------------------------------
98
 *
99
 * RealPath to writable caching folder.
100
 *
101
 * WITH TRAILING SLASH!
102
 */
103
if ( ! defined('PATH_CACHE')) {
104
    define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_CACHE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
}
106
107
/*
108
 *---------------------------------------------------------------
109
 * STORAGE PATH
110
 *---------------------------------------------------------------
111
 *
112
 * RealPath to writable storage folder.
113
 *
114
 * WITH TRAILING SLASH!
115
 */
116
if ( ! defined('PATH_STORAGE')) {
117
    define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_STORAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
118
}
119
120
/*
121
 *---------------------------------------------------------------
122
 * RESOURCES PATH
123
 *---------------------------------------------------------------
124
 *
125
 * RealPath to writable resources folder.
126
 *
127
 * WITH TRAILING SLASH!
128
 */
129
if ( ! defined('PATH_RESOURCES')) {
130
    define('PATH_RESOURCES', PATH_ROOT . DIR_RESOURCES . DIRECTORY_SEPARATOR);
0 ignored issues
show
Bug introduced by
The constant O2System\DIR_RESOURCES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
131
}
132
133
134
/*
135
 *---------------------------------------------------------------
136
 * FRAMEWORK CONSTANTS
137
 *---------------------------------------------------------------
138
 */
139
require __DIR__ . '/Config/Constants.php';
140
141
/*
142
 *---------------------------------------------------------------
143
 * FRAMEWORK HELPERS
144
 *---------------------------------------------------------------
145
 */
146
require __DIR__ . '/Helpers/Reactor.php';
147
148
/**
149
 * Class Reactor
150
 *
151
 * @package O2System
152
 */
153
class Reactor extends Kernel
154
{
155
    /**
156
     * Reactor Container Models
157
     *
158
     * @var Reactor\Containers\Models
159
     */
160
    public $models;
161
162
    /**
163
     * Reactor Container Modules
164
     *
165
     * @var Reactor\Containers\Modules
0 ignored issues
show
Bug introduced by
The type O2System\Reactor\Containers\Modules was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
166
     */
167
    public $modules;
168
169
    // ------------------------------------------------------------------------
170
171
    /**
172
     * Reactor::__construct
173
     *
174
     * @return Reactor
175
     */
176
    protected function __construct()
177
    {
178
        parent::__construct();
179
180
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
181
            profiler()->watch('Starting O2System Reactor');
182
        }
183
184
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
185
            profiler()->watch('Starting Reactor Services');
186
        }
187
188
        $services = [
189
            'Services\Loader' => 'loader',
190
            'Services\Config' => 'config'
191
        ];
192
193
        foreach ($services as $className => $classOffset) {
194
            $this->services->load($className, $classOffset);
195
        }
196
197
        // Instantiate Models Container
198
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
199
            profiler()->watch('Starting Models Container');
200
        }
201
202
        $this->models = new Reactor\Containers\Models();
203
204
        // Instantiate Cache Service
205
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
206
            profiler()->watch('Starting Cache Service');
207
        }
208
209
        $this->services->add(new Reactor\Services\Cache(config('cache', true)), 'cache');
0 ignored issues
show
Bug introduced by
It seems like config('cache', true) can also be of type O2System\Reactor\Services\Config; however, parameter $config of O2System\Reactor\Services\Cache::__construct() does only seem to accept O2System\Cache\Datastructures\Config, 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

209
        $this->services->add(new Reactor\Services\Cache(/** @scrutinizer ignore-type */ config('cache', true)), 'cache');
Loading history...
210
    }
211
212
    // ------------------------------------------------------------------------
213
214
    /**
215
     * Reactor::__reconstruct
216
     */
217
    protected function __reconstruct()
218
    {
219
        if (is_cli()) {
220
            $this->cliHandler();
221
        } else {
222
            $this->httpHandler();
223
        }
224
    }
225
226
    // ------------------------------------------------------------------------
227
228
    /**
229
     * Reactor::cliHandler
230
     *
231
     * @return void
232
     */
233
    private function cliHandler()
234
    {
235
        // Instantiate CLI Router Service
236
        $this->services->load(Kernel\Cli\Router::class);
237
238
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
239
            profiler()->watch('Parse Router Request');
240
        }
241
        router()->parseRequest();
242
243
        if ($commander = router()->getCommander()) {
244
            if ($commander instanceof Kernel\Cli\Router\Datastructures\Commander) {
0 ignored issues
show
introduced by
$commander is always a sub-type of O2System\Kernel\Cli\Rout...atastructures\Commander.
Loading history...
245
246
                // Autoload Model
247
                foreach ($this->modules as $module) {
248
                    if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
249
                        continue;
250
                    }
251
                    $module->loadModel();
252
                }
253
254
                // Autoload Model
255
                $modelClassName = str_replace('Commanders', 'Models', $commander->getName());
256
257
                if (class_exists($modelClassName)) {
258
                    models()->load($modelClassName, 'commander');
0 ignored issues
show
Bug introduced by
The method load() does not exist on O2System\Reactor\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

258
                    models()->/** @scrutinizer ignore-call */ load($modelClassName, 'commander');
Loading history...
Bug introduced by
The method load() does not exist on O2System\Reactor\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

258
                    models()->/** @scrutinizer ignore-call */ load($modelClassName, 'commander');

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...
259
                }
260
261
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
262
                    profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass());
263
                }
264
                $requestCommander = $commander->getInstance();
265
266
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
267
                    profiler()->watch('Execute Requested Commander: ' . $commander->getClass());
268
                }
269
                $requestCommander->execute();
270
271
                exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
272
            }
273
        }
274
    }
275
276
    // ------------------------------------------------------------------------
277
278
    /**
279
     * Reactor::httpHandler
280
     *
281
     * @return void
282
     */
283
    private function httpHandler()
284
    {
285
        // Instantiate Http Router Service
286
        $this->services->load(Reactor\Http\Router::class);
0 ignored issues
show
Bug introduced by
The type O2System\Reactor\Http\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
287
288
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
289
            profiler()->watch('Parse Router Request');
290
        }
291
        router()->parseRequest();
292
        
293
        // Instantiate Http Middleware Service
294
        $this->services->load(Reactor\Http\Middleware::class);
295
296
        if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
297
            profiler()->watch('Running Middleware Service: Pre Controller');
298
        }
299
        middleware()->run();
300
301
        if (false !== ($controller = $this->services->get('controller'))) {
302
            if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) {
303
                // Autoload Model
304
                foreach ($this->modules as $module) {
305
                    if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) {
306
                        continue;
307
                    }
308
                    $module->loadModel();
309
                }
310
311
                // Autoload Model
312
                $modelClassName = str_replace('Controllers', 'Models', $controller->getName());
313
314
                if (class_exists($modelClassName)) {
315
                    $this->models->register($modelClassName, 'controller');
0 ignored issues
show
Bug introduced by
$modelClassName of type string is incompatible with the type O2System\Spl\Containers\...ures\SplServiceRegistry expected by parameter $service of O2System\Reactor\Containers\Models::register(). ( Ignorable by Annotation )

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

315
                    $this->models->register(/** @scrutinizer ignore-type */ $modelClassName, 'controller');
Loading history...
316
                }
317
318
                // Initialize Controller
319
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
320
                    profiler()->watch('Calling Hooks Service: Pre Controller');
321
                }
322
323
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
324
                    profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass());
325
                }
326
                $requestController = $controller->getInstance();
327
328
                if (method_exists($requestController, '__reconstruct')) {
329
                    $requestController->__reconstruct();
330
                } elseif (method_exists($requestController, 'initialize')) {
331
                    $requestController->initialize();
332
                }
333
334
                $this->services->add($requestController, 'controller');
335
336
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
337
                    profiler()->watch('Calling Middleware Service: Post Controller');
338
                }
339
                hooks()->callEvent(Reactor\Services\Hooks::POST_CONTROLLER);
0 ignored issues
show
Bug introduced by
The type O2System\Reactor\Services\Hooks was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The function hooks was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

339
                /** @scrutinizer ignore-call */ 
340
                hooks()->callEvent(Reactor\Services\Hooks::POST_CONTROLLER);
Loading history...
340
341
                $requestMethod = $controller->getRequestMethod();
342
                $requestMethodArgs = $controller->getRequestMethodArgs();
343
344
                // Call the requested controller method
345
                if (profiler() !== false) {
0 ignored issues
show
introduced by
The condition profiler() !== false is always true.
Loading history...
346
                    profiler()->watch('Execute Requested Controller Method');
347
                }
348
                ob_start();
349
                $requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs);
350
351
                if (empty($requestControllerOutput)) {
352
                    $requestControllerOutput = ob_get_contents();
353
                    ob_end_clean();
354
                }
355
356
                if (empty($requestControllerOutput) or $requestControllerOutput === '') {
357
                    // Send default error 204 - No Content
358
                    output()->sendError(204);
359
                } elseif (is_bool($requestControllerOutput)) {
360
                    if ($requestControllerOutput === true) {
361
                        output()->sendError(200);
362
                    } elseif ($requestControllerOutput === false) {
0 ignored issues
show
introduced by
The condition $requestControllerOutput === false is always true.
Loading history...
363
                        output()->sendError(204);
364
                    }
365
                } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) {
366
                    $requestController->sendPayload($requestControllerOutput);
367
                } elseif (is_numeric($requestControllerOutput)) {
368
                    output()->sendError($requestControllerOutput);
369
                } elseif (is_string($requestControllerOutput)) {
370
                    if (is_json($requestControllerOutput)) {
371
                        output()->setContentType('application/json');
0 ignored issues
show
Bug introduced by
The method setContentType() 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

371
                        output()->/** @scrutinizer ignore-call */ setContentType('application/json');

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...
372
                        output()->send($requestControllerOutput);
0 ignored issues
show
Bug introduced by
The method send() 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

372
                        output()->/** @scrutinizer ignore-call */ send($requestControllerOutput);

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...
373
                    } elseif (is_serialized($requestControllerOutput)) {
374
                        output()->send($requestControllerOutput);
375
                    } else {
376
                        output()->send($requestControllerOutput);
377
                    }
378
                }
379
            }
380
        }
381
382
        // Show Error (404) Page Not Found
383
        output()->sendError(404);
384
    }
385
}
386