Passed
Push — develop ( efe27a...afc25d )
by Baptiste
01:56
created

Argument::resolve()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 1
dl 0
loc 29
ccs 14
cts 14
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Dependency;
5
6
use Innmind\Compose\{
7
    Definition\Name,
8
    Definition\Dependency,
9
    Services,
10
    Exception\ArgumentNotProvided,
11
    Exception\ArgumentNotDefined,
12
    Exception\NameNotNamespaced,
13
    Exception\ReferenceNotFound
14
};
15
use Innmind\Immutable\Str;
16
17
final class Argument
18
{
19
    private $name;
20
    private $value;
21
    private $reference;
22
23 20
    private function __construct(Name $name)
24
    {
25 20
        $this->name = $name;
26 20
    }
27
28 20
    public static function fromValue(Name $name, $value): self
29
    {
30 20
        if (!is_string($value)) {
31 3
            $self = new self($name);
32 3
            $self->value = $value;
33
34 3
            return $self;
35
        }
36
37 18
        $value = Str::of($value);
38
39 18
        if ((string) $value->substring(0, 1) !== '$') {
40 1
            $self = new self($name);
41 1
            $self->value = (string) $value;
42
43 1
            return $self;
44
        }
45
46 17
        $self = new self($name);
47 17
        $self->reference = new Name((string) $value->substring(1));
48
49 17
        return $self;
50
    }
51
52 13
    public function name(): Name
53
    {
54 13
        return $this->name;
55
    }
56
57 14
    public function resolve(Services $services)
58
    {
59 14
        if (!$this->reference instanceof Name) {
60 2
            return $this->value;
61
        }
62
63
        try {
64 12
            return $services->arguments()->get($this->reference);
65 9
        } catch (ArgumentNotProvided $e) {
66 3
            if ($e->argument()->hasDefault()) {
67 1
                return $services->build($e->argument()->default());
68
            }
69
70 2
            if ($e->argument()->optional()) {
71 1
                return null;
72
            }
73
74 1
            throw $e;
75 6
        } catch (ArgumentNotDefined $e) {
76
            //pass
77
        }
78
79
        try {
80 6
            return $services->dependencies()->build($this->reference);
81 4
        } catch (ReferenceNotFound $e) {
82
            //pass
83
        }
84
85 4
        return $services->build($this->reference);
86
    }
87
88 7
    public function refersTo(Dependency $dependeny): bool
89
    {
90 7
        if (!$this->reference instanceof Name) {
91 2
            return false;
92
        }
93
94
        try {
95 6
            $root = $this->reference->root();
96 1
        } catch (NameNotNamespaced $e) {
97 1
            return false;
98
        }
99
100 5
        if (!$root->equals($dependeny->name())) {
101 4
            return false;
102
        }
103
104 4
        return $dependeny->has($this->reference->withoutRoot());
105
    }
106
}
107