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

ApplicationStrategy::getNotFoundDecorator()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
     * [email protected]}
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
     * [email protected]}
51
     */
52 3
    public function getNotFoundDecorator(NotFoundException $exception)
53
    {
54 3
        throw $exception;
55
    }
56
57
    /**
58
     * [email protected]}
59
     */
60 3
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception)
61
    {
62 3
        throw $exception;
63
    }
64
65
    /**
66
     * [email protected]}
67
     */
68 3
    public function getExceptionDecorator(Exception $exception)
69
    {
70 3
        throw $exception;
71
    }
72
}
73