Passed
Push — master ( fe2b7e...40b1d2 )
by Jean-Christophe
12:27
created

StartupAsync::runAction()   C

Complexity

Conditions 13
Paths 66

Size

Total Lines 42
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 33
c 3
b 0
f 0
dl 0
loc 42
ccs 0
cts 41
cp 0
rs 6.6166
cc 13
nc 66
nop 3
crap 182

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, Roadrunner, php-pm...)
9
 * Ubiquity\controllers$StartupAsync
10
 * This class is part of Ubiquity
11
 *
12
 * @author jcheron <[email protected]>
13
 * @version 1.0.2
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
						if (! \method_exists ( $controller, $action )) {
40
							static::onError ( 404, "This action does not exist on the controller " . $ctrl, $controller );
41
						} else {
42
							Logger::warn ( 'Startup', $e->getTraceAsString (), 'runAction' );
43
							if (self::$config ['debug']) {
44
								throw $e;
45
							} else {
46
								static::onError ( 500, $e->getMessage (), $controller );
47
							}
48
						}
49
					}
50
					if (($binaryCalls & self::FINALIZE) && $finalize) {
51
						$controller->finalize ();
52
					}
53
				}
54
			} else {
55
				Logger::warn ( 'Startup', 'The controller `' . $ctrl . '` doesn\'t exists! <br/>', 'runAction' );
56
				static::onError ( 404 );
57
			}
58
		} catch ( \Error $eC ) {
59
			Logger::warn ( 'Startup', $eC->getTraceAsString (), 'runAction' );
60
			if (self::$config ['debug']) {
61
				throw $eC;
62
			} else {
63
				static::onError ( 500, $eC->getMessage () );
64
			}
65
		}
66
	}
67
68
	public static function getControllerInstance($controllerName): ?object {
69
		return self::$controllers [$controllerName] ??= self::_getControllerInstance ( $controllerName );
70
	}
71
72
	public static function warmupAction($controller, $action = 'index') {
73
		ob_start ();
74
		$ru = [ $controller,$action ];
75
		static::runAction ( $ru, true, true );
76
		ob_end_clean ();
77
	}
78
}
79