MatchRoute::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php declare(strict_types = 1);
2
3
/**
4
 * This file is part of the Simplex package.
5
 *
6
 * (c) Freddie Frantzen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Simplex\HttpMiddleware;
12
13
use Psr\Http\Message\ResponseInterface as Response;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Simplex\Routing\RouteParamsRegistry;
16
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
17
use Symfony\Component\Routing\Matcher\UrlMatcher;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Routing\RouteCollection;
20
21
final class MatchRoute
22
{
23
    /** @var RouteCollection */
24
    private $routeCollection;
25
26
    /** @var RouteParamsRegistry */
27
    private $routeParamsRegistry;
28
29 1
    public function __construct(RouteCollection $routeCollection, RouteParamsRegistry $routeParamsRegistry)
30
    {
31 1
        $this->routeCollection = $routeCollection;
32 1
        $this->routeParamsRegistry = $routeParamsRegistry;
33 1
    }
34
35 1
    public function __invoke(ServerRequestInterface $request, Response $response, callable $next)
36
    {
37 1
        $routeParameters = $this->matchRoute($request);
38
39 1
        $this->routeParamsRegistry->setRouteParams($routeParameters);
40
41 1
        $response = $next($request, $response);
42
43 1
        return $response;
44
    }
45
46 1
    private function matchRoute(ServerRequestInterface $request): array
47
    {
48 1
        $factory = new HttpFoundationFactory();
49 1
        $symfonyRequest = $factory->createRequest($request);
50
51 1
        $context = new RequestContext();
52 1
        $context->fromRequest($symfonyRequest);
53
54 1
        $matcher = new UrlMatcher($this->routeCollection, $context);
55
56 1
        return $matcher->match($request->getUri()->getPath());
57
    }
58
}
59