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

SchemaConveyor::addGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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