Passed
Pull Request — master (#831)
by Maxim
06:01
created

GroupRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 10
c 0
b 0
f 0
dl 0
loc 39
ccs 12
cts 12
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultGroup() 0 5 1
A __construct() 0 3 1
A getDefaultGroup() 0 3 1
A getIterator() 0 3 1
A getGroup() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Router;
6
7
use Spiral\Core\FactoryInterface;
8
9
/**
10
 * Manages the presets for various route groups.
11
 */
12
final class GroupRegistry implements \IteratorAggregate
13
{
14
    private string $defaultGroup = 'web';
15
16
    /** @var array<non-empty-string, RouteGroup> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, RouteGroup> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, RouteGroup>.
Loading history...
17
    private array $groups = [];
18
19 276
    public function __construct(
20
        private readonly FactoryInterface $factory
21
    ) {
22
    }
23
24 276
    public function getGroup(string $name): RouteGroup
25
    {
26 276
        if (!isset($this->groups[$name])) {
27 276
            $this->groups[$name] = $this->factory->make(RouteGroup::class, ['name' => $name]);
28
        }
29
30 276
        return $this->groups[$name];
31
    }
32
33 1
    public function setDefaultGroup(string $group): self
34
    {
35 1
        $this->defaultGroup = $group;
36
37 1
        return $this;
38
    }
39
40 275
    public function getDefaultGroup(): string
41
    {
42 275
        return $this->defaultGroup;
43
    }
44
45
    /**
46
     * @return \ArrayIterator<array-key, RouteGroup>
47
     */
48 3
    public function getIterator(): \Traversable
49
    {
50 3
        return new \ArrayIterator($this->groups);
51
    }
52
}
53