anonymous//src/Strategy/ApplicationStrategy.php$1   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route\Strategy;
6
7
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
8
use League\Route\Route;
9
use League\Route\{ContainerAwareInterface, ContainerAwareTrait};
10
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
11
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
12
use Throwable;
13
14
class ApplicationStrategy extends AbstractStrategy implements ContainerAwareInterface
15
{
16
    use ContainerAwareTrait;
17
18 6
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception): MiddlewareInterface
19
    {
20 6
        return $this->throwThrowableMiddleware($exception);
21
    }
22
23 21
    public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
24
    {
25 21
        return $this->throwThrowableMiddleware($exception);
26
    }
27
28
    public function getThrowableHandler(): MiddlewareInterface
29
    {
30
        return new class implements MiddlewareInterface
31
        {
32 33
            public function process(
33
                ServerRequestInterface $request,
34
                RequestHandlerInterface $handler
35
            ): ResponseInterface {
36
                try {
37 33
                    return $handler->handle($request);
38 6
                } catch (Throwable $e) {
39 6
                    throw $e;
40
                }
41
            }
42
        };
43
    }
44
45 33
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
46
    {
47 33
        $controller = $route->getCallable($this->getContainer());
48 33
        $response = $controller($request, $route->getVars());
49 30
        return $this->decorateResponse($response);
50
    }
51
52 18
    protected function throwThrowableMiddleware(Throwable $error): MiddlewareInterface
53
    {
54
        return new class ($error) implements MiddlewareInterface
55
        {
56
            protected $error;
57
58 9
            public function __construct(Throwable $error)
59
            {
60 27
                $this->error = $error;
61 27
            }
62
63 9
            public function process(
64
                ServerRequestInterface $request,
65
                RequestHandlerInterface $handler
66
            ): ResponseInterface {
67 27
                throw $this->error;
68
            }
69
        };
70
    }
71
}
72