Passed
Push — master ( 7a381a...c331f6 )
by Jelmer
03:04
created

SymfonyRouter::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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