Completed
Push — develop ( f8d1c0...c6d734 )
by Baptiste
02:31
created

Pair   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
dl 0
loc 61
ccs 28
cts 28
cp 1
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 20 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
    Definitions,
10
    Exception\ValueNotSupported,
11
    Exception\LogicException
12
};
13
use Innmind\Immutable\{
14
    Pair as ImmutablePair,
15
    StreamInterface,
16
    Str
17
};
18
19
final class Pair implements Argument
20
{
21
    private $key;
22
    private $value;
23
24 6
    private function __construct(Argument $key, Argument $value)
25
    {
26 6
        if ($key instanceof Unwind || $value instanceof Unwind) {
27 2
            throw new LogicException;
28
        }
29
30 4
        $this->key = $key;
31 4
        $this->value = $value;
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 11
    public static function fromValue($value, Arguments $arguments): Argument
38
    {
39 11
        if (!is_string($value)) {
40 5
            throw new ValueNotSupported;
41
        }
42
43 8
        $value = Str::of($value);
44
45 8
        if (!$value->matches('~^<\S+, ?\S+>$~')) {
46 7
            throw new ValueNotSupported((string) $value);
47
        }
48
49 6
        $components = $value->capture('~^<(?<key>\S+), ?(?<value>\S+)>$~');
50
51 6
        return new self(
52 6
            $arguments->load((string) $components->get('key')),
53 6
            $arguments->load((string) $components->get('value'))
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function resolve(
61
        StreamInterface $built,
62
        Definitions $definitions
63
    ): StreamInterface {
64
        $key = $this
65 2
            ->key
66 2
            ->resolve(
67 2
                $built->clear(),
68 2
                $definitions
69
            )
70 2
            ->current();
71
        $value = $this
72 2
            ->value
73 2
            ->resolve(
74 2
                $built->clear(),
75 2
                $definitions
76
            )
77 2
            ->current();
78
79 2
        return $built->add(new ImmutablePair($key, $value));
80
    }
81
}
82