Completed
Push — master ( b9d832...421ea6 )
by Phil
06:19 queued 04:14
created

RouteGroup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace League\Route;
4
5
class RouteGroup implements RouteCollectionInterface
6
{
7
    use RouteCollectionMapTrait;
8
    use RouteConditionTrait;
9
10
    /**
11
     * @var callable
12
     */
13
    protected $callback;
14
15
    /**
16
     * @var \League\Route\RouteCollectionInterface
17
     */
18
    protected $collection;
19
20
    /**
21
     * @var string
22
     */
23
    protected $prefix;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string                        $prefix
29
     * @param callable                      $callback
30
     * @param \League\Route\RouteCollection $collection
31
     */
32 3
    public function __construct($prefix, callable $callback, RouteCollectionInterface $collection)
33
    {
34 3
        $this->callback   = $callback;
35 3
        $this->collection = $collection;
36 3
        $this->prefix     = sprintf('/%s', ltrim($prefix, '/'));
37 3
    }
38
39
    /**
40
     * Process the group and ensure routes are added to the collection.
41
     *
42
     * @return void
43
     */
44 3
    public function __invoke()
45
    {
46 3
        call_user_func_array($this->callback, [$this]);
47 3
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function map($method, $path, $handler)
53
    {
54 3
        $path  = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/'));
55 3
        $route = $this->collection->map($method, $path, $handler);
56
57 3
        $route->setParentGroup($this);
58
59 3
        if ($host = $this->getHost()) {
60 3
            $route->setHost($host);
61 3
        }
62
63 3
        if ($scheme = $this->getScheme()) {
64 3
            $route->setScheme($scheme);
65 3
        }
66
67 3
        return $route;
68
    }
69
}
70