Passed
Push — develop ( fa86c6...7cec4e )
by Baptiste
02:54
created

Pair   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 20 1
A references() 0 13 3
A compile() 0 5 1
A __construct() 0 8 3
A fromValue() 0 17 3
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\Pair as CompiledPair,
13
    Exception\ValueNotSupported,
14
    Exception\LogicException,
15
};
16
use Innmind\Immutable\{
17
    Pair as ImmutablePair,
18
    StreamInterface,
19
    Str,
20
    SetInterface,
21
    Set,
22
};
23
24
final class Pair implements Argument, HoldReferences
25
{
26
    private $key;
27
    private $value;
28
29 13
    private function __construct(Argument $key, Argument $value)
30
    {
31 13
        if ($key instanceof Unwind || $value instanceof Unwind) {
32 2
            throw new LogicException;
33
        }
34
35 11
        $this->key = $key;
36 11
        $this->value = $value;
37 11
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 20
    public static function fromValue($value, Arguments $arguments): Argument
43
    {
44 20
        if (!is_string($value)) {
45 12
            throw new ValueNotSupported;
46
        }
47
48 15
        $value = Str::of($value);
49
50 15
        if (!$value->matches('~^<\S+, ?\S+>$~')) {
51 13
            throw new ValueNotSupported((string) $value);
52
        }
53
54 13
        $components = $value->capture('~^<(?<key>\S+), ?(?<value>\S+)>$~');
55
56 13
        return new self(
57 13
            $arguments->load((string) $components->get('key')),
58 13
            $arguments->load((string) $components->get('value'))
59
        );
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function resolve(
66
        StreamInterface $built,
67
        Services $services
68
    ): StreamInterface {
69
        $key = $this
70 2
            ->key
71 2
            ->resolve(
72 2
                $built->clear(),
73 2
                $services
74
            )
75 2
            ->first();
76
        $value = $this
77 2
            ->value
78 2
            ->resolve(
79 2
                $built->clear(),
80 2
                $services
81
            )
82 2
            ->first();
83
84 2
        return $built->add(new ImmutablePair($key, $value));
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 3
    public function references(): SetInterface
91
    {
92 3
        $references = Set::of(Name::class);
93
94 3
        if ($this->key instanceof HoldReference) {
95 1
            $references = $references->add($this->key->reference());
96
        }
97
98 3
        if ($this->value instanceof HoldReference) {
99 2
            $references = $references->add($this->value->reference());
100
        }
101
102 3
        return $references;
103
    }
104
105 5
    public function compile(): CompiledArgument
106
    {
107 5
        return new CompiledPair(
108 5
            $this->key->compile(),
109 5
            $this->value->compile()
110
        );
111
    }
112
}
113