Passed
Pull Request — master (#59)
by Sergei
22:25 queued 07:22
created

FromFilesSchemaProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 38
c 1
b 0
f 0
dl 0
loc 90
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 3 1
A isReadable() 0 3 1
A read() 0 19 6
A isWritable() 0 3 1
A clear() 0 3 1
A __construct() 0 3 1
A withConfig() 0 29 5
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
19
    /**
20
     * @var array array of files with schema
21
     */
22
    private array $files = [];
23
24
    /**
25
     * @var bool throw exception if file not found
26
     */
27
    private bool $strict = false;
28
29
    private Aliases $aliases;
30
31
    public function __construct(Aliases $aliases)
32
    {
33
        $this->aliases = $aliases;
34
    }
35
36
    public function withConfig(array $config): self
37
    {
38
        if (empty($config['files'])) {
39
            throw new InvalidArgumentException('Files not set.');
40
        }
41
        if (!is_array($config['files'])) {
42
            throw new InvalidArgumentException('The "files" parameter must be an array.');
43
        }
44
45
        if (isset($config['strict'])) {
46
            if (!is_bool($config['strict'])) {
47
                throw new InvalidArgumentException('The "strict" parameter must be a boolean.');
48
            }
49
            $strict = $config['strict'];
50
        } else {
51
            $strict = $this->strict;
52
        }
53
54
        $files = $config['files'];
55
56
        $files = array_map(
57
            fn ($file) => $this->aliases->get($file),
58
            $files
59
        );
60
61
        $new = clone $this;
62
        $new->files = $files;
63
        $new->strict = $strict;
64
        return $new;
65
    }
66
67
    public function read(): ?array
68
    {
69
        $schema = null;
70
71
        foreach ($this->files as $file) {
72
            if (is_file($file)) {
73
                $schema = $schema ?? [];
74
                foreach (require $file as $role => $definition) {
75
                    if (array_key_exists($role, $schema)) {
76
                        throw new DuplicateRoleException($role);
77
                    }
78
                    $schema[$role] = $definition;
79
                }
80
            } elseif ($this->strict) {
81
                throw new SchemaFileNotFoundException($file);
82
            }
83
        }
84
85
        return $schema;
86
    }
87
88
    public function write(array $schema): bool
89
    {
90
        return false;
91
    }
92
93
    public function clear(): bool
94
    {
95
        return false;
96
    }
97
98
    public function isWritable(): bool
99
    {
100
        return false;
101
    }
102
103
    public function isReadable(): bool
104
    {
105
        return true;
106
    }
107
}
108