Passed
Push — master ( c437ac...1d79be )
by Aleksei
09:50 queued 07:46
created

SchemaManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema;
6
7
use Generator;
8
use Psr\Container\ContainerInterface;
9
10
/**
11
 * SchemaManager allows reading schema from providers available and clearing the schema in providers.
12
 */
13
final class SchemaManager
14
{
15
    private ContainerInterface $container;
16
    /** @var string[]|SchemaProviderInterface[] */
17
    private array $providers;
18
19
    public function __construct(ContainerInterface $container, array $providers)
20
    {
21
        $this->container = $container;
22
        $this->providers = $providers;
23
    }
24
25
    public function read(): ?array
26
    {
27
        $toWrite = new \SplStack();
28
        $schema = null;
29
30
        foreach ($this->getProviders() as $provider) {
31
            if ($provider->isReadable()) {
32
                $schema = $provider->read();
33
                if ($schema !== null) {
34
                    break;
35
                }
36
            }
37
            if ($provider->isWritable()) {
38
                $toWrite->push($provider);
39
            }
40
        }
41
42
        if ($schema === null) {
43
            return null;
44
        }
45
46
        // Save schema
47
        /** @var SchemaProviderInterface $provider */
48
        foreach ($toWrite as $provider) {
49
            $provider->write($schema);
50
        }
51
52
        return $schema;
53
    }
54
55
    public function clear(): void
56
    {
57
        $toClear = [];
58
        $isWritableLast = false;
59
        foreach ($this->getProviders() as $provider) {
60
            $isWritableLast = $provider->isWritable();
61
            if ($isWritableLast) {
62
                $toClear[] = $provider;
63
            }
64
        }
65
        if ($isWritableLast) {
66
            array_pop($toClear);
67
        }
68
        foreach ($toClear as $provider) {
69
            $provider->clear();
70
        }
71
    }
72
73
    /**
74
     * @return Generator|SchemaProviderInterface[]
75
     */
76
    private function getProviders(): Generator
77
    {
78
        foreach ($this->providers as $key => &$provider) {
79
            // Providers resolving
80
            if (is_string($provider)) {
81
                $provider = $this->container->get($provider);
82
            }
83
            // If Provider defined as ClassName => ConfigArray
84
            if (is_array($provider) && is_string($key)) {
85
                $provider = $this->container->get($key)->withConfig($provider);
86
            }
87
88
            if (!$provider instanceof SchemaProviderInterface) {
89
                throw new \RuntimeException('Provider should be instance of SchemaProviderInterface.');
90
            }
91
            yield $provider;
92
        }
93
    }
94
}
95