Passed
Push — master ( be71ba...d26f6c )
by Jean-Christophe
03:55
created

RouterAdminTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 45.16%

Importance

Changes 0
Metric Value
wmc 18
eloc 29
dl 0
loc 51
ccs 14
cts 31
cp 0.4516
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotations() 0 10 5
A filterRoutes() 0 9 3
A getRouteInfo() 0 11 5
A getRouteInfoByControllerAction() 0 11 5
1
<?php
2
3
namespace Ubiquity\controllers\traits;
4
5
/**
6
 * @author jcheron <[email protected]>
7
 * @property array $routes
8
 *
9
 */
10
trait RouterAdminTrait {
11
	
12
	abstract protected static function slashPath($path);
13
	
14 1
	public static function getRouteInfoByControllerAction($controller, $action) {
15 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
16 1
			if (! isset ( $routeDetails ["controller"] )) {
17 1
				$routeDetails = \current ( $routeDetails );
18
			}
19 1
			if ($controller === $routeDetails ["controller"] && $action === $routeDetails ["action"]) {
20
				$routeDetails ["path"] = $routePath;
21
				return $routeDetails;
22
			}
23
		}
24 1
		return false;
25
	}
26
	
27
	public static function getRouteInfo($path) {
28
		$path = self::slashPath ( $path );
29
		foreach ( self::$routes as $routePath => $routeDetails ) {
30
			if (preg_match ( "@^" . $routePath . "$@s", $path, $matches ) || \stripslashes ( $routePath ) == $path) {
31
				if (! isset ( $routeDetails ["controller"] )) {
32
					return \current ( $routeDetails );
33
				} else
34
					return $routeDetails;
35
			}
36
		}
37
		return false;
38
	}
39
	
40 1
	public static function getAnnotations($controllerName, $actionName) {
41 1
		$result = [ ];
42 1
		foreach ( self::$routes as $routePath => $routeDetails ) {
43 1
			if (! isset ( $routeDetails ["controller"] )) {
44 1
				$routeDetails = \current ( $routeDetails );
45
			}
46 1
			if ($routeDetails ["controller"] === $controllerName && $routeDetails ["action"] === $actionName)
47 1
				$result [$routePath] = $routeDetails;
48
		}
49 1
		return $result;
50
	}
51
	
52
	public static function filterRoutes($path) {
53
		$path = self::slashPath ( $path );
54
		$result = [ ];
55
		foreach ( self::$routes as $routePath => $routeDetails ) {
56
			if (preg_match ( "@^" . $routePath . ".*?$@s", $path, $matches )) {
57
				$result [$routePath] = $routeDetails;
58
			}
59
		}
60
		return $result;
61
	}
62
}
63
64