Group   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 34
dl 0
loc 86
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 12 3
A addGroup() 0 3 1
A controller() 0 5 1
A addRoute() 0 23 6
A render() 0 5 1
A group() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck;
6
7
use Closure;
8
use Conia\Chuck\Exception\RuntimeException;
9
use Conia\Chuck\Routing\AddsMiddleware;
10
use Conia\Chuck\Routing\AddsRoutes;
11
use Conia\Chuck\Routing\RouteAdder;
12
13
/** @psalm-api */
14
class Group implements RouteAdder
15
{
16
    use AddsRoutes;
17
    use AddsMiddleware;
18
19
    /** @psalm-var list<Group> */
20
    protected array $subgroups = [];
21
    protected ?RouteAdder $routeAdder = null;
22
    protected ?string $renderer = null;
23
    protected ?string $controller = null;
24
    protected bool $created = false;
25
26 14
    public function __construct(
27
        protected string $patternPrefix,
28
        protected Closure $createClosure,
29
        protected string $namePrefix = '',
30
    ) {
31 14
    }
32
33 3
    public function controller(string $controller): static
34
    {
35 3
        $this->controller = $controller;
36
37 3
        return $this;
38
    }
39
40 1
    public function render(string $renderer): static
41
    {
42 1
        $this->renderer = $renderer;
43
44 1
        return $this;
45
    }
46
47 14
    public function addRoute(Route $route): Route
48
    {
49 14
        $route->prefix($this->patternPrefix, $this->namePrefix);
50
51 14
        if ($this->renderer && empty($route->getRenderer())) {
52 1
            $route->render($this->renderer);
53
        }
54
55 14
        if ($this->controller) {
56 3
            $route->controller($this->controller);
57
        }
58
59 12
        if (!empty($this->middleware)) {
60 2
            $route->replaceMiddleware(array_merge($this->middleware, $route->getMiddleware()));
61
        }
62
63 12
        if ($this->routeAdder) {
64 11
            $this->routeAdder->addRoute($route);
65
66 11
            return $route;
67
        }
68
69 1
        throw new RuntimeException('RouteAdder not set');
70
    }
71
72 1
    public function addGroup(Group $group): void
73
    {
74 1
        $group->create($this);
75
    }
76
77 1
    public function group(
78
        string $patternPrefix,
79
        Closure $createClosure,
80
        string $namePrefix = '',
81
    ): Group {
82 1
        $group = new Group($patternPrefix, $createClosure, $namePrefix);
83 1
        $this->subgroups[] = $group;
84
85 1
        return $group;
86
    }
87
88 13
    public function create(RouteAdder $adder): void
89
    {
90 13
        if ($this->created) {
91 1
            return;
92
        }
93
94 13
        $this->created = true;
95 13
        $this->routeAdder = $adder;
96 13
        ($this->createClosure)($this);
97
98 11
        foreach ($this->subgroups as $subgroup) {
99 1
            $subgroup->create($this);
100
        }
101
    }
102
}
103