Passed
Push — master ( 43db54...cc5a21 )
by Atanas
02:03
created

Handler::executeHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
1
<?php
2
3
namespace CarbonFramework\Routing;
4
5
use Closure;
6
use Exception;
7
use CarbonFramework\Framework;
8
9
/**
10
 * Represent a callable or a controller method to be executed in response to a request
11
 */
12
class Handler {
13
	/**
14
	 * Actual handler
15
	 *
16
	 * @var callable|array|null
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param string|callable $handler
24
	 */
25
	public function __construct( $handler ) {
26
		$this->set( $handler );
27
	}
28
29
	/**
30
	 * Parse a handler to a callable or a [class, method] array
31
	 *
32
	 * @param  string|callable     $handler
33
	 * @return callable|array|null
34
	 */
35
	protected function parse( $handler ) {
36
		if ( $handler instanceof Closure ) {
37
			return $handler;
38
		}
39
40
		if ( is_string( $handler ) )  {
41
			return $this->parseFromString( $handler );
42
		}
43
44
		return null;
45
	}
46
47
	/**
48
	 * Parse a string handler to a callable or a [class, method] array
49
	 *
50
	 * @param  string              $handler
51
	 * @return callable|array|null
52
	 */
53
	protected function parseFromString( $handler ) {
54
		$handlerPieces = preg_split( '/@|::/', $handler, 2 );
55
56
		if ( count( $handlerPieces ) === 1 ) {
57
			if ( is_callable( $handlerPieces[0] ) ) {
58
				return $handlerPieces[0];
59
			}
60
			return null;
61
		}
62
63
		return array(
64
			'class' => $handlerPieces[0],
65
			'method' => $handlerPieces[1],
66
		);
67
	}
68
69
	/**
70
	 * Execute the handler returning raw result
71
	 *
72
	 * @return string|array|\Psr\Http\Message\ResponseInterface
73
	 */
74
	protected function executeHandler() {
75
		$arguments = func_get_args();
76
		if ( is_callable( $this->handler ) ) {
77
			return call_user_func_array( $this->handler, $arguments );
78
		}
79
80
		$class = $this->handler['class'];
81
		$method = $this->handler['method'];
82
83
		$controller = Framework::instantiate( $class );
84
		return call_user_func_array( [$controller, $method], $arguments );
85
	}
86
87
	/**
88
	 * Set the handler
89
	 *
90
	 * @param  string|callable $new_handler
91
	 * @return null
92
	 */
93
	public function set( $new_handler ) {
94
		$handler = $this->parse( $new_handler );
95
96
		if ( $handler === null ) {
97
			throw new Exception( 'No or invalid handler provided.' );
98
		}
99
100
		$this->handler = $handler;
101
	}
102
103
	/**
104
	 * Execute the handler
105
	 *
106
	 * @return \Psr\Http\Message\ResponseInterface
107
	 */
108
	public function execute() {
109
		$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() );
110
111
		if ( is_string( $response ) ) {
112
			return cf_output( $response );
113
		}
114
115
		if ( is_array( $response ) ) {
116
			return cf_json( $response );
117
		}
118
119
		return $response;
120
	}
121
}
122