Passed
Push — master ( d5dda9...c69dbe )
by Alexander
02:26
created

FromFilesSchemaProvider::read()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 8.4444
cc 8
nc 18
nop 1
crap 8
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
    /** @var array Schema files */
19
    private array $files = [];
20
21
    /** @var bool Throw exception if file not found */
22
    private bool $strict = false;
23
24
    private Aliases $aliases;
25
26 19
    public function __construct(Aliases $aliases)
27
    {
28 19
        $this->aliases = $aliases;
29 19
    }
30
31 16
    public function withConfig(array $config): self
32
    {
33 16
        $files = $config['files'] ?? [];
34 16
        if (!is_array($files)) {
35 1
            throw new InvalidArgumentException('The "files" parameter must be an array.');
36
        }
37 15
        if (count($files) === 0) {
38 2
            throw new InvalidArgumentException('Schema file list is not set.');
39
        }
40
41 13
        $strict = $config['strict'] ?? $this->strict;
42 13
        if (!is_bool($strict)) {
43 1
            throw new InvalidArgumentException('The "strict" parameter must be a boolean.');
44
        }
45
46 12
        $files = array_map(
47 12
            function ($file) {
48 12
                if (!is_string($file)) {
49 5
                    throw new InvalidArgumentException('The "files" parameter must contain string values.');
50
                }
51 7
                return $this->aliases->get($file);
52 12
            },
53
            $files
54
        );
55
56 7
        $new = clone $this;
57 7
        $new->files = $files;
58 7
        $new->strict = $strict;
59 7
        return $new;
60
    }
61
62 9
    public function read(?SchemaProviderInterface $nextProvider = null): ?array
63
    {
64 9
        $schema = null;
65 9
        foreach ($this->files as $file) {
66 7
            if (is_file($file)) {
67 6
                $schema = $schema ?? [];
68 6
                foreach (require $file as $role => $definition) {
69 5
                    if (array_key_exists($role, $schema)) {
70 1
                        throw new DuplicateRoleException($role);
71
                    }
72 5
                    $schema[$role] = $definition;
73
                }
74 3
            } elseif ($this->strict) {
75 1
                throw new SchemaFileNotFoundException($file);
76
            }
77
        }
78
79 7
        return $schema !== null || $nextProvider === null ? $schema : $nextProvider->read();
80
    }
81
82 1
    public function clear(): bool
83
    {
84 1
        return false;
85
    }
86
}
87