SymfonyRouter::getGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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