Test Failed
Push — master ( 0dac24...9d4dcc )
by Jean-Christophe
02:41
created

Startup   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 81.13%

Importance

Changes 0
Metric Value
eloc 107
dl 0
loc 184
ccs 86
cts 106
cp 0.8113
rs 8.8
c 0
b 0
f 0
wmc 45

18 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 6 2
A getApplicationName() 0 2 1
A getControllerSimpleName() 0 2 1
A callController() 0 19 4
A errorHandler() 0 6 3
A getFrameworkDir() 0 2 1
A parseUrl() 0 9 3
A getController() 0 2 1
A runAsString() 0 4 1
A startTemplateEngine() 0 12 4
B runAction() 0 25 7
A getViewNameFileExtension() 0 2 1
A getAction() 0 2 1
A runCallable() 0 12 4
A forward() 0 18 5
A getApplicationDir() 0 2 1
A getActionParams() 0 2 1
A injectDependences() 0 6 4

How to fix   Complexity   

Complex Class

Complex classes like Startup 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 Startup, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\controllers;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\views\engine\TemplateEngine;
7
use Ubiquity\utils\http\USession;
8
use Ubiquity\log\Logger;
9
use Ubiquity\controllers\traits\StartupConfigTrait;
10
11
/**
12
 * Starts the framework
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.0
17
 *
18
 */
