Passed
Pull Request — master (#59)
by Sergei
12:10
created

FromFilesSchemaProvider::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider;
6
7
use InvalidArgumentException;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Yii\Cycle\Exception\DuplicateRoleException;
10
use Yiisoft\Yii\Cycle\Exception\SchemaFileNotFoundException;
11
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
12
13
/**
14
 * Be careful, using this class may be insecure.
15
 */
16
final class FromFilesSchemaProvider implements SchemaProviderInterface
17
{
18
    private array $files = [];
19
    private bool $strict = false;
20
21
    private Aliases $aliases;
22
23
    public function __construct(Aliases $aliases)
24
    {
25
        $this->aliases = $aliases;
26
    }
27
28
    public function withConfig(array $config): self
29
    {
30
        if (empty($config['files'])) {
31
            throw new InvalidArgumentException('Files not set.');
32
        }
33
        if (!is_array($config['files'])) {
34
            throw new InvalidArgumentException('The "files" parameter must be an array.');
35
        }
36
37
        if (isset($config['strict'])) {
38
            if (!is_bool($config['strict'])) {
39
                throw new InvalidArgumentException('The "strict" parameter must be a boolean.');
40
            }
41
            $strict = $config['strict'];
42
        } else {
43
            $strict = $this->strict;
44
        }
45
46
        $files = $config['files'];
47
48
        $files = array_map(
49
            fn ($file) => $this->aliases->get($file),
50
            $files
51
        );
52
53
        $new = clone $this;
54
        $new->files = $files;
55
        $new->strict = $strict;
56
        return $new;
57
    }
58
59
    public function read(): ?array
60
    {
61
        $schema = null;
62
63
        foreach ($this->files as $file) {
64
            if (is_file($file)) {
65
                $schema = $schema ?? [];
66
                foreach (require $file as $role => $definition) {
67
                    if (array_key_exists($role, $schema)) {
68
                        throw new DuplicateRoleException($role);
69
                    }
70
                    $schema[$role] = $definition;
71
                }
72
            } elseif ($this->strict) {
73
                throw new SchemaFileNotFoundException($file);
74
            }
75
        }
76
77
        return $schema;
78
    }
79
80
    public function write(array $schema): bool
81
    {
82
        return false;
83
    }
84
85
    public function clear(): bool
86
    {
87
        return false;
88
    }
89
90
    public function isWritable(): bool
91
    {
92
        return false;
93
    }
94
95
    public function isReadable(): bool
96
    {
97
        return true;
98
    }
99
}
100