RouteGroup   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 93
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 4 1
A getPrefix() 0 4 1
B map() 0 29 8
1
<?php declare(strict_types=1);
2
3
namespace League\Route;
4
5
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
6
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait};
7
8
class RouteGroup implements
9
    MiddlewareAwareInterface,
10
    RouteCollectionInterface,
11
    RouteConditionHandlerInterface,
12
    StrategyAwareInterface
13
{
14
    use MiddlewareAwareTrait;
15
    use RouteCollectionTrait;
16
    use RouteConditionHandlerTrait;
17
    use StrategyAwareTrait;
18
19
    /**
20
     * @var callable
21
     */
22
    protected $callback;
23
24
    /**
25
     * @var \League\Route\RouteCollectionInterface
26
     */
27
    protected $collection;
28
29
    /**
30
     * @var string
31
     */
32
    protected $prefix;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param string                        $prefix
38
     * @param callable                      $callback
39
     * @param \League\Route\RouteCollection $collection
40
     */
41 10
    public function __construct(string $prefix, callable $callback, RouteCollectionInterface $collection)
42
    {
43 10
        $this->callback   = $callback;
44 10
        $this->collection = $collection;
45 10
        $this->prefix     = sprintf('/%s', ltrim($prefix, '/'));
46 10
    }
47
48
    /**
49
     * Return the prefix of the group.
50
     *
51
     * @return string
52
     */
53 4
    public function getPrefix() : string
54
    {
55 4
        return $this->prefix;
56
    }
57
58
    /**
59
     * Process the group and ensure routes are added to the collection.
60
     *
61
     * @return void
62
     */
63 8
    public function __invoke() : void
64
    {
65 8
        call_user_func_array($this->callback, [$this]);
66 8
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 8
    public function map(string $method, string $path, $handler) : Route
72
    {
73 8
        $path  = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/'));
74 8
        $route = $this->collection->map($method, $path, $handler);
75
76 8
        $route->setParentGroup($this);
77
78 8
        if ($host = $this->getHost()) {
79 2
            $route->setHost($host);
80
        }
81
82 8
        if ($scheme = $this->getScheme()) {
83 2
            $route->setScheme($scheme);
84
        }
85
86 8
        if ($port = $this->getPort()) {
87 2
            $route->setPort($port);
88
        }
89
90 8
        foreach ($this->getMiddlewareStack() as $middleware) {
91 2
            $route->middleware($middleware);
92
        }
93
94 8
        if (is_null($route->getStrategy()) && ! is_null($this->getStrategy())) {
95 4
            $route->setStrategy($this->getStrategy());
96
        }
97
98 8
        return $route;
99
    }
100
}
101