Completed
Pull Request — master (#19)
by John
06:15 queued 03:47
created

ParserDocument   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 90
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 7

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 13 2
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 = $this->fieldSorter->sort($subImage['Field']);
49
        $parsers = [];
50
        foreach ($fields as $field) {
51
            $parser = $this->fieldParserFactory->getFieldParser($field);
52
            // @codingStandardsIgnoreLine
53
            $parser->parse($this->fieldParserRegistry, $field, $xml->Header, $this->getScale($xml));
54
            $parsers[] = $parser;
55
        }
56
57
        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...
58
    }
59
60
    /**
61
     * @param SimpleXMLElement $xml
62
     * @return float
63
     */
64
    public function getWidth(SimpleXMLElement $xml)
65
    {
66
        // @codingStandardsIgnoreLine
67
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
68
    }
69
70
    /**
71
     * @param SimpleXMLElement $xml
72
     * @return float
73
     */
74
    public function getHeight(SimpleXMLElement $xml)
75
    {
76
        // @codingStandardsIgnoreLine
77
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
78
    }
79
80
    /**
81
     * @param SimpleXMLElement $xml
82
     * @return float
83
     */
84
    public function getScale(SimpleXMLElement $xml)
85
    {
86
        // @codingStandardsIgnoreLine
87
        return (float) $xml->SubImage->SubHeader->XRes;
88
    }
89
90
    /**
91
     * @return ParserDocument
92
     */
93
    public static function factory()
94
    {
95
        return new static(
96
            new FieldSorter(),
97
            new FieldParserFactory(),
98
            new FieldParserRegistry()
99
        );
100
    }
101
}
102