Passed
Push — master ( 627bcb...9608fc )
by Jean-Christophe
05:42
created

RouterAdminTrait::getRouteInfoByControllerAction()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.3906

Importance

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