Constructors::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 18
    public function __construct(string ...$constructors)
19
    {
20 18
        $constructors = Stream::of('string', ...$constructors);
21
22 18
        if ($constructors->size() === 0) {
23 17
            $constructors = self::defaults();
24
        }
25
26 18
        $this->constructors = $constructors;
27 18
    }
28
29 16
    public function load(Str $value): Constructor
30
    {
31 16
        foreach ($this->constructors as $constructor) {
32
            try {
33 16
                $build = $constructor.'::fromString';
34
35 16
                return $build($value);
36 15
            } 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 18
    public static function defaults(): StreamInterface
48
    {
49 18
        return self::$defaults ?? self::$defaults = Stream::of(
50 1
            'string',
51 1
            Constructor\Factory::class,
52 1
            Constructor\ServiceFactory::class,
53 1
            Constructor\Set::class,
54 1
            Constructor\Stream::class,
55 1
            Constructor\Map::class,
56 1
            Constructor\Merge::class,
57 18
            Constructor\Construct::class
58
        );
59
    }
60
}
61