Test Failed
Push — master ( 0d14a6...e876c1 )
by Fran
09:15
created

Dispatcher::run()   C

Complexity

Conditions 7
Paths 23

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.4822

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 23
nop 0
dl 0
loc 23
ccs 11
cts 14
cp 0.7856
crap 7.4822
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Fran López <[email protected]>
4
 * @version 1.0
5
 */
6
7
namespace PSFS;
8
9
use PSFS\base\exception\AdminCredentialsException;
10
use PSFS\base\exception\RouterException;
11
use PSFS\base\exception\SecurityException;
12
use PSFS\base\Logger;
13
use PSFS\base\Request;
14
use PSFS\base\Security;
15
use PSFS\base\Singleton;
16
use PSFS\base\types\helpers\I18nHelper;
17
use PSFS\base\types\helpers\RequestHelper;
18
use PSFS\base\types\traits\SystemTrait;
19
use PSFS\controller\ConfigController;
20
use PSFS\controller\UserController;
21
22
/**
23
 * Class Dispatcher
24
 * @package PSFS
25
 */
26
class Dispatcher extends Singleton
27
{
28
    use SystemTrait;
29
    /**
30
     * @Inyectable
31
     * @var \PSFS\base\Security $security
32
     */
33
    protected $security;
34
    /**
35
     * @Inyectable
36
     * @var \PSFS\base\Router $router
37
     */
38
    protected $router;
39
    /**
40
     * @Inyectable
41
     * @var \PSFS\base\config\Config $config
42
     */
43
    protected $config;
44
45
    private $actualUri;
46
47
    /**
48
     * Initializer method
49 1
     */
50
    public function init()
51 1
    {
52 1
        Logger::log('Dispatcher init');
53 1
        parent::init();
54 1
        $this->initiateStats();
55 1
        I18nHelper::setLocale();
56 1
        $this->bindWarningAsExceptions();
57 1
        $this->actualUri = Request::getInstance()->getServer("REQUEST_URI");
58 1
        Logger::log('End dispatcher init');
59
    }
60
61
    /**
62
     * Run method
63
     * @return string HTML
64 4
     */
65
    public function run()
66 4
    {
67
        Logger::log('Begin runner');
68 4
        try {
69 4
            if ($this->config->isConfigured()) {
70 4
                //Check CORS for requests
71
                RequestHelper::checkCORS();
72
                if (!Request::getInstance()->isFile()) {
73
                    return $this->router->execute($this->actualUri);
74
                }
75 3
            } else {
76
                return ConfigController::getInstance()->config();
77 3
            }
78 1
        } catch (AdminCredentialsException $a) {
79 2
            return UserController::showAdminManager();
80 1
        } catch (SecurityException $s) {
81 1
            return $this->security->notAuthorized($this->actualUri);
82 1
        } catch (RouterException $r) {
83
            return $this->router->httpNotFound($r);
84
        } catch (\Exception $e) {
85
            return $this->dumpException($e);
86
        }
87
    }
88
89
    /**
90
     * Method that convert an exception to html
91
     *
92
     * @param \Exception $e
93 1
     *
94
     * @return string HTML
95 1
     */
96 1
    protected function dumpException(\Exception $e)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
97
    {
98 1
        Logger::log('Starting dump exception');
99 1
        $ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ex. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
100 1
        $error = array(
101
            "error" => $ex->getMessage(),
102 1
            "file" => $ex->getFile(),
103 1
            "line" => $ex->getLine(),
104
        );
105 1
        Logger::log('Throwing exception', LOG_ERR, $error);
106
        unset($error);
107
108
        return $this->router->httpNotFound($ex);
109
    }
110
111
}
112