ApplicationStrategy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

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