Unwind   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 81
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reference() 0 3 1
A __construct() 0 3 1
A compile() 0 3 1
A fromValue() 0 14 3
B resolve() 0 38 6
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\Service\Arguments,
9
    Definition\Name,
10
    Services,
11
    Compilation\Service\Argument as CompiledArgument,
12
    Compilation\Service\Argument\Unwind as CompiledUnwind,
13
    Exception\ValueNotSupported,
14
    Exception\ArgumentNotProvided,
15
    Exception\ArgumentNotDefined,
16
    Exception\ReferenceNotFound
17
};
18
use Innmind\Immutable\{
19
    Str,
20
    StreamInterface,
21
    Stream
22
};
23
24
final class Unwind implements Argument, HoldReference
25
{
26
    private $name;
27
28 12
    private function __construct(Name $name)
29
    {
30 12
        $this->name = $name;
31 12
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 30
    public static function fromValue($value, Arguments $arguments): Argument
37
    {
38 30
        if (!is_string($value)) {
39 12
            throw new ValueNotSupported;
40
        }
41
42 25
        $value = Str::of($value);
43
44 25
        if ((string) $value->substring(0, 4) !== '...$') {
45 14
            throw new ValueNotSupported((string) $value);
46
        }
47
48 12
        return new self(new Name(
49 12
            (string) $value->substring(4)
50
        ));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 7
    public function resolve(
57
        StreamInterface $built,
58
        Services $services
59
    ): StreamInterface {
60
        try {
61 7
            return $built->append(Stream::of(
62 7
                'mixed',
63 7
                ...$services->arguments()->get($this->name)
64
            ));
65 6
        } catch (ArgumentNotProvided $e) {
66 4
            if ($e->argument()->hasDefault()) {
67 1
                return $built->append(Stream::of(
68 1
                    'mixed',
69 1
                    ...$services->build($e->argument()->default())
70
                ));
71
            }
72
73 3
            if ($e->argument()->optional()) {
74 2
                return $built;
75
            }
76
77 1
            throw $e;
78 2
        } catch (ArgumentNotDefined $e) {
79
            //pass
80
        }
81
82
        try {
83 2
            return $built->append(Stream::of(
84 2
                'mixed',
85 2
                ...$services->dependencies()->build($this->name)
86
            ));
87 1
        } catch (ReferenceNotFound $e) {
88
            //pass
89
        }
90
91 1
        return $built->append(Stream::of(
92 1
            'mixed',
93 1
            ...$services->build($this->name)
94
        ));
95
    }
96
97 1
    public function reference(): Name
98
    {
99 1
        return $this->name;
100
    }
101
102 1
    public function compile(): CompiledArgument
103
    {
104 1
        return new CompiledUnwind($this->name);
105
    }
106
}
107