QueueRunner::getMiddleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QueueRunner
4
 *
5
 * @link         https://www.php-fig.org/psr/psr-15/meta/
6
 *
7
 * @created      10.03.2019
8
 * @author       smiley <[email protected]>
9
 * @copyright    2019 smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\HTTP\Psr15;
14
15
use Psr\Http\Server\MiddlewareInterface;
16
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
17
use Psr\Http\Server\RequestHandlerInterface;
18
19
use function array_pop;
20
21
class QueueRunner implements RequestHandlerInterface{
22
23
	/** @var \Psr\Http\Server\MiddlewareInterface[] */
24
	protected array                   $middlewareStack;
25
	protected RequestHandlerInterface $fallbackHandler;
26
27
	/**
28
	 *  constructor.
29
	 *
30
	 * @param \Psr\Http\Server\MiddlewareInterface[]   $middlewareStack
31
	 * @param \Psr\Http\Server\RequestHandlerInterface $fallbackHandler
32
	 */
33
	public function __construct(array $middlewareStack, RequestHandlerInterface $fallbackHandler){
34
		$this->middlewareStack = $middlewareStack;
35
		$this->fallbackHandler = $fallbackHandler;
36
	}
37
38
	/**
39
	 * @inheritDoc
40
	 */
41
	public function handle(ServerRequestInterface $request):ResponseInterface{
42
43
		if(empty($this->middlewareStack)){
44
			return $this->fallbackHandler->handle($request);
45
		}
46
47
		return $this->getMiddleware()->process($request, $this);
48
	}
49
50
	/**
51
	 *
52
	 */
53
	protected function getMiddleware():MiddlewareInterface{
54
		return array_pop($this->middlewareStack);
55
	}
56
57
}
58