Passed
Branch master (f36b3c)
by Matthew
08:01
created

Router::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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