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

RouteGroup   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 7
c 8
b 1
f 0
lcom 1
cbo 5
dl 0
loc 70
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 4 1
B map() 0 21 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