Completed
Push — master ( 03df00...648c2d )
by Anu
07:35 queued 05:02
created

app   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

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

20 Methods

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

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
{
5
    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...
6
    public $app;
7
    public $session;
8
    public $authenticatedUser;
9
10
    public function __construct()
11
    {
12
        $this->app = $this->app ?: Base::instance();
13
    }
14
15
    public static function singleton()
16
    {
17
        if (Registry::exists('APP')) {
18
            $app = Registry::get('APP');
19
        } else {
20
            $app = new self;
21
            Registry::set('APP', $app);
22
        }
23
        return $app;
24
    }
25
26
    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...
27
    {
28
        return $this->db ?: $this->app->DB;
29
    }
30
31
    public function session()
32
    {
33
        return $this->session ?: $this->app->SESSION;
34
    }
35
36
    public function user()
37
    {
38
        return $this->authenticatedUser = $this->app->get('SESSION.USER')?:false;
39
    }
40
41
    public function initialized($def = null)
42
    {
43
        return null !== $def ? $this->app->set('INITIALIZED', $def) : $this->app->get('INITIALIZED');
44
    }
45
46
    public function config($key = null)
47
    {
48
        return $this->app->get($key?:'CONFIG') ;
49
    }
50
51
    public function status()
52
    {
53
        return $this->app->IS_LIVE;
54
    }
55
56
    public function run()
57
    {
58
        $this->loadConfig();
59
        $this->loadRoutes();
60
        $this->checkForMaintenance();
61
        $this->configureDebug();
62
        $this->configureDB();
63
        $this->configureSession();
64
        $this->configureAssets();
65
        $this->registerErrorHandler();
66
        $this->initialized(true);
67
        $this->app->run();
68
    }
69
70
    public function loadRoutes($file = null)
71
    {
72
        $this->_load('routes', $file);
73
    }
74
75
    public function loadConfig($file = null)
76
    {
77
        $this->_load('config', $file);
78
    }
79
80
    private function _load($path, $file = null)
81
    {
82
        if ($file) {
83
            $this->app->config(base_path("{path}/{$file}"));
84
        } else {
85
            foreach (glob(base_path("{$path}/*.ini")) as $file) {
86
                $this->app->config($file);
87
            }
88
        }
89
    }
90
91
    public function checkForMaintenance()
92
    {
93
        if (!$this->status()) {
94
            template('maintenance');
95
            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...
96
        }
97
    }
98
99
    public function configureDebug()
100
    {
101
        if (!$this->app->DEV) {
102
            $this->app->set('DEBUG', 0);
103
        }
104
    }
105
106
    public function configureDB()
107
    {
108
        $type = strtolower($this->app->DB_TYPE);
109
110
        if ($type == 'jig') {
111
            $this->db = new DB\Jig($this->app->DB_PATH, DB\Jig::FORMAT_JSON);
112
        } elseif ($type == 'sql') {
113
            $this->db = new DB\SQL($this->app->DB, $this->app->DB_USER, $this->app->DB_PSWD);
114
        } elseif ($type == 'mongo') {
115
            $this->db = new DB\Mongo($this->app->DB, $this->app->DB_USER);
116
        }
117
        $this->app->set('DB', $this->db);
118
    }
119
120
    public function configureSession()
121
    {
122
        $type = strtolower($this->app->SESSION);
123
124
        if ($type) {
125
            if ($this->app->CSRF && ('jig' == $type || 'sql' == $type || 'mongo' == $type)) {
126
                $this->configureCSRF($type);
127
            } elseif ($this->app->CSRF) {
128
                $this->session = new Session(null, 'CSRF');
129
            } else {
130
                if ($type == 'jig' || $type == 'mongo' || $type == 'sql') {
131
                    $session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session');
132
                    $this->session = new $session($this->app->DB);
133
                } else {
134
                    $this->session = new Session();
135
                }
136
            }
137
            $this->app->set('SESSION', $this->session);
138
        }
139
    }
140
141
    public function configureCSRF($type)
142
    {
143
        $session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session');
144
        $this->session = new $session($this->app->DB, 'sessions', null, 'CSRF');
145
    }
146
147
    private function _getDBType($type)
148
    {
149
        if ($type == 'jig' || $type == 'mongo') {
150
            return ucfirst($type);
151
        } elseif ($type == 'sql') {
152
            return strtoupper($type);
153
        }
154
    }
155
156
    public function configureAssets()
157
    {
158
        $assets = Assets::instance();
0 ignored issues
show
Unused Code introduced by
The assignment to $assets is dead and can be removed.
Loading history...
159
        $this->app->set('ASSETS.onFileNotFound', function ($file) {
160
            echo 'file not found: '.$file;
161
        });
162
    }
163
164
    public function registerErrorHandler()
165
    {
166
        if ($this->app->DEV) {
167
            Falsum\Run::handler($this->app->DEBUG != 3);
168
        } else {
169
            $this->app->set('ONERROR', 'App\Core\Controllers\ErrorController->init');
170
        }
171
    }
172
}
173