Passed
Push — master ( 76ec75...d0de83 )
by Fran
03:15
created

Dispatcher::run()   B

Complexity

Conditions 9
Paths 28

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.1317

Importance

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