Completed
Push — master ( 3b33a7...03df00 )
by Anu
02:48
created

App::configureSession()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 13
nc 5
nop 0
dl 0
loc 17
rs 7.2765
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class App extends Prefab{
4
    public $db;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. 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...
5
	public $app;
6
    public $session;
7
    public $authenticatedUser;
8
9
	public function __construct() {
10
	    $this->app = $this->app ?: Base::instance();
11
	}
12
13
    static public function singleton() {
14
        if (Registry::exists('APP'))
15
            $app = Registry::get('APP');
16
        else {
17
            $app = new self;
18
            Registry::set('APP',$app);
19
        }
20
        return $app;
21
    }
22
23
    public function db() {
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
24
        return $this->db ?: $this->app->DB;
25
    }
26
27
    public function session() {
28
        return $this->session ?: $this->app->SESSION;
29
    }
30
31
	public function user() {
32
		return $this->authenticatedUser = $this->app->get('SESSION.USER')?:false;
33
	}
34
35
    public function initialized($def = null) {
36
        return null !== $def ? $this->app->set('INITIALIZED', $def) : $this->app->get('INITIALIZED');
37
    }
38
39
    public function config($key = null) {
40
        return $this->app->get($key?:'CONFIG') ;
41
    }
42
43
    public function status() {
44
        return $this->app->IS_LIVE;
45
    }
46
47
    public function run() {
48
        $this->loadConfig();
49
        $this->loadRoutes();
50
        $this->checkForMaintenance();
51
        $this->configureDebug();
52
	    $this->configureDB();
53
	    $this->configureSession();
54
        $this->configureAssets();
55
		$this->registerErrorHandler();
56
	    $this->initialized(true);
57
		$this->app->run();
58
    }
59
60
    public function loadRoutes($file = null){
61
        $this->_load('routes', $file);
62
    }
63
64
    public function loadConfig($file = null){
65
        $this->_load('config', $file);
66
    }
67
68
    private function _load($path, $file = null) {
69
        if($file) {
70
            $this->app->config(base_path("{path}/{$file}"));
71
        }else{
72
            foreach(glob(base_path("{$path}/*.ini")) as $file) {
73
                $this->app->config($file);
74
            }
75
        }
76
    }
77
78
	public function checkForMaintenance() {
79
		if(!$this->status()) {
80
	       	template('maintenance');
81
		    exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkForMaintenance() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
82
        }
83
	}
84
85
    public function configureDebug() {
86
	    if(!$this->app->DEV) {
87
	        $this->app->set('DEBUG', 0);
88
        }
89
    }
90
91
    public function configureDB() {
92
        $type = strtolower($this->app->DB_TYPE);
93
94
        if($type == 'jig'){
95
            $this->db = new DB\Jig($this->app->DB_PATH, DB\Jig::FORMAT_JSON);
96
        }else if($type == 'sql'){
97
            $this->db = new DB\SQL($this->app->DB, $this->app->DB_USER, $this->app->DB_PSWD);
98
        }else if($type == 'mongo'){
99
            $this->db = new DB\Mongo($this->app->DB, $this->app->DB_USER);
100
        }
101
        $this->app->set('DB', $this->db);
102
    }
103
104
    public function configureSession() {
105
        $type = strtolower($this->app->SESSION);
106
107
        if($type) {
108
            if($this->app->CSRF && ('jig' == $type || 'sql' == $type || 'mongo' == $type)) {
109
                $this->configureCSRF($type);
110
            }else if($this->app->CSRF){
111
                $this->session = new Session(null, 'CSRF');
112
            }else{
113
                if($type == 'jig' || $type == 'mongo' || $type == 'sql'){
114
                    $session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session');
115
                    $this->session = new $session($this->app->DB);
116
                }else{
117
                    $this->session = new Session();
118
                }
119
            }
120
            $this->app->set('SESSION', $this->session);
121
        }
122
    }
123
124
    public function configureCSRF($type) {
125
        $session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session');
126
        $this->session = new $session($this->app->DB, 'sessions', null, 'CSRF');
127
    }
128
129
    private function _getDBType($type) {
130
        if($type == 'jig' || $type == 'mongo'){
131
            return ucfirst($type);
132
        }else if($type == 'sql') {
133
            return strtoupper($type);
134
        }
135
    }
136
137
    public function configureAssets() {
138
        $assets = Assets::instance();
0 ignored issues
show
Unused Code introduced by
The assignment to $assets is dead and can be removed.
Loading history...
139
        $this->app->set('ASSETS.onFileNotFound',function($file) {
140
            echo 'file not found: '.$file;
141
        });
142
    }
143
144
	public function registerErrorHandler() {
145
		if($this->app->DEV) {
146
			Falsum\Run::handler($this->app->DEBUG != 3);
147
		}else{
148
			$this->app->set('ONERROR', 'App\Core\Controllers\ErrorController->init');
149
		}
150
	}
151
}