Passed
Push — master ( 4f9d14...27c065 )
by Atanas
01:47
created

Router   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 102
ccs 29
cts 29
cp 1
rs 10
c 1
b 0
f 1
wmc 12
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A registerRewriteRules() 0 6 2
A execute() 0 18 4
A getCurrentRoute() 0 3 1
A setCurrentRoute() 0 3 1
A handle() 0 16 3
1
<?php
2
3
namespace Obsidian\Routing;
4
5
use Exception;
6
use Psr\Http\Message\ResponseInterface;
7
use Obsidian\Framework;
8
use Obsidian\Request;
9
use Obsidian\Response as FrameworkResponse;
10
11
/**
12
 * Provide routing
13
 */
14
class Router implements HasRoutesInterface {
15
	use HasRoutesTrait;
16
17
	/**
18
	 * Current active route
19
	 *
20
	 * @var RouteInterface
21
	 */
22
	protected $current_route = null;
23
24
	/**
25
	 * Hook into WordPress actions
26
	 *
27
	 * @codeCoverageIgnore
28
	 * @return void
29
	 */
30
	public function boot() {
31
		add_action( 'init', array( $this, 'registerRewriteRules' ), 1000 );
32
		add_action( 'template_include', array( $this, 'execute' ), 1000 );
33
	}
34
35
	/**
36
	 * Register route rewrite rules with WordPress
37
	 *
38
	 * @codeCoverageIgnore
39
	 * @return void
40
	 */
41
	public function registerRewriteRules() {
42
		$rules = apply_filters( 'obsidian.routing.rewrite_rules', [] );
43
		foreach ( $rules as $rule => $rewrite_to ) {
44
			add_rewrite_rule( $rule, $rewrite_to, 'top' );
45
		}
46
	}
47
48
	/**
49
	 * Add global middlewares and execute the first satisfied route (if any)
50
	 *
51
	 * @param  string $template
52
	 * @return string
53
	 */
54 7
	public function execute( $template ) {
55 7
		$routes = $this->getRoutes();
56 7
		$global_middleware = Framework::resolve( 'framework.routing.global_middleware' );
57 7
		$request = Request::fromGlobals();
58
59 7
		foreach ( $routes as $route ) {
60 7
			$route->addMiddleware( $global_middleware );
61 7
		}
62
63 7
		foreach ( $routes as $route ) {
64 7
			if ( $route->satisfied( $request ) ) {
65 5
				$this->setCurrentRoute( $route );
66 5
				return $this->handle( $request, $route, $template );
67
			}
68 2
		}
69
70 2
		return $template;
71
	}
72
73
	/**
74
	 * Execute a route
75
	 *
76
	 * @param  Request        $request
77
	 * @param  RouteInterface $route
78
	 * @param  string         $template
79
	 * @return string
80
	 */
81 4
	protected function handle( Request $request, RouteInterface $route, $template ) {
82 4
		$response = $route->handle( $request, $template );
83
84 4
		if ( ! is_a( $response, ResponseInterface::class ) ) {
85 2
			if ( Framework::debugging() ) {
86 1
				throw new Exception( 'Response returned by controller is not valid (expectected ' . ResponseInterface::class . '; received ' . gettype( $response ) . ').' );
87
			}
88 1
			$response = FrameworkResponse::error( FrameworkResponse::response(), 500 );
89 1
		}
90
91 3
		add_filter( 'obsidian.response', function() use ( $response ) {
92 2
			return $response;
93 3
		} );
94
95 3
		return OBSIDIAN_DIR . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'template.php';
96
	}
97
98
	/**
99
	 * Get the current route
100
	 *
101
	 * @return RouteInterface
102
	 */
103 1
	public function getCurrentRoute() {
104 1
		return $this->current_route;
105
	}
106
107
	/**
108
	 * Set the current route
109
	 *
110
	 * @param RouteInterface
111
	 */
112 1
	public function setCurrentRoute( RouteInterface $current_route ) {
113 1
		$this->current_route = $current_route;
114 1
	}
115
}
116