Test Failed
Push — master ( 48e4aa...acaa8d )
by Fran
02:55
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', Inspector::SCOPE_DEBUG);
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', Inspector::SCOPE_DEBUG);
63
    }
64
65
    /**
66
     * Run method
67
     * @param string $uri
68
     * @return string HTML
69
     * @throws base\exception\GeneratorException
70
     */
71 6
    public function run($uri = null)
72
    {
73 6
        Inspector::stats('[Dispatcher] Begin runner', Inspector::SCOPE_DEBUG);
74
        try {
75 6
            if ($this->config->isConfigured()) {
76
                //Check CORS for requests
77 5
                RequestHelper::checkCORS();
78 5
                if (!Request::getInstance()->isFile()) {
79 5
                    return $this->router->execute($uri ?: $this->actualUri);
80
                }
81
            } else {
82 1
                return ConfigController::getInstance()->config();
83
            }
84 4
        } catch (AdminCredentialsException $a) {
85
            return UserController::showAdminManager();
86 4
        } catch (SecurityException $s) {
87 1
            return $this->security->notAuthorized($this->actualUri);
88 3
        } catch (RouterException $r) {
89 1
            return $this->router->httpNotFound($r);
90 2
        } catch(ApiException $a) {
91
            return $this->router->httpNotFound($a, true);
92 2
        } catch (\Exception $e) {
93 2
            return $this->dumpException($e);
94
        }
95
    }
96
97
    /**
98
     * @param \Exception $exception
99
     * @return string HTML
100
     * @throws base\exception\GeneratorException
101
     */
102 2
    protected function dumpException(\Exception $exception)
103
    {
104 2
        Inspector::stats('[Dispatcher] Starting dump exception', Inspector::SCOPE_DEBUG);
105 2
        $exception = (NULL !== $exception->getPrevious()) ? $exception->getPrevious() : $exception;
106 2
        $error = array(
107 2
            "error" => $exception->getMessage(),
108 2
            "file" => $exception->getFile(),
109 2
            "line" => $exception->getLine(),
110 2
        );
111 2
        Logger::log('Throwing exception', LOG_ERR, $error);
112 2
        unset($error);
113
114 2
        return $this->router->httpNotFound($exception);
115
    }
116
117
}
118
119
Config::initialize();
120