Completed
Push — master ( 90fd7e...b7f6a5 )
by Dawid
02:15
created

Router::findRoute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Igni\Http;
4
5
use FastRoute\DataGenerator;
6
use FastRoute\Dispatcher;
7
use FastRoute\Dispatcher\GroupCountBased;
8
use FastRoute\RouteParser;
9
use Igni\Http\Exception\MethodNotAllowedException;
10
use Igni\Http\Exception\NotFoundException;
11
use Igni\Http\Exception\RouterException;
12
13
/**
14
 * Utility class for simplifying api for nikic router.
15
 *
16
 * @package Igni\Http
17
 */
18
class Router
19
{
20
    /**
21
     * @var RouteParser
22
     */
23
    private $routeParser;
24
25
    /**
26
     * @var DataGenerator
27
     */
28
    private $dataGenerator;
29
30
    /**
31
     * Router constructor.
32
     * @param RouteParser $routeParser
33
     * @param DataGenerator $dataGenerator
34
     */
35 17
    public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator)
36
    {
37 17
        $this->routeParser = $routeParser;
38 17
        $this->dataGenerator = $dataGenerator;
39 17
    }
40
41
    /**
42
     * Registers new route.
43
     *
44
     * @param Route $route
45
     */
46 13
    public function addRoute(Route $route): void
47
    {
48 13
        if (!$route instanceof Route) {
0 ignored issues
show
introduced by
$route is always a sub-type of Igni\Http\Route.
Loading history...
49
            throw RouterException::invalidRoute($route);
50
        }
51 13
        $routeDatas = $this->routeParser->parse($route->getExpression());
52 13
        foreach ((array) $route->getMethod() as $method) {
53 13
            foreach ($routeDatas as $routeData) {
54 13
                $this->dataGenerator->addRoute($method, $routeData, $route);
55
            }
56
        }
57 13
    }
58
59
    /**
60
     * Finds route matching clients request.
61
     *
62
     * @param string $method request method.
63
     * @param string $uri request uri.
64
     * @return Route
65
     */
66 14
    public function findRoute(string $method, string $uri): Route
67
    {
68 14
        $dispatcher = new GroupCountBased($this->getData());
69 14
        $info = $dispatcher->dispatch($method, $uri);
70
71 14
        switch ($info[0]) {
72 14
            case Dispatcher::NOT_FOUND:
73 3
                throw NotFoundException::notFound($uri, $method);
74
75 11
            case Dispatcher::METHOD_NOT_ALLOWED:
76 1
                $allowedMethods = $info[1];
77 1
                throw MethodNotAllowedException::methodNotAllowed($uri, $method, $allowedMethods);
78
79 10
            case Dispatcher::FOUND:
80 10
                return $info[1]->withAttributes($info[2]);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Igni\Http\Route. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
81
        }
82
    }
83
84 14
    protected function getData()
85
    {
86 14
        return $this->dataGenerator->getData();
87
    }
88
}
89