Passed
Push — master ( 510786...275623 )
by Fran
04:26
created

Dispatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
dl 0
loc 84
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
B run() 0 21 7
A dumpException() 0 14 2
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 1
    public function init()
50
    {
51 1
        Logger::log('Dispatcher init');
52 1
        parent::init();
53 1
        $this->initiateStats();
54 1
        I18nHelper::setLocale();
55 1
        $this->bindWarningAsExceptions();
56 1
        $this->actualUri = Request::getInstance()->getServer("REQUEST_URI");
57 1
        Logger::log('End dispatcher init');
58 1
    }
59
60
    /**
61
     * Run method
62
     * @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
     */
64 5
    public function run()
65
    {
66 5
        Logger::log('Begin runner');
67
        try {
68 5
            if ($this->config->isConfigured()) {
69 4
                if (!Request::getInstance()->isFile()) {
70 4
                    return $this->router->execute($this->actualUri);
71
                }
72
            } else {
73 1
                return ConfigController::getInstance()->config();
74
            }
75 4
        } catch (AdminCredentialsException $a) {
76
            return UserController::showAdminManager();
77 4
        } catch (SecurityException $s) {
78 1
            return $this->security->notAuthorized($this->actualUri);
79 3
        } catch (RouterException $r) {
80 1
            return $this->router->httpNotFound($r);
81 2
        } catch (\Exception $e) {
82 2
            return $this->dumpException($e);
83
        }
84
    }
85
86
    /**
87
     * Method that convert an exception to html
88
     *
89
     * @param \Exception $e
90
     *
91
     * @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 2
    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 2
        Logger::log('Starting dump exception');
96 2
        $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 2
            "error" => $ex->getMessage(),
99 2
            "file" => $ex->getFile(),
100 2
            "line" => $ex->getLine(),
101 2
        );
102 2
        Logger::log('Throwing exception', LOG_ERR, $error);
103 2
        unset($error);
104
105 2
        return $this->router->httpNotFound($ex);
106
    }
107
108
}
109