Completed
Push — master ( f9c388...b2e961 )
by Andrew
05:15 queued 01:05
created

Router   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 88.46%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 55
ccs 23
cts 26
cp 0.8846
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A match() 0 13 4
1
<?php
2
3
namespace SimpleRoute;
4
5
use FastRoute;
6
use FastRoute\Dispatcher;
7
use FastRoute\RouteCollector;
8
use SimpleRoute\Exception\MethodNotAllowedException;
9
use SimpleRoute\Exception\NotFoundException;
10
11
final class Router implements RouterInterface
12
{
13
    /**
14
     * @var Dispatcher
15
     */
16
    private $dispatcher;
17
18
    /**
19
     * @var RouteInterface
20
     */
21
    private $routes;
22
23
    /**
24
     * @param RouteInterface[] A collection of routes
25
     */
26 12
    public function __construct(array $routes)
27
    {
28 12
        $this->routes = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<SimpleRoute\RouteInterface> of property $routes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
30 12
        $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $collector) use ($routes) {
31 12
            foreach ($routes as $route) {
32 9
                $key = spl_object_hash($route);
33
34 9
                $this->routes[$key] = $route;
35
36 9
                if (!$route instanceof RouteInterface) {
37
                    throw new \InvalidArgumentException('Routes array must contain only RouteInterface objects');
38
                }
39
40 9
                $collector->addRoute(
41 9
                    $route->getMethods(),
42 9
                    $route->getPattern(),
43
                    $key
44 9
                );
45 12
            }
46 12
        });
47 12
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 12
    public function match($method, $uri)
53
    {
54 12
        $result = $this->dispatcher->dispatch($method, $uri);
55
56 12
        switch ($result[0]) {
57 12
            case Dispatcher::FOUND:
58 6
                return new Result($this->routes[$result[1]], $result[2]); 
59 6
            case Dispatcher::METHOD_NOT_ALLOWED:
60 3
                throw new MethodNotAllowedException($method, $result[1]);
61 3
            case Dispatcher::NOT_FOUND:
62 3
                throw new NotFoundException($uri);
63
        }
64
    }
65
}
66