Completed
Push — master ( f5bdea...18af5f )
by Phil
14s
created

ApplicationStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getExecutionChain() 0 29 3
A getNotFoundDecorator() 0 4 1
A getMethodNotAllowedDecorator() 0 4 1
A getExceptionDecorator() 0 4 1
1
<?php
2
3
namespace League\Route\Strategy;
4
5
use \Exception;
6
use League\Route\Http\Exception\MethodNotAllowedException;
7
use League\Route\Http\Exception\NotFoundException;
8
use League\Route\Middleware\ExecutionChain;
9
use League\Route\Route;
10
use RuntimeException;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
class ApplicationStrategy implements StrategyInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function getExecutionChain(Route $route, array $vars)
20
    {
21 12
        $middleware = function (
22
            ServerRequestInterface $request, ResponseInterface $response, callable $next
23
        ) use (
24 12
            $route, $vars
25
        ) {
26 12
            $response = call_user_func_array($route->getCallable(), [$request, $response, $vars]);
27
28 9
            if (! $response instanceof ResponseInterface) {
29 3
                throw new RuntimeException(
30
                    'Route callables must return an instance of (Psr\Http\Message\ResponseInterface)'
31 3
                );
32
            }
33
34 6
            return $next($request, $response);
35 12
        };
36
37 12
        $execChain = (new ExecutionChain)->middleware($middleware);
38
39
        // ensure middleware is executed in the order it was added
40 12
        $stack = array_reverse($route->getMiddlewareStack());
41
42 12
        foreach ($stack as $middleware) {
43 3
            $execChain->middleware($middleware);
44 12
        }
45
46 12
        return $execChain;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function getNotFoundDecorator(NotFoundException $exception)
53
    {
54 3
        throw $exception;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
61
    {
62 3
        throw $exception;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 3
    public function getExceptionDecorator(Exception $exception)
69
    {
70 3
        throw $exception;
71
    }
72
}
73