SchemaConveyor::addGenerator()   A
last analyzed

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 0
Metric Value
cc 1
eloc 1
c 0
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
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Conveyor;
6
7
use Cycle\Schema\Generator;
8
use Cycle\Schema\GeneratorInterface;
9
use Psr\Container\ContainerInterface;
10
use Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException;
11
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface;
12
13
abstract class SchemaConveyor implements SchemaConveyorInterface
14
{
15
    protected array $conveyor = [
16
        self::STAGE_INDEX => [
17
            Generator\ResetTables::class,       // re-declared table schemas (remove columns)
18
        ],
19
        self::STAGE_RENDER => [
20
            Generator\GenerateRelations::class, // generate entity relations
21
            Generator\GenerateModifiers::class, // generate changes from schema modifiers
22
            Generator\ValidateEntities::class,  // make sure all entity schemas are correct
23
            Generator\RenderTables::class,      // declare table schemas
24
            Generator\RenderRelations::class,   // declare relation keys and indexes
25
            Generator\RenderModifiers::class,   // render all schema modifiers
26
            Generator\ForeignKeys::class,       // define foreign key constraints
27
        ],
28
        self::STAGE_USERLAND => [],
29
        self::STAGE_POSTPROCESS => [
30
            Generator\GenerateTypecast::class,   // typecast non string columns
31
        ],
32
    ];
33
34
    protected ContainerInterface $container;
35
36 23
    public function __construct(ContainerInterface $container)
37
    {
38 23
        $this->container = $container;
39
    }
40
41 13
    public function addGenerator(string $stage, $generator): void
42
    {
43 13
        $this->conveyor[$stage][] = $generator;
44
    }
45
46 18
    public function getGenerators(): array
47
    {
48 18
        $result = [];
49 18
        foreach ($this->conveyor as $group) {
50 18
            foreach ($group as $generatorDefinition) {
51 18
                $generator = null;
52 18
                if (is_string($generatorDefinition)) {
53 18
                    $generator = $this->container->get($generatorDefinition);
54 15
                } elseif ($generatorDefinition instanceof GeneratorInterface) {
55 11
                    $result[] = $generatorDefinition;
56 11
                    continue;
57 9
                } elseif (is_object($generatorDefinition) && is_callable($generatorDefinition)) {
58 3
                    $generator = $generatorDefinition($this->container);
59
                }
60 18
                if ($generator instanceof GeneratorInterface) {
61 18
                    $result[] = $generator;
62 18
                    continue;
63
                }
64 10
                throw new BadGeneratorDeclarationException($generator ?? $generatorDefinition);
65
            }
66
        }
67 8
        return $result;
68
    }
69
}
70