Passed
Pull Request — master (#38)
by Aleksei
13:38
created

FromConveyorSchemaProvider::getGenerators()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider;
6
7
use Closure;
8
use Cycle\Schema\Compiler;
9
use Cycle\Schema\GeneratorInterface;
10
use Cycle\Schema\Registry;
11
use Spiral\Database\DatabaseManager;
12
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface;
13
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
14
15
final class FromConveyorSchemaProvider implements SchemaProviderInterface
16
{
17
    private SchemaConveyorInterface $conveyor;
18
    private DatabaseManager $dbal;
19
    /**
20
     * Additional generators when reading Schema
21
     * @var string[]|GeneratorInterface[]|Closure[]
22
     */
23
    private array $generators = [];
24
25
    public function __construct(SchemaConveyorInterface $conveyor, DatabaseManager $dbal)
26
    {
27
        $this->conveyor = $conveyor;
28
        $this->dbal = $dbal;
29
    }
30
31
    public function withConfig(array $config): SchemaProviderInterface
32
    {
33
        $clone = clone $this;
34
        $clone->generators = $config['generators'] ?? [];
35
        return $clone;
36
    }
37
38
    public function read(): ?array
39
    {
40
        $generators = $this->getGenerators();
41
        return (new Compiler())->compile(new Registry($this->dbal), $generators);
42
    }
43
44
    public function write($schema): bool
45
    {
46
        return false;
47
    }
48
49
    public function clear(): bool
50
    {
51
        return false;
52
    }
53
54
    public function isWritable(): bool
55
    {
56
        return false;
57
    }
58
59
    public function isReadable(): bool
60
    {
61
        return true;
62
    }
63
64
    private function getGenerators(): array
65
    {
66
        $conveyor = clone $this->conveyor;
67
        // add generators to userland stage
68
        foreach ($this->generators as $generator) {
69
            $conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, $generator);
70
        }
71
        return $conveyor->getGenerators();
72
    }
73
}
74