Test Failed
Push — master ( 21e806...ed25dd )
by Fran
05:25 queued 02:04
created

Dispatcher::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 2
rs 9.9332
c 0
b 0
f 0
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
    public function init()
52
    {
53
        Logger::log('Dispatcher init');
54
        parent::init();
55
        $this->initiateStats();
56
        I18nHelper::setLocale();
57
        $this->bindWarningAsExceptions();
58
        $this->actualUri = Request::getInstance()->getServer('REQUEST_URI');
59
        Logger::log('End dispatcher init');
60
    }
61
62
    /**
63
     * Run method
64
     * @return string HTML
65
     */
66
    public function run()
67
    {
68
        Logger::log('Begin runner');
69
        try {
70
            if ($this->config->isConfigured()) {
71
                //Check CORS for requests
72
                RequestHelper::checkCORS();
73
                if (!Request::getInstance()->isFile()) {
74
                    return $this->router->execute($this->actualUri);
75
                }
76
            } else {
77
                return ConfigController::getInstance()->config();
78
            }
79
        } catch (AdminCredentialsException $a) {
80
            return UserController::showAdminManager();
81
        } catch (SecurityException $s) {
82
            return $this->security->notAuthorized($this->actualUri);
83
        } catch (RouterException $r) {
84
            return $this->router->httpNotFound($r);
85
        } catch(ApiException $a) {
86
            return $this->router->httpNotFound($a, true);
87
        } catch (\Exception $e) {
88
            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
    protected function dumpException(\Exception $e)
100
    {
101
        Logger::log('Starting dump exception');
102
        $ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e;
103
        $error = array(
104
            "error" => $ex->getMessage(),
105
            "file" => $ex->getFile(),
106
            "line" => $ex->getLine(),
107
        );
108
        Logger::log('Throwing exception', LOG_ERR, $error);
109
        unset($error);
110
111
        return $this->router->httpNotFound($ex);
112
    }
113
114
}
115