Completed
Push — master ( e81b92...349f3b )
by Phil
58:34 queued 08:42
created

ApplicationStrategy::throwThrowableMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 5
cts 5
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ApplicationStrategy.php$0 ➔ __construct() 0 4 1
A ApplicationStrategy.php$0 ➔ process() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace League\Route\Strategy;
4
5
use League\Route\{ContainerAwareInterface, ContainerAwareTrait};
6
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
7
use League\Route\Route;
8
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
9
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
10
use Throwable;
11
12
class ApplicationStrategy extends AbstractStrategy implements ContainerAwareInterface
13
{
14
    use ContainerAwareTrait;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 8
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request) : ResponseInterface
20
    {
21 8
        $controller = $route->getCallable($this->getContainer());
22
23 8
        $response = $controller($request, $route->getVars());
24 6
        $response = $this->applyDefaultResponseHeaders($response);
25
26 6
        return $response;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 12
    public function getNotFoundDecorator(NotFoundException $exception) : MiddlewareInterface
33
    {
34 12
        return $this->throwThrowableMiddleware($exception);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 4
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception) : MiddlewareInterface
41
    {
42 4
        return $this->throwThrowableMiddleware($exception);
43
    }
44
45
    /**
46
     * Return a middleware that simply throws an error
47
     *
48
     * @param \Throwable $error
49
     *
50
     * @return \Psr\Http\Server\MiddlewareInterface
51
     */
52
    protected function throwThrowableMiddleware(Throwable $error) : MiddlewareInterface
53
    {
54
        return new class($error) implements MiddlewareInterface
55
        {
56
            protected $error;
57
58 16
            public function __construct(Throwable $error)
59
            {
60 16
                $this->error = $error;
61 16
            }
62
63 16
            public function process(
64
                ServerRequestInterface $request,
65
                RequestHandlerInterface $requestHandler
66
            ) : ResponseInterface {
67 16
                throw $this->error;
68
            }
69
        };
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 8
    public function getExceptionHandler() : MiddlewareInterface
76
    {
77 8
        return $this->getThrowableHandler();
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getThrowableHandler() : MiddlewareInterface
84
    {
85
        return new class implements MiddlewareInterface
86
        {
87
            /**
88
             * {@inheritdoc}
89
             */
90 8
            public function process(
91
                ServerRequestInterface $request,
92
                RequestHandlerInterface $requestHandler
93
            ) : ResponseInterface {
94
                try {
95 8
                    return $requestHandler->handle($request);
96 4
                } catch (Throwable $e) {
97 4
                    throw $e;
98
                }
99
            }
100
        };
101
    }
102
}
103