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

Reference   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
dl 0
loc 51
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 19 5
A fromValue() 0 14 3
A __construct() 0 3 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
    StreamInterface,
16
    Str
17
};
18
19
final class Reference implements Argument
20
{
21
    private $name;
22
23 15
    private function __construct(Name $name)
24
    {
25 15
        $this->name = $name;
26 15
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 17
    public static function fromValue($value): Argument
32
    {
33 17
        if (!is_string($value)) {
34 1
            throw new ValueNotSupported;
35
        }
36
37 16
        $value = Str::of($value);
38
39 16
        if ((string) $value->substring(0, 1) !== '$') {
40 1
            throw new ValueNotSupported((string) $value);
41
        }
42
43 15
        return new self(new Name(
44 15
            (string) $value->substring(1)
45
        ));
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 8
    public function resolve(
52
        StreamInterface $built,
53
        Definitions $definitions
54
    ): StreamInterface {
55
        try {
56 8
            $value = $definitions->arguments()->get($this->name);
57 7
        } catch (ArgumentNotProvided $e) {
58 3
            if ($e->argument()->hasDefault()) {
59 2
                $value = $definitions->build($e->argument()->default());
60
            }
61
62 3
            if ($e->argument()->optional()) {
63 3
                $value = null;
64
            }
65 4
        } catch (ArgumentNotDefined $e) {
66 4
            $value = $definitions->build($this->name);
67
        }
68
69 8
        return $built->add($value);
70
    }
71
}
72