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

FromConveyorSchemaProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 57
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getGenerators() 0 8 2
A read() 0 4 1
A isWritable() 0 3 1
A clear() 0 3 1
A isReadable() 0 3 1
A write() 0 3 1
A withConfig() 0 5 1
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