Passed
Push — master ( 473b48...eb1314 )
by Jelmer
01:55
created

SymfonyRouter::parseRequest()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 4
nop 1
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.9666
c 1
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle\Router;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Symfony\Component\Routing\Exception\ExceptionInterface as SymfonyRoutingException;
7
use Symfony\Component\Routing\Generator\UrlGenerator;
8
use Symfony\Component\Routing\Matcher\UrlMatcher;
9
use Symfony\Component\Routing\RequestContext;
10
use Symfony\Component\Routing\Route;
11
use Symfony\Component\Routing\RouteCollection;
12
13
final class SymfonyRouter implements RouterInterface
14
{
15
    private string $baseUrl;
16
    private RouteCollection $router;
17
    private SymfonyUrlGenerator $generator;
18
19 10
    public function __construct(string $baseUrl)
20
    {
21 10
        $this->baseUrl = $baseUrl;
22
23 10
        $this->router = new RouteCollection();
24 10
        $this->generator = new SymfonyUrlGenerator(
25 10
            new UrlGenerator(
26 10
                $this->router,
27 10
                new RequestContext($this->baseUrl, 'GET', parse_url($this->baseUrl, PHP_URL_HOST) ?: 'localhost')
28
            )
29
        );
30
    }
31
32 7
    public function parseRequest(ServerRequestInterface $request): RouteMatchInterface
33
    {
34
        try {
35 7
            $matcher = new UrlMatcher($this->router, $this->getRequestContext($request));
36 7
            $routeMatch = $matcher->match($request->getUri()->getPath());
37
38 6
            return new RouteMatch(
39
                true,
40 6
                ($routeMatch['controller'])(),
41 6
                array_diff_key($routeMatch, array_flip(['controller', '_route']))
42
            );
43 1
        } catch (SymfonyRoutingException $exception) {
44 1
            return new RouteMatch(false);
45
        }
46
    }
47
48 7
    private function getRequestContext(ServerRequestInterface $request): RequestContext
49
    {
50 7
        return new RequestContext(
51 7
            $this->baseUrl,
52 7
            $request->getMethod(),
53 7
            $request->getUri()->getHost(),
54 7
            $request->getUri()->getScheme(),
55
            80,
56
            443,
57 7
            $request->getUri()->getPath(),
58 7
            $request->getUri()->getQuery()
59
        );
60
    }
61
62 1
    public function getGenerator(): UrlGeneratorInterface
63
    {
64 1
        return $this->generator;
65
    }
66
67 1
    public function registerRoutes(RoutingProviderInterface $routingProvider): void
68
    {
69 1
        $routingProvider->registerRoutes($this);
70
    }
71
72 1
    public function get(
73
        string $name,
74
        string $path,
75
        callable $controllerFactory,
76
        array $defaults = [],
77
        array $requirements = []
78
    ): Route
79
    {
80 1
        return $this->match($name, 'GET', $path, $controllerFactory, $defaults, $requirements);
81
    }
82
83 6
    public function match(
84
        string $name,
85
        string $methods,
86
        string $path,
87
        callable $controllerFactory,
88
        array $defaults = [],
89
        array $requirements = []
90
    ): Route
91
    {
92 6
        $route = new Route($path, $defaults, $requirements);
93 6
        $route->setMethods(explode('|', $methods))
94 6
            ->setDefault('controller', $controllerFactory);
95 6
        $this->router->add($name, $route);
96 6
        return $route;
97
    }
98
99 1
    public function post(
100
        string $name,
101
        string $path,
102
        callable $controllerFactory,
103
        array $defaults = [],
104
        array $requirements = []
105
    ): Route
106
    {
107 1
        return $this->match($name, 'POST', $path, $controllerFactory, $defaults, $requirements);
108
    }
109
110 1
    public function put(
111
        string $name,
112
        string $path,
113
        callable $controllerFactory,
114
        array $defaults = [],
115
        array $requirements = []
116
    ): Route
117
    {
118 1
        return $this->match($name, 'PUT', $path, $controllerFactory, $defaults, $requirements);
119
    }
120
121 1
    public function patch(
122
        string $name,
123
        string $path,
124
        callable $controllerFactory,
125
        array $defaults = [],
126
        array $requirements = []
127
    ): Route
128
    {
129 1
        return $this->match($name, 'PATCH', $path, $controllerFactory, $defaults, $requirements);
130
    }
131
132 1
    public function delete(
133
        string $name,
134
        string $path,
135
        callable $controllerFactory,
136
        array $defaults = [],
137
        array $requirements = []
138
    ): Route
139
    {
140 1
        return $this->match($name, 'DELETE', $path, $controllerFactory, $defaults, $requirements);
141
    }
142
}
143