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

App   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 146
rs 8.439
c 0
b 0
f 0
wmc 47

20 Methods

Rating   Name   Duplication   Size   Complexity  
A loadRoutes() 0 2 1
A config() 0 2 2
A configureDB() 0 11 4
A loadConfig() 0 2 1
A run() 0 11 1
A initialized() 0 2 2
A db() 0 2 2
A configureCSRF() 0 3 1
A configureAssets() 0 4 1
A session() 0 2 2
A singleton() 0 8 2
A __construct() 0 2 2
A configureDebug() 0 3 2
A _getDBType() 0 5 4
B configureSession() 0 17 10
A checkForMaintenance() 0 4 2
A registerErrorHandler() 0 5 2
A _load() 0 6 3
A status() 0 2 1
A user() 0 2 2

How to fix   Complexity   

Complex Class

Complex classes like App often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use App, and based on these observations, apply Extract Interface, too.

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
}