Completed
Push — master ( 6e909e...c441c1 )
by Phil
03:17
created

ExecutionChain::execute()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 5
Code Lines 3

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace League\Route\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class ExecutionChain implements StackAwareInterface
9
{
10
    use StackAwareTrait;
11
12
    /**
13
     * Build and execute the chain.
14
     *
15
     * @param \Psr\Http\Message\ServerRequestInterface $request
16
     * @param \Psr\Http\Message\ResponseInterface      $response
17
     *
18
     * @return \Psr\Http\Message\ResponseInterface
19
     */
20 33
    public function execute(ServerRequestInterface $request, ResponseInterface $response)
21
    {
22 33
        $chain = $this->buildExecutionChain();
23 33
        return $chain($request, $response);
24
    }
25
26
    /**
27
     * Build an execution chain.
28
     *
29
     * @return callable
30
     */
31 33
    protected function buildExecutionChain()
32
    {
33 33
        $stack = $this->getMiddlewareStack();
34
35
        $next = function (ServerRequestInterface $request, ResponseInterface $response) {
36 12
            return $response;
37 33
        };
38
39 33
        foreach ($stack as $middleware) {
40 33
            $next = function (ServerRequestInterface $request, ResponseInterface $response) use ($middleware, $next) {
41 33
                return $middleware($request, $response, $next);
42 33
            };
43 33
        }
44
45 33
        return $next;
46
    }
47
}
48