Completed
Push — develop ( f8d1c0...c6d734 )
by Baptiste
02:31
created

Constructors   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 43
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A load() 0 13 3
A defaults() 0 9 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service;
5
6
use Innmind\Compose\Exception\ValueNotSupported;
7
use Innmind\Immutable\{
8
    StreamInterface,
9
    Stream,
10
    Str
11
};
12
13
final class Constructors
14
{
15
    private static $defaults;
16
    private $constructors;
17
18 8
    public function __construct(string ...$constructors)
19
    {
20 8
        $constructors = Stream::of('string', ...$constructors);
21
22 8
        if ($constructors->size() === 0) {
23 7
            $constructors = self::defaults();
24
        }
25
26 8
        $this->constructors = $constructors;
27 8
    }
28
29 7
    public function load(Str $value): Constructor
30
    {
31 7
        foreach ($this->constructors as $constructor) {
32
            try {
33 7
                $build = $constructor.'::fromString';
34
35 7
                return $build($value);
36 6
            } catch (ValueNotSupported $e) {
37
                //pass
38
            }
39
        }
40
41 1
        throw new ValueNotSupported(var_export($value, true));
42
    }
43
44
    /**
45
     * @return StreamInterface<string>
46
     */
47 8
    public static function defaults(): StreamInterface
48
    {
49 8
        return self::$defaults ?? self::$defaults = Stream::of(
50 1
            'string',
51 1
            Constructor\Factory::class,
52 1
            Constructor\Set::class,
53 1
            Constructor\Stream::class,
54 1
            Constructor\Map::class,
55 8
            Constructor\Construct::class
56
        );
57
    }
58
}
59