Test Failed
Push — master ( 306f11...77481f )
by Fran
14:04
created

Dispatcher::run()   B

Complexity

Conditions 9
Paths 28

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 18
c 0
b 0
f 0
nc 28
nop 1
dl 0
loc 23
ccs 0
cts 17
cp 0
crap 90
rs 8.0555
1
<?php
2
/**
3
 * @author Fran López <[email protected]>
4
 * @version 1.0
5
 */
6
7
namespace PSFS;
8
9
use PSFS\base\config\Config;
10
use PSFS\base\events\CloseSessionEvent;
11
use PSFS\base\exception\AdminCredentialsException;
12
use PSFS\base\exception\ApiException;
13
use PSFS\base\exception\RouterException;
14
use PSFS\base\exception\SecurityException;
15
use PSFS\base\Logger;
16
use PSFS\base\Request;
0 ignored issues
show
Bug introduced by
The type PSFS\base\Request 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...
17
use PSFS\base\Singleton;
18
use PSFS\base\types\helpers\EventHelper;
19
use PSFS\base\types\helpers\I18nHelper;
20
use PSFS\base\types\helpers\Inspector;
21
use PSFS\base\types\helpers\RequestHelper;
22
use PSFS\base\types\traits\SystemTrait;
23
use PSFS\controller\ConfigController;
24
use PSFS\controller\UserController;
25
26
/**
27
 * Class Dispatcher
28
 * @package PSFS
29
 */
30
class Dispatcher extends Singleton
31
{
32
    use SystemTrait;
33
34
    /**
35
     * @Injectable
36
     * @var \PSFS\base\Security $security
37
     */
38
    protected $security;
39
    /**
40
     * @Injectable
41
     * @var \PSFS\base\Router $router
42
     */
43
    protected $router;
44
    /**
45
     * @Injectable
46
     * @var \PSFS\base\config\Config $config
47
     */
48
    protected $config;
49
50
    private $actualUri;
51
52
    /**
53
     * Initializer method
54
     * @throws base\exception\GeneratorException
55
     */
56 4
    public function init()
57
    {
58 4
        Config::getInstance();
59 4
        Inspector::stats('[Dispatcher] Dispatcher init', Inspector::SCOPE_DEBUG);
60 4
        parent::init();
61 4
        $this->initiateStats();
62 4
        I18nHelper::setLocale();
63
        $this->bindWarningAsExceptions();
64
        $this->actualUri = Request::getInstance()->getServer('REQUEST_URI');
65
        Inspector::stats('[Dispatcher] Dispatcher init end', Inspector::SCOPE_DEBUG);
66
        EventHelper::addEvent(EventHelper::EVENT_END_REQUEST, CloseSessionEvent::class);
67
    }
68
69
    /**
70
     * Run method
71
     * @param string $uri
72
     * @return string HTML
73
     * @throws base\exception\GeneratorException
74
     */
75
    public function run($uri = null)
76
    {
77
        Inspector::stats('[Dispatcher] Begin runner', Inspector::SCOPE_DEBUG);
78
        try {
79
            if ($this->config->isConfigured()) {
80
                //Check CORS for requests
81
                RequestHelper::checkCORS();
82
                if (!Request::getInstance()->isFile()) {
83
                    return $this->router->execute($uri ?: $this->actualUri);
84
                }
85
            } else {
86
                return ConfigController::getInstance()->config();
87
            }
88
        } catch (AdminCredentialsException $a) {
89
            return UserController::showAdminManager();
90
        } catch (SecurityException $s) {
91
            return $this->security->notAuthorized($this->actualUri);
92
        } catch (RouterException $r) {
93
            return $this->router->httpNotFound($r);
94
        } catch (ApiException $a) {
95
            return $this->router->httpNotFound($a, true);
96
        } catch (\Exception $e) {
97
            return $this->dumpException($e);
98
        }
99
    }
100
101
    /**
102
     * @param \Exception $exception
103
     * @return string HTML
104
     * @throws base\exception\GeneratorException
105
     */
106
    protected function dumpException(\Exception $exception)
107
    {
108
        Inspector::stats('[Dispatcher] Starting dump exception', Inspector::SCOPE_DEBUG);
109
        $exception = (NULL !== $exception->getPrevious()) ? $exception->getPrevious() : $exception;
110
        $error = array(
111
            "error" => $exception->getMessage(),
112
            "file" => $exception->getFile(),
113
            "line" => $exception->getLine(),
114
        );
115
        Logger::log('Throwing exception', LOG_ERR, $error);
116
        unset($error);
117
118
        return $this->router->httpNotFound($exception);
119
    }
120
121
}
122
123
Config::initialize();
124