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

SchemaManager::getProviders()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 9
nop 0
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
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
                    echo get_class($provider);
35
                    break;
36
                }
37
            }
38
            if ($provider->isWritable()) {
39
                $toWrite->push($provider);
40
            }
41
        }
42
43
        if ($schema === null) {
44
            return null;
45
        }
46
47
        // Save schema
48
        /** @var SchemaProviderInterface $provider */
49
        foreach ($toWrite as $provider) {
50
            $provider->write($schema);
51
        }
52
53
        return $schema;
54
    }
55
56
    public function clear(): void
57
    {
58
        $toClear = [];
59
        $isWritableLast = false;
60
        foreach ($this->getProviders() as $provider) {
61
            $isWritableLast = $provider->isWritable();
62
            if ($isWritableLast) {
63
                $toClear[] = $provider;
64
            }
65
        }
66
        if ($isWritableLast) {
67
            array_pop($toClear);
68
        }
69
        foreach ($toClear as $provider) {
70
            $provider->clear();
71
        }
72
    }
73
74
    /**
75
     * @return Generator|SchemaProviderInterface[]
76
     */
77
    private function getProviders(): Generator
78
    {
79
        foreach ($this->providers as $key => &$provider) {
80
            // Providers resolving
81
            if (is_string($provider)) {
82
                $provider = $this->container->get($provider);
83
            }
84
            // If Provider defined as ClassName => ConfigArray
85
            if (is_array($provider) && is_string($key)) {
86
                $provider = $this->container->get($key)->withConfig($provider);
87
            }
88
89
            if (!$provider instanceof SchemaProviderInterface) {
90
                throw new \RuntimeException('Provider should be instance of SchemaProviderInterface.');
91
            }
92
            yield $provider;
93
        }
94
    }
95
}
96