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

ApplicationStrategy   A

Coupling/Cohesion

Components 0
Dependencies 2

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getExecutionChain() 0 29 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
     * [email protected]}
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