Passed
Pull Request — master (#68)
by Aleksei
02:32
created

FromConveyorSchemaProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 44
ccs 0
cts 18
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withConfig() 0 5 1
A getGenerators() 0 8 2
A read() 0 6 3
A clear() 0 3 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\DatabaseProviderInterface;
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 DatabaseProviderInterface $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, DatabaseProviderInterface $dbal)
26
    {
27
        $this->conveyor = $conveyor;
28
        $this->dbal = $dbal;
29
    }
30
31
    public function withConfig(array $config): self
32
    {
33
        $new = clone $this;
34
        $new->generators = $config['generators'] ?? [];
35
        return $new;
36
    }
37
38
    public function read(?SchemaProviderInterface $nextProvider = null): ?array
39
    {
40
        $generators = $this->getGenerators();
41
        $schema = (new Compiler())->compile(new Registry($this->dbal), $generators);
42
43
        return count($schema) !== 0 || $nextProvider === null ? $schema : $nextProvider->read();
44
    }
45
46
    public function clear(): bool
47
    {
48
        return false;
49
    }
50
51
    private function getGenerators(): array
52
    {
53
        $conveyor = clone $this->conveyor;
54
        // add generators to userland stage
55
        foreach ($this->generators as $generator) {
56
            $conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, $generator);
57
        }
58
        return $conveyor->getGenerators();
59
    }
60
}
61