Router   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A match() 0 6 2
A __construct() 0 3 1
1
<?php
2
namespace Fyuze\Routing;
3
4
use Fyuze\Http\Exception\NotFoundException;
5
use Psr\Http\Message\ServerRequestInterface;
6
7
class Router
8
{
9
    /**
10
     * @var Collection
11
     */
12
    protected $routes;
13
14
    /**
15
     * @param Collection $routes
16
     */
17 5
    public function __construct(Collection $routes)
18
    {
19 5
        $this->routes = $routes;
20
    }
21
22
    /**
23
     * @param ServerRequestInterface $request
24
     * @return Route
25
     * @throws NotFoundException
26
     */
27 5
    public function resolve(ServerRequestInterface $request)
28
    {
29 5
        $route = $this->match($request);
30
31 5
        if (count($route) === 0) {
32 3
            throw new NotFoundException('Page not found');
33
        }
34
35 2
        return reset($route);
36
    }
37
38
    /**
39
     * @param ServerRequestInterface $request
40
     * @return array
41
     */
42 5
    protected function match(ServerRequestInterface $request)
43
    {
44 5
        return array_filter($this->routes->getRoutes(), function (Route $route) use ($request) {
45 5
            $match = $route->matches($request);
46 5
            if(false !== $match) {
47 2
                return $route->setParams($match);
0 ignored issues
show
Bug introduced by
$match of type true is incompatible with the type array expected by parameter $params of Fyuze\Routing\Route::setParams(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                return $route->setParams(/** @scrutinizer ignore-type */ $match);
Loading history...
48
            }
49 5
        });
50
    }
51
}
52