Passed
Pull Request — master (#116)
by Dmitriy
04:07 queued 01:27
created

Gii   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
eloc 8
c 4
b 1
f 0
dl 0
loc 28
ccs 9
cts 12
cp 0.75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addGenerator() 0 3 1
A __construct() 0 2 1
A getGenerators() 0 5 2
A getGenerator() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii;
6
7
use Closure;
8
use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;
9
10
/**
11
 * @psalm-import-type LazyGenerator from GiiInterface
12
 */
13
final class Gii implements GiiInterface
14
{
15
    /**
16
     * @param array<string, GeneratorInterface|LazyGenerator> $generators
17
     */
18 8
    public function __construct(private array $generators)
19
    {
20 8
    }
21
22
    public function addGenerator(GeneratorInterface $generator): void
23
    {
24
        $this->generators[$generator::getId()] = $generator;
25
    }
26
27 1
    public function getGenerator(string $id): GeneratorInterface
28
    {
29 1
        if (!isset($this->generators[$id])) {
30 1
            throw new GeneratorNotFoundException('Generator "' . $id . '" not found');
31
        }
32
33
        return $this->generators[$id] instanceof Closure ? $this->generators[$id]() : $this->generators[$id];
34
    }
35
36 4
    public function getGenerators(): array
37
    {
38 4
        return array_map(
39 4
            fn (Closure|GeneratorInterface $generator) => $generator instanceof Closure ? $generator() : $generator,
40 4
            $this->generators
41 4
        );
42
    }
43
}
44