Completed
Push — master ( fd3130...674c45 )
by John
03:39
created

ParserDocument   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 84.38%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 100
rs 10
c 0
b 0
f 0
ccs 27
cts 32
cp 0.8438
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getHeight() 0 4 1
A factory() 0 6 1
A getWidth() 0 4 1
A getFieldParsers() 0 23 3
A getScale() 0 4 1
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldSorter;
6
use Graze\CiffRenderer\Parser\FieldParserFactory;
7
use Graze\CiffRenderer\Parser\FieldParserRegistry;
8
use \SimpleXMLElement;
9
10
class ParserDocument
11
{
12
    /**
13
     * @var FieldSorter
14
     */
15
    private $fieldSorter;
16
17
    /**
18
     * @var FieldParserFactory
19
     */
20
    private $fieldParserFactory;
21
22
    /**
23
     * @var FieldParserRegistry
24
     */
25
    private $fieldParserRegistry;
26
27
    /**
28
     * @param FieldSorter $fieldSorter
29
     * @param FieldParserFactory $fieldParserFactory
30
     * @param FieldParserRegistry $fieldParserRegistry
31
     */
32 4
    public function __construct(
33
        FieldSorter $fieldSorter,
34
        FieldParserFactory $fieldParserFactory,
35
        FieldParserRegistry $fieldParserRegistry
36
    ) {
37 4
        $this->fieldSorter = $fieldSorter;
38 4
        $this->fieldParserFactory = $fieldParserFactory;
39 4
        $this->fieldParserRegistry = $fieldParserRegistry;
40 4
    }
41
42
    /**
43
     * @param SimpleXMLElement $xml
44
     * @return ParserInterface[]
45
     */
46 1
    public function getFieldParsers(SimpleXMLElement $xml)
47
    {
48 1
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
49 1
        $fields = $subImage['Field'];
50
51 1
        if (!is_array($fields)) {
52
            // ensure we have an array even if only a single field is present
53 1
            $fields = [$fields];
54 1
        }
55
56 1
        $this->fieldSorter->sort($fields);
57
58 1
        $parsers = [];
59 1
        foreach ($fields as $field) {
60 1
            $fieldType = (string) $field->FldType; // @codingStandardsIgnoreLine
61 1
            $parser = $this->fieldParserFactory->getFieldParser($fieldType);
62
63 1
            $scale = $this->getScale($xml);
64 1
            $parser->parse($this->fieldParserRegistry, $field, $xml->Header, $scale); // @codingStandardsIgnoreLine
65 1
            $parsers[] = $parser;
66 1
        }
67
68 1
        return $parsers;
69
    }
70
71
    /**
72
     * @param SimpleXMLElement $xml
73
     * @return float
74
     */
75 1
    public function getWidth(SimpleXMLElement $xml)
76
    {
77
        // @codingStandardsIgnoreLine
78 1
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
79
    }
80
81
    /**
82
     * @param SimpleXMLElement $xml
83
     * @return float
84
     */
85 1
    public function getHeight(SimpleXMLElement $xml)
86
    {
87
        // @codingStandardsIgnoreLine
88 1
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
89
    }
90
91
    /**
92
     * @param SimpleXMLElement $xml
93
     * @return float
94
     */
95 4
    public function getScale(SimpleXMLElement $xml)
96
    {
97
        // @codingStandardsIgnoreLine
98 4
        return (float) $xml->SubImage->SubHeader->XRes;
99
    }
100
101
    /**
102
     * @return ParserDocument
103
     */
104
    public static function factory()
105
    {
106
        return new static(
107
            new FieldSorter(),
108
            new FieldParserFactory(),
109
            new FieldParserRegistry()
110
        );
111
    }
112
}
113