Passed
Push — master ( 8030fa...0b327c )
by Alexander
01:43
created

Gii   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 19
dl 0
loc 40
ccs 18
cts 21
cp 0.8571
c 4
b 1
f 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getGenerator() 0 19 7
A addGenerator() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii;
6
7
use Psr\Container\ContainerInterface;
8
use RuntimeException;
9
use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;
10
11
final class Gii implements GiiInterface
12
{
13
    private ContainerInterface $container;
14
    private iterable $generators;
15
16 3
    public function __construct(iterable $generators, ContainerInterface $container)
17
    {
18 3
        $this->generators = $generators;
19 3
        $this->container = $container;
20 3
    }
21
22 1
    public function addGenerator(string $name, $generator): void
23
    {
24 1
        $this->generators[$name] = $generator;
25 1
    }
26
27
    /**
28
     * @param string $name
29
     * @return GeneratorInterface
30
     * @throws GeneratorNotFoundException
31
     */
32 3
    public function getGenerator(string $name): GeneratorInterface
33
    {
34 3
        if (!isset($this->generators[$name])) {
35 1
            throw new GeneratorNotFoundException('Generator "' . $name . '" not found');
36
        }
37 2
        $generator = $this->generators[$name];
38 2
        if (is_string($generator)) {
39
            $generator = $this->container->get($generator);
40 2
        } elseif ($generator instanceof GeneratorInterface) {
41 1
            return $generator;
42 1
        } elseif (is_object($generator) && method_exists($generator, '__invoke')) {
43
            $generator = $generator($this->container);
44
        }
45 1
        if (!($generator instanceof GeneratorInterface)) {
46 1
            throw new RuntimeException(
47 1
                'Generator should be GeneratorInterface instance. "' . get_class($generator) . '" given.'
48
            );
49
        }
50
        return $generator;
51
    }
52
}
53