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

Stream::fromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Constructor;
5
6
use Innmind\Compose\{
7
    Definition\Service\Constructor,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Immutable\{
11
    Str,
12
    Stream as ImmutableStream
13
};
14
15
final class Stream implements Constructor
16
{
17
    private $type;
18
19 4
    private function __construct(string $type)
20
    {
21 4
        $this->type = $type;
22 4
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 6
    public static function fromString(Str $value): Constructor
28
    {
29 6
        if (!$value->matches('~^stream<\S+>$~')) {
30 4
            throw new ValueNotSupported((string) $value);
31
        }
32
33 4
        $components = $value->capture('~^stream<(?<type>\S+)>$~');
34
35 4
        return new self((string) $components->get('type'));
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function __invoke(...$arguments): object
42
    {
43 2
        return ImmutableStream::of($this->type, ...$arguments);
44
    }
45
}
46