Test Failed
Push — master ( fc5a43...510786 )
by Fran
04:03
created

Dispatcher::initiateStats()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 3.0175
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\traits\SystemTrait;
18
use PSFS\controller\ConfigController;
19
use PSFS\controller\UserController;
20
21
/**
22
 * Class Dispatcher
23
 * @package PSFS
24
 */
25
class Dispatcher extends Singleton
26
{
27
    use SystemTrait;
28
    /**
29
     * @Inyectable
30
     * @var \PSFS\base\Security $security
31
     */
32
    protected $security;
33
    /**
34
     * @Inyectable
35
     * @var \PSFS\base\Router $router
36
     */
37
    protected $router;
38
    /**
39
     * @Inyectable
40
     * @var \PSFS\base\config\Config $config
41
     */
42
    protected $config;
43
44
    private $actualUri;
45
46
    /**
47
     * Initializer method
48
     */
49
    public function init()
50
    {
51
        Logger::log('Dispatcher init');
52
        parent::init();
53
        $this->initiateStats();
54
        I18nHelper::setLocale();
55
        $this->bindWarningAsExceptions();
56
        $this->actualUri = Request::getInstance()->getServer("REQUEST_URI");
57
        Logger::log('End dispatcher init');
58 1
    }
59
60 1
    /**
61 1
     * Run method
62 1
     * @return string HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
63 1
     */
64 1
    public function run()
65 1
    {
66 1
        Logger::log('Begin runner');
67 1
        try {
68
            if ($this->config->isConfigured()) {
69
                if (!Request::getInstance()->isFile()) {
70
                    return $this->router->execute($this->actualUri);
71
                }
72
            } else {
73 5
                return ConfigController::getInstance()->config();
74
            }
75 5
        } catch (AdminCredentialsException $a) {
76
            return UserController::showAdminManager();
77 5
        } catch (SecurityException $s) {
78 4
            return Security::getInstance()->notAuthorized($this->actualUri);
79 4
        } catch (RouterException $r) {
80
            return $this->router->httpNotFound($r);
81
        } catch (\Exception $e) {
82 1
            return $this->dumpException($e);
83
        }
84 4
    }
85
86 4
    /**
87 1
     * Method that convert an exception to html
88 3
     *
89 1
     * @param \Exception $e
90 2
     *
91 2
     * @return string HTML
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
     */
93
    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...
94
    {
95
        Logger::log('Starting dump exception');
96
        $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...
97
        $error = array(
98
            "error" => $ex->getMessage(),
99
            "file" => $ex->getFile(),
100
            "line" => $ex->getLine(),
101
        );
102 2
        Logger::log('Throwing exception', LOG_ERR, $error);
103
        unset($error);
104 2
105 2
        return $this->router->httpNotFound($ex);
106
    }
107 2
108
}
109