Passed
Branch refactor/kernels (9c85ce)
by Atanas
01:45
created

Pipeline   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 74
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A to() 0 4 1
A setHandler() 0 2 1
A run() 0 3 1
A pipe() 0 4 1
A getHandler() 0 2 1
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing;
11
12
use WPEmerge\Middleware\ExecutesMiddlewareTrait;
13
use WPEmerge\Requests\RequestInterface;
14
15
/**
16
 * Represent middleware that envelops a handler.
17
 */
18
class Pipeline {
19
	use ExecutesMiddlewareTrait;
20
21
	/**
22
	 * Pipeline handler.
23
	 *
24
	 * @var PipelineHandler
25
	 */
26
	protected $handler = null;
27
28
	/**
29
	 * Middleware.
30
	 *
31
	 * @var array<array>
32
	 */
33
	protected $middleware = [];
34
35
	/**
36
	 * Get handler.
37
	 *
38
	 * @codeCoverageIgnore
39
	 * @return PipelineHandler
40
	 */
41
	public function getHandler() {
42
		return $this->handler;
43
	}
44
45
	/**
46
	 * Set handler.
47
	 *
48
	 * @codeCoverageIgnore
49
	 * @param  string|\Closure $handler
50
	 * @return void
51
	 */
52
	public function setHandler( $handler ) {
53
		$this->handler = new PipelineHandler( $handler );
54
	}
55
56
	/**
57
	 * Fluent alias for setHandler().
58
	 *
59
	 * @codeCoverageIgnore
60
	 * @param  string|\Closure $handler
61
	 * @return static          $this
62
	 */
63
	public function to( $handler ) {
64
		call_user_func_array( [$this, 'setHandler'], func_get_args() );
65
66
		return $this;
67
	}
68
69
	/**
70
	 * Add middleware to the pipeline.
71
	 *
72
	 * @codeCoverageIgnore
73
	 * @param  array<array> $middleware
74
	 * @return static       $this
75
	 */
76
	public function pipe( $middleware ) {
77
		$this->middleware = array_merge( $this->middleware, $middleware );
78
79
		return $this;
80
	}
81
82
	/**
83
	 * Get a response for the given request.
84
	 *
85
	 * @param  RequestInterface                    $request
86
	 * @param  array                               $arguments
87
	 * @return \Psr\Http\Message\ResponseInterface
88
	 */
89
	public function run( RequestInterface $request, $arguments ) {
90 1
		return $this->executeMiddleware( $this->middleware, $request, function () use ( $arguments ) {
91 1
			return call_user_func_array( [$this->getHandler(), 'execute'], $arguments );
92 1
		} );
93
	}
94
}
95