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

Gii::addGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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