Passed
Push — master ( ee28da...1c3e2f )
by Alexander
14:20 queued 13:06
created

SchemaConveyor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getGenerators() 0 22 9
A addGenerator() 0 3 1
1
<?php
2
3
namespace Yiisoft\Yii\Cycle\Generator;
4
5
use Cycle\Schema\Generator;
6
use Cycle\Schema\GeneratorInterface;
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException;
9
use Yiisoft\Yii\Cycle\SchemaConveyorInterface;
10
11
class SchemaConveyor implements SchemaConveyorInterface
12
{
13
    protected array $conveyor = [
14
        self::STAGE_INDEX => [
15
            Generator\ResetTables::class,       // re-declared table schemas (remove columns)
16
        ],
17
        self::STAGE_RENDER => [
18
            Generator\GenerateRelations::class, // generate entity relations
19
            Generator\ValidateEntities::class,  // make sure all entity schemas are correct
20
            Generator\RenderTables::class,      // declare table schemas
21
            Generator\RenderRelations::class,   // declare relation keys and indexes
22
        ],
23
        self::STAGE_USERLAND => [],
24
        self::STAGE_POSTPROCESS => [
25
            Generator\GenerateTypecast::class   // typecast non string columns
26
        ],
27
    ];
28
29
    protected ContainerInterface $container;
30
31 9
    public function __construct(ContainerInterface $container)
32
    {
33 9
        $this->container = $container;
34
    }
35
36 6
    public function addGenerator(string $stage, $generator): void
37
    {
38 6
        $this->conveyor[$stage][] = $generator;
39
    }
40
41 8
    public function getGenerators(): array
42
    {
43 8
        $result = [];
44 8
        foreach ($this->conveyor as $group) {
45 8
            foreach ($group as $generatorDefinition) {
46 8
                $generator = null;
47 8
                if (is_string($generatorDefinition)) {
48 8
                    $generator = $this->container->get($generatorDefinition);
49 5
                } elseif (is_object($generatorDefinition) && $generatorDefinition instanceof GeneratorInterface) {
50 3
                    $result[] = $generatorDefinition;
51 3
                    continue;
52 3
                } elseif (is_object($generatorDefinition) && method_exists($generatorDefinition, '__invoke')) {
53 2
                    $generator = $generatorDefinition($this->container);
54
                }
55 8
                if ($generator instanceof GeneratorInterface) {
56 8
                    $result[] = $generator;
57 8
                    continue;
58
                }
59 3
                throw new BadGeneratorDeclarationException();
60
            }
61
        }
62 5
        return $result;
63
    }
64
}
65