Passed
Push — master ( 8f83e8...e4d779 )
by Chema
02:15
created

SegmentFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 84.38%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 52
rs 10
c 0
b 0
f 0
ccs 27
cts 32
cp 0.8438
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B segmentFromArray() 0 30 10
A __construct() 0 3 1
A customSegment() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdifactParser\Segments;
6
7
final class SegmentFactory
8
{
9
    /** @var CustomSegmentFactoryInterface|null */
10
    private $customSegmentsFactory;
11
12 7
    public function __construct(?CustomSegmentFactoryInterface $customSegmentsFactory)
13
    {
14 7
        $this->customSegmentsFactory = $customSegmentsFactory;
15 7
    }
16
17 7
    public function segmentFromArray(array $rawArray): SegmentInterface
18
    {
19 7
        $customSegment = $this->customSegment($rawArray);
20
21 7
        if ($customSegment) {
22 1
            return $customSegment;
23
        }
24
25 7
        $name = $rawArray[0];
26
27 7
        switch ($name) {
28 7
            case 'UNH':
29 7
                return new UNHMessageHeader($rawArray);
30 6
            case 'DTM':
31
                return new DTMDateTimePeriod($rawArray);
32 6
            case 'NAD':
33
                return new NADNameAddress($rawArray);
34 6
            case 'MEA':
35
                return new MEADimensions($rawArray);
36 6
            case 'CNT':
37 3
                return new CNTControl($rawArray);
38 6
            case 'PCI':
39
                return new PCIPackageId($rawArray);
40 6
            case 'BGM':
41
                return new BGMBeginningOfMessage($rawArray);
42 6
            case 'UNT':
43 6
                return new UNTMessageFooter($rawArray);
44
        }
45
46 5
        return new UnknownSegment($rawArray);
47
    }
48
49 7
    private function customSegment(array $rawArray): ?SegmentInterface
50
    {
51 7
        if ($this->customSegmentsFactory) {
52 1
            $segment = $this->customSegmentsFactory->segmentFromArray($rawArray);
53 1
            if ($segment) {
54 1
                return $segment;
55
            }
56
        }
57
58 7
        return null;
59
    }
60
}
61