Completed
Push — master ( 723741...e2f01d )
by smiley
02:37
created

QueueRequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QueueRequestHandler
4
 *
5
 * @link https://github.com/libreworks/caridea-dispatch
6
 *
7
 * @filesource   QueueRequestHandler.php
8
 * @created      08.03.2019
9
 * @package      chillerlan\HTTP\Psr15
10
 * @author       smiley <[email protected]>
11
 * @copyright    2019 smiley
12
 * @license      MIT
13
 */
14
15
namespace chillerlan\HTTP\Psr15;
16
17
use chillerlan\HTTP\Psr15\Middleware\MiddlewareException;
18
use chillerlan\HTTP\Psr17\ResponseFactory;
19
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
20
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
21
22
class QueueRequestHandler implements MiddlewareInterface, RequestHandlerInterface{
23
24
	/**
25
	 * @var \Psr\Http\Server\MiddlewareInterface[]
26
	 */
27
	protected $middlewareStack = [];
28
29
	/**
30
	 * @var \Psr\Http\Server\RequestHandlerInterface
31
	 */
32
	protected $fallbackHandler;
33
34
	/**
35
	 * QueueRequestHandler constructor.
36
	 *
37
	 * @param iterable|null                                 $middlewareStack
38
	 * @param \Psr\Http\Server\RequestHandlerInterface|null $fallbackHandler
39
	 */
40
	public function __construct(iterable $middlewareStack = null, RequestHandlerInterface $fallbackHandler = null){
41
		$this->fallbackHandler = $fallbackHandler ?? new EmptyResponseHandler(new ResponseFactory, 500);
42
43
		$this->addStack($middlewareStack ?? []);
0 ignored issues
show
Bug introduced by
It seems like $middlewareStack ?? array() can also be of type array; however, chillerlan\HTTP\Psr15\Qu...uestHandler::addStack() does only seem to accept object<chillerlan\HTTP\P...r\MiddlewareInterface>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
	}
45
46
	/**
47
	 * @param iterable|\Psr\Http\Server\MiddlewareInterface[] $middlewareStack
48
	 *
49
	 * @return \chillerlan\HTTP\Psr15\QueueRequestHandler
50
	 * @throws \Exception
51
	 */
52
	public function addStack(iterable $middlewareStack):QueueRequestHandler{
53
54
		foreach($middlewareStack as $middleware){
55
56
			if(!$middleware instanceof MiddlewareInterface){
57
				throw new MiddlewareException('invalid middleware');
58
			}
59
60
			$this->middlewareStack[] = $middleware;
61
		}
62
63
		return $this;
64
	}
65
66
	/**
67
	 * @param \Psr\Http\Server\MiddlewareInterface $middleware
68
	 *
69
	 * @return \chillerlan\HTTP\Psr15\QueueRequestHandler
70
	 */
71
	public function add(MiddlewareInterface $middleware):QueueRequestHandler{
72
		$this->middlewareStack[] = $middleware;
73
74
		return $this;
75
	}
76
77
	/**
78
	 * @inheritDoc
79
	 */
80
	public function handle(ServerRequestInterface $request):ResponseInterface{
81
		return (new QueueRunner($this->middlewareStack, $this->fallbackHandler))->handle($request);
82
	}
83
84
	/**
85
	 * @inheritDoc
86
	 */
87
	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler):ResponseInterface{
88
		return (new QueueRunner($this->middlewareStack, $handler))->handle($request);
89
	}
90
91
}
92