RouteGroup::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route;
6
7
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait};
8
use Psr\Http\Server\RequestHandlerInterface;
9
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait};
10
11
class RouteGroup implements
12
    MiddlewareAwareInterface,
13
    RouteCollectionInterface,
14
    RouteConditionHandlerInterface,
15
    StrategyAwareInterface
16
{
17
    use MiddlewareAwareTrait;
18
    use RouteCollectionTrait;
19
    use RouteConditionHandlerTrait;
20
    use StrategyAwareTrait;
21
22
    /**
23
     * @var callable
24
     */
25
    protected $callback;
26
27 21
    public function __construct(
28
        protected string $prefix,
29
        callable $callback,
30
        protected RouteCollectionInterface $collection
31
    ) {
32 21
        $this->callback = $callback;
33 21
        $this->prefix = sprintf('/%s', ltrim($this->prefix, '/'));
34
    }
35
36 18
    public function __invoke(): void
37
    {
38 18
        ($this->callback)($this);
39
    }
40
41 15
    public function getPrefix(): string
42
    {
43 15
        return $this->prefix;
44
    }
45
46 18
    public function map(
47
        string|array $method,
48
        string $path,
49
        callable|array|string|RequestHandlerInterface $handler
50
    ): Route {
51 18
        $path = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/'));
52 18
        $route = $this->collection->map($method, $path, $handler);
53
54 18
        $route->setParentGroup($this);
55
56 18
        if ($host = $this->getHost()) {
57 3
            $route->setHost($host);
58
        }
59
60 18
        if ($scheme = $this->getScheme()) {
61 3
            $route->setScheme($scheme);
62
        }
63
64 18
        if ($port = $this->getPort()) {
65 3
            $route->setPort($port);
66
        }
67
68 18
        if ($route->getStrategy() === null && $this->getStrategy() !== null) {
69 9
            $route->setStrategy($this->getStrategy());
70
        }
71
72 18
        return $route;
73
    }
74
}
75