Test Failed
Pull Request — master (#97)
by Gildonei
03:38
created

StartupAsync::runAction()   C

Complexity

Conditions 12
Paths 42

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 30
cp 0
rs 6.9666
cc 12
nc 42
nop 3
crap 156

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
namespace Ubiquity\controllers;
4
5
use Ubiquity\log\Logger;
6
7
/**
8
 * Startup for async platforms (Swoole, Workerman, php-pm...)
9
 * Ubiquity\controllers$StartupAsync
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.0
14
 *
15
 */
16
class StartupAsync extends Startup {
17
	private const IS_VALID = 1;
18
	private const INITIALIZE = 2;
19
	private const FINALIZE = 4;
20
	private static $controllers = [ ];
21
22
	public static function runAction(array &$u, $initialize = true, $finalize = true): void {
23
		self::$controller = $ctrl = $u [0];
24
		self::$action = $action = $u [1] ?? 'index';
25
		self::$actionParams = \array_slice ( $u, 2 );
26
27
		try {
28
			if (null !== $controller = self::getControllerInstance ( $ctrl )) {
29
				$binaryCalls = $controller->_binaryCalls ?? (self::IS_VALID + self::INITIALIZE + self::FINALIZE);
30
				if (($binaryCalls & self::IS_VALID) && ! $controller->isValid ( $action )) {
31
					$controller->onInvalidControl ();
32
				} else {
33
					if (($binaryCalls & self::INITIALIZE) && $initialize) {
34
						$controller->initialize ();
35
					}
36
					try {
37
						$controller->$action ( ...(self::$actionParams) );
38
					} catch ( \Error $e ) {
39
						Logger::warn ( 'Startup', $e->getTraceAsString (), 'runAction' );
40
						if (self::$config ['debug']) {
41
							throw $e;
42
						}
43
					}
44
					if (($binaryCalls & self::FINALIZE) && $finalize) {
45
						$controller->finalize ();
46
					}
47
				}
48
			}
49
		} catch ( \Error $eC ) {
50
			Logger::warn ( 'Startup', $eC->getTraceAsString (), 'runAction' );
51
			if (self::$config ['debug']) {
52
				throw $eC;
53
			}
54
		}
55
	}
56
57
	public static function getControllerInstance($controllerName): ?object {
58
		if (! isset ( self::$controllers [$controllerName] )) {
59
			return self::$controllers [$controllerName] = self::_getControllerInstance ( $controllerName );
60
		}
61
		return self::$controllers [$controllerName];
62
	}
63
}
64
65