Passed
Push — master ( 137834...e941dd )
by Fran
03:36
created

Dispatcher::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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