Passed
Push — master ( a8bd8b...f2f9a5 )
by Atanas
02:01
created

Handler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 14
cts 15
cp 0.9333
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;
6
use WPEmerge\Helpers\Handler as GenericHandler;
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 Handler {
13
	/**
14
	 * Actual handler
15
	 *
16
	 * @var GenericHandler
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 GenericHandler( $handler );
27 1
	}
28
29
	/**
30
	 * Get the handler
31
	 *
32
	 * @return GenericHandler
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 3
	public function execute() {
45 3
		$arguments = func_get_args();
46 3
		$response = call_user_func_array( [$this->handler, 'execute'], $arguments );
47
48 3
		if ( is_string( $response ) ) {
49 1
			return WPEmerge\output( $response );
50
		}
51
52 2
		if ( is_array( $response ) ) {
53 1
			return WPEmerge\json( $response );
54
		}
55
56 1
		if ( $response instanceof ConvertibleToResponseInterface ) {
57
			return $response->toResponse();
58
		}
59
60 1
		return $response;
61
	}
62
}
63