Completed
Pull Request — master (#115)
by Phil
02:51
created

ApplicationStrategy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B getExecutionChain() 0 26 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
        $middleware = function (
19
            ServerRequestInterface $request, ResponseInterface $response, callable $next
20
        ) use (
21
            $route, $vars
22
        ) {
23
            $response = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
24
25
            if (! $response instanceof ResponseInterface) {
26
                throw new RuntimeException(
27
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
28
                );
29
            }
30
31
            return $next($request, $response);
32
        };
33
34
        $execChain = (new ExecutionChain)->middleware($middleware);
35
36
        foreach ($route->getMiddlewareStack() as $middleware) {
37
            $execChain->middleware($middleware);
38
        }
39
40
        return $execChain;
41
    }
42
}
43