Completed
Pull Request — 1.x (#214)
by Yuu
03:24 queued 55s
created

RouterCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
ccs 16
cts 18
cp 0.8889
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 11 3
A routeNotFound() 0 8 1
A generate() 0 11 3
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Router;
8
9
use BEAR\Sunday\Extension\Router\RouterInterface;
10
use BEAR\Sunday\Extension\Router\RouterMatch;
11
12
class RouterCollection implements RouterInterface
13
{
14
    const ROUTE_NOT_FOUND = 'page://self/__route_not_found';
15
    /**
16
     * @var RouterInterface[]
17
     */
18
    private $routers;
19
20
    /**
21
     * @param RouterInterface[] $routers
22
     */
23 3
    public function __construct(array $routers)
24
    {
25 3
        $this->routers = $routers;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function match(array $globals, array $server)
32
    {
33 2
        foreach ($this->routers as $route) {
34
            $match = $route->match($globals, $server);
35 1
            if ($match !== false) {
36 1
                return $match;
37
            }
38 2
        }
39
40
        return $this->routeNotFound();
41 2
    }
42
43
    /**
44
     * @return RouterMatch
45
     */
46 1
    private function routeNotFound()
47
    {
48
        $routeMatch = new RouterMatch;
49 1
        $routeMatch->method = 'get';
50 1
        $routeMatch->path = self::ROUTE_NOT_FOUND;
51
52 1
        return $routeMatch;
53
    }
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function generate($name, $data)
58
    {
59 2
        foreach ($this->routers as $route) {
60
            $uri = $route->generate($name, $data);
61
            if ($uri) {
62 1
                return $uri;
63
            }
64
        }
65
66 1
        return false;
67
    }
68
}
69