Structures::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Config;
5
6
use Innmind\Config\Exception\{
7
    DomainException,
8
    SchemaNotParseable,
9
};
10
use Innmind\Immutable\{
11
    StreamInterface,
12
    Stream,
13
};
14
15
final class Structures
16
{
17
    private static $defaults;
18
    private $structures;
19
20 26
    public function __construct(string ...$structures)
21
    {
22 26
        $structures = Stream::of('string', ...$structures);
23
24 26
        if ($structures->size() === 0) {
25 23
            $structures = self::defaults();
26
        }
27
28 26
        $structures->foreach(static function(string $structure): void {
29 26
            $refl = new \ReflectionClass($structure);
30
31 26
            if (!$refl->implementsInterface(Structure::class)) {
32 1
                throw new DomainException($structure);
33
            }
34 26
        });
35
36 25
        $this->structures = $structures;
37 25
    }
38
39 19
    public function build(array $schema, Properties $properties): Structure
40
    {
41 19
        foreach ($this->structures as $structure) {
42
            try {
43 19
                return [$structure, 'build']($schema, $this, $properties);
44 19
            } catch (SchemaNotParseable $e) {
45
                //pass
46
            }
47
        }
48
49 4
        throw new SchemaNotParseable('', 0, $e ?? null);
50
    }
51
52
    /**
53
     * @return StreamInterface<Structure>
54
     */
55 24
    public static function defaults(): StreamInterface
56
    {
57 24
        return self::$defaults ?? self::$defaults = Stream::of(
58 1
            'string',
59 1
            Structure\Prototype::class,
60 24
            Structure\Map::class
61
        );
62
    }
63
}
64