Completed
Push — master ( 1f4ad9...320f5c )
by John
03:15
created

ParserDocument   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 96
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0
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 19 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\FieldParser\FieldParserFactory;
7
use \SimpleXMLElement;
8
9
class ParserDocument
10
{
11
    /**
12
     * @var FieldSorter
13
     */
14
    private $fieldSorter;
15
16
    /**
17
     * @var FieldParserFactory
18
     */
19
    private $fieldParserFactory;
20
21
    /**
22
     * @var FieldParserRegistry
23
     */
24
    private $fieldParserRegistry;
25
26
    /**
27
     * @param FieldSorter $fieldSorter
28
     * @param FieldParserFactory $fieldParserFactory
29
     * @param FieldParserRegistry $fieldParserRegistry
30
     */
31
    public function __construct(
32
        FieldSorter $fieldSorter,
33
        FieldParserFactory $fieldParserFactory,
34
        FieldParserRegistry $fieldParserRegistry
35
    ) {
36
        $this->fieldSorter = $fieldSorter;
37
        $this->fieldParserFactory = $fieldParserFactory;
38
        $this->fieldParserRegistry = $fieldParserRegistry;
39
    }
40
41
    /**
42
     * @param SimpleXMLElement $xml
43
     * @return ParserInterface[]
44
     */
45
    public function getFieldParsers(SimpleXMLElement $xml)
46
    {
47
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
48
        $fields = $subImage['Field'];
49
        if (!is_array($fields)) {
50
            // ensure we have an array even if only a single field is present
51
            $fields = [$fields];
52
        }
53
54
        $fields = $this->fieldSorter->sort($fields);
55
        $parsers = [];
56
        foreach ($fields as $field) {
57
            $parser = $this->fieldParserFactory->getFieldParser($field);
58
            // @codingStandardsIgnoreLine
59
            $parser->parse($this->fieldParserRegistry, $field, $xml->Header, $this->getScale($xml));
60
            $parsers[] = $parser;
61
        }
62
63
        return $parsers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parsers returns an array which contains values of type Graze\CiffRenderer\Parse...er\FieldParserInterface which are incompatible with the documented value type Graze\CiffRenderer\Parser\ParserInterface.
Loading history...
64
    }
65
66
    /**
67
     * @param SimpleXMLElement $xml
68
     * @return float
69
     */
70
    public function getWidth(SimpleXMLElement $xml)
71
    {
72
        // @codingStandardsIgnoreLine
73
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
74
    }
75
76
    /**
77
     * @param SimpleXMLElement $xml
78
     * @return float
79
     */
80
    public function getHeight(SimpleXMLElement $xml)
81
    {
82
        // @codingStandardsIgnoreLine
83
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
84
    }
85
86
    /**
87
     * @param SimpleXMLElement $xml
88
     * @return float
89
     */
90
    public function getScale(SimpleXMLElement $xml)
91
    {
92
        // @codingStandardsIgnoreLine
93
        return (float) $xml->SubImage->SubHeader->XRes;
94
    }
95
96
    /**
97
     * @return ParserDocument
98
     */
99
    public static function factory()
100
    {
101
        return new static(
102
            new FieldSorter(),
103
            new FieldParserFactory(),
104
            new FieldParserRegistry()
105
        );
106
    }
107
}
108