Stream::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
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
    Lazy\Stream as LazyStream,
10
    Compilation\Service\Constructor as CompiledConstructor,
11
    Compilation\Service\Constructor\Stream as CompiledStream,
12
    Compilation\Service\Argument as CompiledArgument
13
};
14
use Innmind\Immutable\Str;
15
16
final class Stream implements Constructor
17
{
18
    private $type;
19
20 11
    private function __construct(string $type)
21
    {
22 11
        $this->type = $type;
23 11
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 16
    public static function fromString(Str $value): Constructor
29
    {
30 16
        if (!$value->matches('~^stream<\S+>$~')) {
31 12
            throw new ValueNotSupported((string) $value);
32
        }
33
34 11
        $components = $value->capture('~^stream<(?<type>\S+)>$~');
35
36 11
        return new self((string) $components->get('type'));
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    public function __invoke(...$arguments): object
43
    {
44 3
        return new LazyStream($this->type, ...$arguments);
45
    }
46
47 5
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
48
    {
49 5
        return new CompiledStream($this->type, ...$arguments);
50
    }
51
52 2
    public function __toString(): string
53
    {
54 2
        return sprintf('stream<%s>', $this->type);
55
    }
56
}
57