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

RouterCollection::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3.0261
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