RouteGroup   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 89
ccs 23
cts 23
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 25 7
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\RouteCollectionInterface $collection
40
     */
41 12
    public function __construct(string $prefix, callable $callback, RouteCollectionInterface $collection)
42
    {
43 12
        $this->callback   = $callback;
44 12
        $this->collection = $collection;
45 12
        $this->prefix     = sprintf('/%s', ltrim($prefix, '/'));
46 12
    }
47
48
    /**
49
     * Return the prefix of the group
50
     *
51
     * @return string
52
     */
53 6
    public function getPrefix() : string
54
    {
55 6
        return $this->prefix;
56
    }
57
58
    /**
59
     * Process the group and ensure routes are added to the collection
60
     *
61
     * @return void
62
     */
63 10
    public function __invoke() : void
64
    {
65 10
        ($this->callback)($this);
66 10
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 10
    public function map(string $method, string $path, $handler) : Route
72
    {
73 10
        $path  = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/'));
74 10
        $route = $this->collection->map($method, $path, $handler);
75
76 10
        $route->setParentGroup($this);
77
78 10
        if ($host = $this->getHost()) {
79 2
            $route->setHost($host);
80
        }
81
82 10
        if ($scheme = $this->getScheme()) {
83 2
            $route->setScheme($scheme);
84
        }
85
86 10
        if ($port = $this->getPort()) {
87 2
            $route->setPort($port);
88
        }
89
90 10
        if (is_null($route->getStrategy()) && ! is_null($this->getStrategy())) {
91 6
            $route->setStrategy($this->getStrategy());
92
        }
93
94 10
        return $route;
95
    }
96
}
97