Completed
Push — develop ( afc25d...fe7998 )
by Baptiste
01:57
created

Parameter::fromValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
crap 3
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 Parameter
18
{
19
    private $name;
20
    private $value;
21
    private $reference;
22
23 22
    private function __construct(Name $name)
24
    {
25 22
        $this->name = $name;
26 22
    }
27
28 22
    public static function fromValue(Name $name, $value): self
29
    {
30 22
        if (!is_string($value)) {
31 5
            $self = new self($name);
32 5
            $self->value = $value;
33
34 5
            return $self;
35
        }
36
37 20
        $value = Str::of($value);
38
39 20
        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 19
        $self = new self($name);
47 19
        $self->reference = new Name((string) $value->substring(1));
48
49 19
        return $self;
50
    }
51
52 15
    public function name(): Name
53
    {
54 15
        return $this->name;
55
    }
56
57 16
    public function resolve(Services $services)
58
    {
59 16
        if (!$this->reference instanceof Name) {
60 4
            return $this->value;
61
        }
62
63
        try {
64 14
            return $services->arguments()->get($this->reference);
65 11
        } 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 8
        } catch (ArgumentNotDefined $e) {
76
            //pass
77
        }
78
79
        try {
80 8
            return $services->dependencies()->build($this->reference);
81 6
        } catch (ReferenceNotFound $e) {
82
            //pass
83
        }
84
85 6
        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