ApplicationStrategy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 93
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 4 1
A hp$0 ➔ process() 0 6 1
A invokeRouteCallable() 0 9 1
A getNotFoundDecorator() 0 4 1
A getMethodNotAllowedDecorator() 0 4 1
A throwThrowableMiddleware() 0 19 1
A getExceptionHandler() 0 4 1
A hp$1 ➔ process() 0 10 2
A getThrowableHandler() 0 21 2
1
<?php declare(strict_types=1);
2
3
namespace League\Route\Strategy;
4
5
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
6
use League\Route\Route;
7
use League\Route\{ContainerAwareInterface, ContainerAwareTrait};
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 15
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
20
    {
21 15
        $controller = $route->getCallable($this->getContainer());
22
23 15
        $response = $controller($request, $route->getVars());
24 12
        $response = $this->applyDefaultResponseHeaders($response);
25
26 12
        return $response;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 18
    public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
33
    {
34 18
        return $this->throwThrowableMiddleware($exception);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $exception): MiddlewareInterface
41
    {
42 6
        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 24
            public function __construct(Throwable $error)
59
            {
60 24
                $this->error = $error;
61 24
            }
62
63 24
            public function process(
64
                ServerRequestInterface $request,
65
                RequestHandlerInterface $requestHandler
66
            ): ResponseInterface {
67 24
                throw $this->error;
68
            }
69
        };
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 15
    public function getExceptionHandler(): MiddlewareInterface
76
    {
77 15
        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
             * @throws Throwable
91
             */
92 15
            public function process(
93
                ServerRequestInterface $request,
94
                RequestHandlerInterface $requestHandler
95
            ): ResponseInterface {
96
                try {
97 15
                    return $requestHandler->handle($request);
98 6
                } catch (Throwable $e) {
99 6
                    throw $e;
100
                }
101
            }
102
        };
103
    }
104
}
105