19
class Startup {
20
	use StartupConfigTrait;
21
	public static $urlParts;
22
	public static $templateEngine;
23
	private static $controller;
24
	private static $action;
25
	private static $actionParams;
26
27 3
	public static function run(array &$config) {
28 3
		self::$config = $config;
29 3
		self::startTemplateEngine ( $config );
30 3
		if (isset ( $config ['sessionName'] ))
31 3
			USession::start ( $config ['sessionName'] );
32 3
		self::forward ( $_GET ['c'] );
33 3
	}
34
35 4
	public static function forward($url, $initialize = true, $finalize = true) {
36 4
		$u = self::parseUrl ( $url );
37 4
		if (($ru = Router::getRoute ( $url )) !== false) {
38 3
			if (\is_array ( $ru )) {
39 3
				self::runAction ( $ru, $initialize, $finalize );
40
			} else {
41 3
				echo $ru; // Displays route response from cache
42
			}
43
		} else {
44 4
			self::setCtrlNS ();
45 4
			$u [0] = self::$ctrlNS . $u [0];
46 4
			if (\class_exists ( $u [0] )) {
47 4
				self::runAction ( $u, $initialize, $finalize );
48
			} elseif (is_callable ( $u [0] )) {
49
				self::runCallable ( $u );
50
			} else {
51
				\header ( 'HTTP/1.0 404 Not Found', true, 404 );
52
				Logger::warn ( "Startup", "The controller `" . $u [0] . "` doesn't exists! <br/>", "forward" );
53 4
			}
54
		}
55 4
	}
56 4
57
	private static function parseUrl(&$url) {
58
		if (! $url) {
59 4
			$url = "_default";
60 3
		}
61 4
		if (UString::endswith ( $url, "/" ))
62
			$url = \substr ( $url, 0, strlen ( $url ) - 1 );
63 4
		self::$urlParts = \explode ( "/", $url );
64
65
		return self::$urlParts;
66 3
	}
67
68 3
	private static function startTemplateEngine(&$config) {
69
		try {
70
			if (isset ( $config ['templateEngine'] )) {
71
				$templateEngine = $config ['templateEngine'];
72
				$engineOptions = $config ['templateEngineOptions'] ?? array ('cache' => \ROOT . \DS . 'views/cache/' );
73 3
				$engine = new $templateEngine ( $engineOptions );
74
				if ($engine instanceof TemplateEngine) {
75
					self::$templateEngine = $engine;
76
				}
77
			}
78
		} catch ( \Exception $e ) {
79 3
			echo $e->getTraceAsString ();
80
		}
81 5
	}
82 5
83 5
	public static function runAction(array &$u, $initialize = true, $finalize = true) {
84 5
		$ctrl = $u [0];
85 5
		self::$controller = $ctrl;
86 5
		self::$action = "index";
87 5
		self::$actionParams = [ ];
88 5
		if (\sizeof ( $u ) > 1)
89 1
			self::$action = $u [1];
90
		if (\sizeof ( $u ) > 2)
91 5
			self::$actionParams = array_slice ( $u, 2 );
92 5
93
		$controller = new $ctrl ();
94
		if (! $controller instanceof Controller) {
95
			print "`{$u[0]}` isn't a controller instance.`<br/>";
96
			return;
97 5
		}
98 5
		// Dependency injection
99 4
		self::injectDependences ( $controller );
100
		if (! $controller->isValid ( self::$action )) {
101 5
			$controller->onInvalidControl ();
102 5
		} else {
103 5
			if ($initialize)
104 5
				$controller->initialize ();
105 5
			self::callController ( $controller, $u );
106
			if ($finalize)
107 5
				$controller->finalize ();
108
		}
109 6
	}
110 6
111 6
	public static function runCallable(array &$u) {
112 6
		self::$actionParams = [ ];
113 6
		if (\sizeof ( $u ) > 1) {
114 6
			self::$actionParams = array_slice ( $u, 1 );
115
		}
116
		if (isset ( self::$config ['di'] )) {
117
			$di = self::$config ['di'];
118 6
			if (\is_array ( $di )) {
119
				self::$actionParams = array_merge ( self::$actionParams, $di );
120 1
			}
121 1
		}
122 1
		call_user_func_array ( $u [0], self::$actionParams );
123 1
	}
124
125
	public static function injectDependences($controller) {
126 5
		if (isset ( self::$config ['di'] )) {
127 5
			$di = self::$config ['di'];
128
			if (\is_array ( $di )) {
129 5
				foreach ( $di as $k => $v ) {
130 3
					$controller->$k = $v ( $controller );
131 3
				}
132 3
			}
133 2
		}
134
	}
135 2
136 2
	public static function runAsString(array &$u, $initialize = true, $finalize = true) {
137
		\ob_start ();
138
		self::runAction ( $u, $initialize, $finalize );
139
		return \ob_get_clean ();
140 2
	}
141
142
	private static function callController(Controller $controller, array &$u) {
143 1
		$urlSize = sizeof ( $u );
144 1
		switch ($urlSize) {
145
			case 1 :
146 5
				$controller->index ();
147
				break;
148
			case 2 :
149
				$action = $u [1];
150
				// Appel de la méthode (2ème élément du tableau)
151
				if (\method_exists ( $controller, $action )) {
152
					$controller->$action ();
153
				} else {
154
					Logger::warn ( "Startup", "The method `{$action}` doesn't exists on controller `" . $u [0] . "`", "callController" );
155
				}
156
				break;
157
			default :
158
				// Appel de la méthode en lui passant en paramètre le reste du tableau
159
				\call_user_func_array ( array ($controller,$u [1] ), self::$actionParams );
160
				break;
161 1
		}
162 1
	}
163
164
	public static function errorHandler($message = "", $code = 0, $severity = 1, $filename = null, int $lineno = 0, $previous = NULL) {
165 1
		if (\error_reporting () == 0) {
166 1
			return;
167
		}
168
		if (\error_reporting () & $severity) {
169 1
			throw new \ErrorException ( $message, 0, $severity, $filename, $lineno, $previous );
170 1
		}
171
	}
172
173 1
	public static function getController() {
174 1
		return self::$controller;
175
	}
176
177 1
	public static function getControllerSimpleName() {
178 1
		return (new \ReflectionClass ( self::$controller ))->getShortName ();
179
	}
180
181 1
	public static function getViewNameFileExtension() {
182 1
		return "html";
183
	}
184
185 1
	public static function getAction() {
186 1
		return self::$action;
187
	}
188
189
	public static function getActionParams() {
190
		return self::$actionParams;
191
	}
192
193
	public static function getFrameworkDir() {
194
		return \dirname ( __FILE__ );
195
	}
196
197
	public static function getApplicationDir() {
198
		return \dirname ( \ROOT );
199
	}
200
201
	public static function getApplicationName() {
202
		return basename ( \dirname ( \ROOT ) );
203
	}
204
}
205