Passed
Push — master ( c89772...da5ca0 )
by Atanas
01:54
created

Handler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Helpers;
4
5
use Closure;
6
use WPEmerge\Exceptions\Exception;
7
use WPEmerge\Facades\Framework;
8
9
/**
10
 * Represent a generic handler - a Closure or a class method to be resolved from the service container
11
 */
12
class Handler {
13
	/**
14
	 * Parsed handler
15
	 *
16
	 * @var array|Closure|null
17
	 */
18
	protected $handler = null;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @throws Exception
24
	 * @param string|Closure $raw_handler
25
	 * @param string|null    $default_method
26
	 */
27
	public function __construct( $raw_handler, $default_method = null ) {
28
		$handler = $this->parse( $raw_handler, $default_method );
29
30
		if ( $handler === null ) {
31
			throw new Exception( 'No or invalid handler provided.' );
32
		}
33
34
		$this->handler = $handler;
35
	}
36
37
	/**
38
	 * Parse a raw handler to a Closure or a [class, method] array
39
	 *
40
	 * @param  string|Closure     $raw_handler
41
	 * @param  string|null        $default_method
42
	 * @return array|Closure|null
43
	 */
44
	protected function parse( $raw_handler, $default_method = null ) {
45
		if ( $raw_handler instanceof Closure ) {
46
			return $raw_handler;
47
		}
48
49
		return $this->parseFromString( $raw_handler, $default_method );
50
	}
51
52
	/**
53
	 * Parse a raw string handler to a [class, method] array
54
	 *
55
	 * @param  string      $raw_handler
56
	 * @param  string|null $default_method
57
	 * @return array|null
58
	 */
59
	protected function parseFromString( $raw_handler, $default_method = null ) {
60
		$handlerPieces = array_filter( preg_split( '/@|::/', $raw_handler, 2 ) );
61
62
		if ( $default_method !== null && count( $handlerPieces ) === 1 ) {
63
			return [
64
				'class' => $handlerPieces[0],
65
				'method' => $default_method,
66
			];
67
		}
68
69
		if ( count( $handlerPieces ) === 2 ) {
70
			return [
71
				'class' => $handlerPieces[0],
72
				'method' => $handlerPieces[1],
73
			];
74
		}
75
76
		return null;
77
	}
78
79
	/**
80
	 * Get the parsed handler
81
	 *
82
	 * @return array|Closure|null
83
	 */
84
	public function get() {
85
		return $this->handler;
86
	}
87
88
	/**
89
	 * Execute the parsed handler with any provided arguments and return the result
90
	 *
91
	 * @param  mixed ...$arguments
92
	 * @return mixed
93
	 */
94
	public function execute() {
95
		$arguments = func_get_args();
96
		if ( $this->handler instanceof Closure ) {
97
			return call_user_func_array( $this->handler, $arguments );
98
		}
99
100
		$class = $this->handler['class'];
101
		$method = $this->handler['method'];
102
103
		$instance = Framework::instantiate( $class );
104
		return call_user_func_array( [$instance, $method], $arguments );
105
	}
106
}
107