Passed
Push — master ( ae9ca1...a66c0c )
by Fran
02:41
created

Dispatcher::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
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;
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 1
    public function init()
57
    {
58 1
        Config::getInstance();
59 1
        Inspector::stats('[Dispatcher] Dispatcher init', Inspector::SCOPE_DEBUG);
60 1
        parent::init();
61 1
        $this->initiateStats();
62 1
        I18nHelper::setLocale();
63 1
        $this->bindWarningAsExceptions();
64 1
        $this->actualUri = Request::getInstance()->getServer('REQUEST_URI');
65 1
        Inspector::stats('[Dispatcher] Dispatcher init end', Inspector::SCOPE_DEBUG);
66 1
        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 6
    public function run($uri = null)
76
    {
77 6
        Inspector::stats('[Dispatcher] Begin runner', Inspector::SCOPE_DEBUG);
78
        try {
79 6
            if ($this->config->isConfigured()) {
80
                //Check CORS for requests
81 5
                RequestHelper::checkCORS();
82 5
                if (!Request::getInstance()->isFile()) {
83 5
                    return $this->router->execute($uri ?: $this->actualUri);
84
                }
85
            } else {
86 1
                return ConfigController::getInstance()->config();
87
            }
88 4
        } catch (AdminCredentialsException $a) {
89
            return UserController::showAdminManager();
90 4
        } catch (SecurityException $s) {
91 1
            return $this->security->notAuthorized($this->actualUri);
92 3
        } catch (RouterException $r) {
93 1
            return $this->router->httpNotFound($r);
94 2
        } catch (ApiException $a) {
95
            return $this->router->httpNotFound($a, true);
96 2
        } catch (\Exception $e) {
97 2
            return $this->dumpException($e);
98
        }
99
    }
100
101
    /**
102
     * @param \Exception $exception
103
     * @return string HTML
104
     * @throws base\exception\GeneratorException
105
     */
106 2
    protected function dumpException(\Exception $exception)
107
    {
108 2
        Inspector::stats('[Dispatcher] Starting dump exception', Inspector::SCOPE_DEBUG);
109 2
        $exception = (NULL !== $exception->getPrevious()) ? $exception->getPrevious() : $exception;
110 2
        $error = array(
111 2
            "error" => $exception->getMessage(),
112 2
            "file" => $exception->getFile(),
113 2
            "line" => $exception->getLine(),
114 2
        );
115 2
        Logger::log('Throwing exception', LOG_ERR, $error);
116 2
        unset($error);
117
118 2
        return $this->router->httpNotFound($exception);
119
    }
120
121
}
122
123
Config::initialize();
124