Passed
Push — master ( b4e0eb...a168fa )
by Atanas
01:42
created

Handler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 116
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 11 3
A parseFromString() 0 12 2
A get() 0 3 1
A set() 0 9 2
A executeHandler() 0 12 2
A execute() 0 13 3
1
<?php
2
3
namespace Obsidian\Routing;
4
5
use Closure;
6
use Exception;
7
use Obsidian\Framework;
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 array|\Closure|null
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->set( $handler );
27 1
	}
28
29
	/**
30
	 * Parse a handler to a \Closure or a [class, method] array
31
	 *
32
	 * @param  string|\Closure     $handler
33
	 * @return array|\Closure|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Closure|array|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
34
	 */
35 5
	protected function parse( $handler ) {
36 5
		if ( $handler instanceof Closure ) {
37 5
			return $handler;
38
		}
39
40 4
		if ( is_string( $handler ) )  {
41 3
			return $this->parseFromString( $handler );
42
		}
43
44 1
		return null;
45
	}
46
47
	/**
48
	 * Parse a string handler to a [class, method] array
49
	 *
50
	 * @param  string              $handler
51
	 * @return array|\Closure|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
52
	 */
53 3
	protected function parseFromString( $handler ) {
54 3
		$handlerPieces = preg_split( '/@|::/', $handler, 2 );
55
56 3
		if ( count( $handlerPieces ) === 2 ) {
57
			return array(
58 2
				'class' => $handlerPieces[0],
59 2
				'method' => $handlerPieces[1],
60 2
			);
61
		}
62
63 1
		return null;
64
	}
65
66
	/**
67
	 * Get the handler
68
	 *
69
	 * @return array|\Closure|null
70
	 */
71 1
	public function get() {
72 1
		return $this->handler;
73
	}
74
75
	/**
76
	 * Set the handler
77
	 *
78
	 * @param  string|\Closure $new_handler
79
	 * @return null
80
	 */
81 6
	public function set( $new_handler ) {
82 6
		$handler = $this->parse( $new_handler );
83
84 6
		if ( $handler === null ) {
85 2
			throw new Exception( 'No or invalid handler provided.' );
86
		}
87
88 6
		$this->handler = $handler;
89 6
	}
90
91
	/**
92
	 * Execute the handler returning raw result
93
	 *
94
	 * @return string|array|\Psr\Http\Message\ResponseInterface
95
	 */
96 2
	protected function executeHandler() {
97 2
		$arguments = func_get_args();
98 2
		if ( is_a( $this->handler, Closure::class ) ) {
99 1
			return call_user_func_array( $this->handler, $arguments );
100
		}
101
102 1
		$class = $this->handler['class'];
103 1
		$method = $this->handler['method'];
104
105 1
		$controller = Framework::instantiate( $class );
106 1
		return call_user_func_array( [$controller, $method], $arguments );
107
	}
108
109
	/**
110
	 * Execute the handler
111
	 *
112
	 * @return \Psr\Http\Message\ResponseInterface
113
	 */
114 5
	public function execute() {
115 5
		$response = call_user_func_array( [$this, 'executeHandler'], func_get_args() );
116
117 5
		if ( is_string( $response ) ) {
118 1
			return obs_output( $response );
119
		}
120
121 4
		if ( is_array( $response ) ) {
122 1
			return obs_json( $response );
123
		}
124
125 3
		return $response;
126
	}
127
}
128