Completed
Push — master ( 6e909e...c441c1 )
by Phil
03:17
created

RouteGroup::map()   B

Complexity

Conditions 5
Paths 16

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 21
Code Lines 11

Code Coverage

Tests 14
CRAP Score 5

Importance

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