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

SegmentFactory::factory()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.2655

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 9
nop 1
dl 0
loc 24
ccs 15
cts 20
cp 0.75
crap 10.2655
rs 8.0555
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A SegmentFactory::__construct() 0 3 1
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