Passed
Push — master ( d2008b...aa9f10 )
by Atanas
01:54
created

RouteHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 15
cts 15
cp 1
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get() 0 3 1
A execute() 0 18 4
1
<?php
2
3
namespace WPEmerge\Routing;
4
5
use WPEmerge\Facades\Response;
6
use WPEmerge\Helpers\Handler;
7
use WPEmerge\Responses\ConvertibleToResponseInterface;
8
9
/**
10
 * Represent a Closure or a controller method to be executed in response to a request
11
 */
12
class RouteHandler {
13
	/**
14
	 * Actual handler
15
	 *
16
	 * @var Handler
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param string|\Closure $handler
24
	 */
25 1
	public function __construct( $handler ) {
26 1
		$this->handler = new Handler( $handler );
27 1
	}
28
29
	/**
30
	 * Get the handler
31
	 *
32
	 * @return Handler
33
	 */
34 1
	public function get() {
35 1
		return $this->handler;
36
	}
37
38
	/**
39
	 * Execute the handler
40
	 *
41
	 * @param  mixed $arguments,...
42
	 * @return mixed
43
	 */
44 4
	public function execute() {
45 4
		$arguments = func_get_args();
46 4
		$response = call_user_func_array( [$this->handler, 'execute'], $arguments );
47
48 4
		if ( is_string( $response ) ) {
49 1
			return Response::output( $response );
50
		}
51
52 3
		if ( is_array( $response ) ) {
53 1
			return Response::json( $response );
54
		}
55
56 2
		if ( $response instanceof ConvertibleToResponseInterface ) {
57 1
			return $response->toResponse();
58
		}
59
60 1
		return $response;
61
	}
62
}
63