Completed
Pull Request — master (#4)
by Michael
03:32
created

Stack   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 75
ccs 12
cts 15
cp 0.8
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A append() 0 4 1
A prepend() 0 4 1
A process() 0 6 1
A dispatch() 0 6 1
1
<?php
2
3
namespace Equip\Dispatch;
4
5
use Interop\Http\Middleware\DelegateInterface;
6
use Interop\Http\Middleware\ServerMiddlewareInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class Stack implements ServerMiddlewareInterface
11
{
12
    /**
13
     * @var callable
14
     */
15
    private $default;
16
17
    /**
18
     * @var array
19
     */
20
    private $middleware = [];
21
22
    /**
23
     * @param array $middleware
24
     */
25 3
    public function __construct(...$middleware)
26
    {
27 3
        array_map([$this, 'append'], $middleware);
28 3
    }
29
30
    /**
31
     * Add a middleware to the end of the stack.
32
     *
33
     * @param ServerMiddlewareInterface $middleware
34
     *
35
     * @return void
36
     */
37 2
    public function append(ServerMiddlewareInterface $middleware)
38
    {
39 2
        array_push($this->middleware, $middleware);
40 2
    }
41
42
    /**
43
     * Add a middleware to the beginning of the stack.
44
     *
45
     * @param ServerMiddlewareInterface $middleware
46
     *
47
     * @return void
48
     */
49 1
    public function prepend(ServerMiddlewareInterface $middleware)
50
    {
51 1
        array_unshift($this->middleware, $middleware);
52 1
    }
53
54
    /**
55
     * Process an incoming server request and return a response, optionally delegating
56
     * to the next middleware component to create the response.
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param DelegateInterface $nextContanierDelegate
60
     *
61
     * @return ResponseInterface
62
     */
63
    public function process(ServerRequestInterface $request, DelegateInterface $nextContanierDelegate)
64
    {
65
        $delegate = new Delegate($this->middleware, new DelegateToCallableAdapter($nextContanierDelegate));
66
67
        return $delegate->process($request);
68
    }
69
70
    /**
71
     * Dispatch the middleware stack.
72
     *
73
     * @param ServerRequestInterface $request
74
     * @param callable $default to call when no middleware is available
75
     *
76
     * @return ResponseInterface
77
     */
78 2
    public function dispatch(ServerRequestInterface $request, callable $default)
79
    {
80 2
        $delegate = new Delegate($this->middleware, $default);
81
82 2
        return $delegate->process($request);
83
    }
84
}
85