Completed
Push — master ( 73a483...7cce89 )
by Woody
03:00
created

Stack   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 54
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A append() 0 4 1
A prepend() 0 4 1
A dispatch() 0 6 1
1
<?php
2
3
namespace Equip\Dispatch;
4
5
use Interop\Http\Middleware\ServerMiddlewareInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class Stack
10
{
11
    /**
12
     * @var array
13
     */
14
    private $middleware;
15
16
    /**
17
     * @param array $middleware
18
     */
19 3
    public function __construct(array $middleware = [])
20
    {
21 3
        $this->middleware = $middleware;
22 3
    }
23
24
    /**
25
     * Add a middleware to the end of the stack.
26
     *
27
     * @param ServerMiddlewareInterface $middleware
28
     *
29
     * @return void
30
     */
31 1
    public function append(ServerMiddlewareInterface $middleware)
32
    {
33 1
        array_push($this->middleware, $middleware);
34 1
    }
35
36
    /**
37
     * Add a middleware to the beginning of the stack.
38
     *
39
     * @param ServerMiddlewareInterface $middleware
40
     *
41
     * @return void
42
     */
43 1
    public function prepend(ServerMiddlewareInterface $middleware)
44
    {
45 1
        array_unshift($this->middleware, $middleware);
46 1
    }
47
48
    /**
49
     * Dispatch the middleware stack.
50
     *
51
     * @param ServerRequestInterface $request
52
     * @param callable $default to call when no middleware is available
53
     *
54
     * @return ResponseInterface
55
     */
56 2
    public function dispatch(ServerRequestInterface $request, callable $default)
57
    {
58 2
        $delegate = new Delegate($this->middleware, $default);
59
60 2
        return $delegate->process($request);
61
    }
62
}
63