Completed
Pull Request — master (#115)
by Phil
02:51
created

RouteGroup   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

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 0
cts 26
cp 0
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
    public function __construct($prefix, callable $callback, RouteCollectionInterface $collection)
37
    {
38
        $this->callback   = $callback;
39
        $this->collection = $collection;
40
        $this->prefix     = sprintf('/%s', ltrim($prefix, '/'));
41
    }
42
43
    /**
44
     * Process the group and ensure routes are added to the collection.
45
     *
46
     * @return void
47
     */
48
    public function __invoke()
49
    {
50
        call_user_func_array($this->callback, [$this]);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function map($method, $path, $handler)
57
    {
58
        $path  = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/'));
59
        $route = $this->collection->map($method, $path, $handler);
60
61
        $route->setParentGroup($this);
62
63
        if ($host = $this->getHost()) {
64
            $route->setHost($host);
65
        }
66
67
        if ($scheme = $this->getScheme()) {
68
            $route->setScheme($scheme);
69
        }
70
71
        foreach ($this->getMiddlewareStack() as $middleware) {
72
            $route->middleware($middleware);
73
        }
74
75
        return $route;
76
    }
77
}
78