RouteGroup::map()   B
last analyzed

Complexity

Conditions 7
Paths 32

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

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