Completed
Push — develop ( b9b8dc...f49312 )
by Baptiste
02:08
created

Unwind::__construct()   A

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Argument;
5
6
use Innmind\Compose\{
7
    Definition\Service\Argument,
8
    Definition\Name,
9
    Definitions,
10
    Exception\ValueNotSupported,
11
    Exception\ArgumentNotProvided,
12
    Exception\ArgumentNotDefined
13
};
14
use Innmind\Immutable\{
15
    Str,
16
    Stream
17
};
18
use Innmind\Immutable\StreamInterface;
19
20
final class Unwind implements Argument
21
{
22
    private $name;
23
24 6
    private function __construct(Name $name)
25
    {
26 6
        $this->name = $name;
27 6
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 8
    public static function fromValue($value): Argument
33
    {
34 8
        if (!is_string($value)) {
35 1
            throw new ValueNotSupported;
36
        }
37
38 7
        $value = Str::of($value);
39
40 7
        if ((string) $value->substring(0, 4) !== '...$') {
41 1
            throw new ValueNotSupported((string) $value);
42
        }
43
44 6
        return new self(new Name(
45 6
            (string) $value->substring(4)
46
        ));
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 5
    public function resolve(
53
        StreamInterface $built,
54
        Definitions $definitions
55
    ): StreamInterface {
56
        try {
57 5
            $value = $definitions->arguments()->get($this->name);
58 4
        } catch (ArgumentNotProvided $e) {
59 3
            if ($e->argument()->hasDefault()) {
60 1
                $value = $definitions->build($e->argument()->default());
61
            }
62
63 3
            if ($e->argument()->optional()) {
64 3
                $value = [];
65
            }
66 1
        } catch (ArgumentNotDefined $e) {
67 1
            $value = $definitions->build($this->name);
68
        }
69
70 5
        return $built->append(Stream::of(
71 5
            'mixed',
72 5
            ...$value
73
        ));
74
    }
75
}
76