Passed
Branch master (ae28f9)
by Alexey
03:28
created

RouteMatcher::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.7536

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 16
cp 0.5625
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 2
crap 3.7536
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Routing;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Venta\Contracts\Routing\FastrouteDispatcherFactory;
7
use Venta\Contracts\Routing\Route as RouteContract;
8
use Venta\Contracts\Routing\RouteCollection as RouteCollectionContract;
9
use Venta\Contracts\Routing\RouteMatcher as RouteMatcherContract;
10
use Venta\Contracts\Routing\RouteParser as RouteParserContract;
11
use Venta\Routing\Exception\NotAllowedException;
12
use Venta\Routing\Exception\NotFoundException;
13
14
/**
15
 * Class RouteMatcher
16
 *
17
 * @package Venta\Routing
18
 */
19
final class RouteMatcher implements RouteMatcherContract
20
{
21
22
    /**
23
     * @var FastrouteDispatcherFactory
24
     */
25
    private $fastrouteDispatcherFactory;
26
27
    /**
28
     * @var RouteParserContract
29
     */
30
    private $parser;
31
32
    /**
33
     * RouteMatcher constructor.
34
     *
35
     * @param RouteParserContract $parser
36
     * @param FastrouteDispatcherFactory $fastrouteDispatcherFactory
37
     */
38 2
    public function __construct(RouteParserContract $parser, FastrouteDispatcherFactory $fastrouteDispatcherFactory)
39
    {
40 2
        $this->parser = $parser;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41 2
        $this->fastrouteDispatcherFactory = $fastrouteDispatcherFactory;
42 2
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 1
    public function match(ServerRequestInterface $request, RouteCollectionContract $routeCollection): RouteContract
48
    {
49 1
        $routes = $routeCollection->getRoutes();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50 1
        $parsedRouteData = $this->parser->parse($routes);
51 1
        $dispatcher = $this->fastrouteDispatcherFactory->create($parsedRouteData);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52 1
        $match = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53 1
        switch ($match[0]) {
54 1
            case $dispatcher::FOUND:
55
                /** @var RouteContract $route */
56 1
                list(, $route, $variables) = $match;
57
58 1
                return $route->withVariables($variables);
59
            case $dispatcher::METHOD_NOT_ALLOWED:
60
                throw new NotAllowedException($match[1]);
61
            default:
62
                throw new NotFoundException($request->getMethod(), $request->getUri()->getPath());
63
        }
64
    }
65
66
}