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

ApplicationStrategy::getExecutionChain()   B

Complexity

Conditions 3
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 29
Code Lines 15

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.8571
cc 3
eloc 15
nc 2
nop 2
crap 3
1
<?php
2
3
namespace League\Route\Strategy;
4
5
use League\Route\Middleware\ExecutionChain;
6
use League\Route\Route;
7
use RuntimeException;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class ApplicationStrategy implements StrategyInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getExecutionChain(Route $route, array $vars)
17
    {
18 9
        $middleware = function (
19
            ServerRequestInterface $request, ResponseInterface $response, callable $next
20
        ) use (
21 9
            $route, $vars
22
        ) {
23 9
            $response = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
24
25 9
            if (! $response instanceof ResponseInterface) {
26 3
                throw new RuntimeException(
27
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
28 3
                );
29
            }
30
31 6
            return $next($request, $response);
32 9
        };
33
34 9
        $execChain = (new ExecutionChain)->middleware($middleware);
35
36
        // ensure middleware is executed in the order it was added
37 9
        $stack = array_reverse($route->getMiddlewareStack());
38
39 9
        foreach ($stack as $middleware) {
40 3
            $execChain->middleware($middleware);
41 9
        }
42
43 9
        return $execChain;
44
    }
45
}
46