Passed
Push — master ( 13ac76...f27e95 )
by Chema
12:22 queued 12s
created

TransactionMessage::withSegments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser;
6
7
use EdifactParser\Segments\SegmentInterface;
8
9
final class TransactionMessage
10
{
11
    /**
12
     * First string: segment key
13
     * Second key: sub segment key.
14
     *
15
     * @psalm-var array<string, array<string,SegmentInterface>>
16
     */
17
    private array $segments = [];
18
19 6
    public static function withSegments(SegmentInterface...$segments): self
20
    {
21 6
        $self = new self();
22
23 6
        foreach ($segments as $segment) {
24 6
            $self->addSegment($segment);
25
        }
26
27 6
        return $self;
28
    }
29
30 9
    public function addSegment(SegmentInterface $segment): void
31
    {
32 9
        $name = $segment->name();
33
34 9
        if (!isset($this->segments[$name])) {
35 9
            $this->segments[$name] = [];
36
        }
37
38 9
        $this->segments[$name][$segment->subSegmentKey()] = $segment;
39 9
    }
40
41 5
    public function segments(): array
42
    {
43 5
        return $this->segments;
44
    }
45
}
46