Passed
Push — master ( b7467b...db04eb )
by Atanas
02:41
created

Handler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parse() 0 11 3
A parseFromString() 0 15 3
A executeHandler() 0 12 2
A set() 0 9 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 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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (object|integer|double|null|boolean) is incompatible with the return type documented by Obsidian\Routing\Handler::execute of type Psr\Http\Message\ResponseInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
120
	}
121
}
122