Completed
Push — master ( 674c45...3a5716 )
by John
02:35
created

ParserDocument::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldSorter;
6
use Graze\CiffRenderer\Parser\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
     * @param FieldSorter $fieldSorter
23
     * @param FieldParserFactory $fieldParserFactory
24
     */
25 4
    public function __construct(
26
        FieldSorter $fieldSorter,
27
        FieldParserFactory $fieldParserFactory
28
    ) {
29 4
        $this->fieldSorter = $fieldSorter;
30 4
        $this->fieldParserFactory = $fieldParserFactory;
31 4
    }
32
33
    /**
34
     * @param SimpleXMLElement $xml
35
     * @return ParserInterface[]
36
     */
37 1
    public function getFieldParsers(SimpleXMLElement $xml)
38
    {
39
        // some type juggling required for SimpleXMLElement to work as required
40 1
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
41 1
        $fields = $subImage['Field'];
42
43 1
        if (!is_array($fields)) {
44
            // ensure we have an array even if only a single field is present
45 1
            $fields = [$fields];
46 1
        }
47
48 1
        $this->fieldSorter->sort($fields);
49
50 1
        $parsers = [];
51 1
        foreach ($fields as $field) {
52 1
            $scale = $this->getScale($xml);
53 1
            $parser = $this->fieldParserFactory->getFieldParser(
54 1
                $xml->Header, // @codingStandardsIgnoreLine
55 1
                $field,
56
                $scale
57 1
            );
58
59 1
            $parsers[] = $parser;
60 1
        }
61
62 1
        return $parsers;
63
    }
64
65
    /**
66
     * @param SimpleXMLElement $xml
67
     * @return float
68
     */
69 1
    public function getWidth(SimpleXMLElement $xml)
70
    {
71
        // @codingStandardsIgnoreLine
72 1
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
73
    }
74
75
    /**
76
     * @param SimpleXMLElement $xml
77
     * @return float
78
     */
79 1
    public function getHeight(SimpleXMLElement $xml)
80
    {
81
        // @codingStandardsIgnoreLine
82 1
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
83
    }
84
85
    /**
86
     * @param SimpleXMLElement $xml
87
     * @return float
88
     */
89 4
    public function getScale(SimpleXMLElement $xml)
90
    {
91
        // @codingStandardsIgnoreLine
92 4
        return (float) $xml->SubImage->SubHeader->XRes;
93
    }
94
95
    /**
96
     * @return ParserDocument
97
     */
98
    public static function factory()
99
    {
100
        return new static(
101
            new FieldSorter(),
102
            FieldParserFactory::factory()
103
        );
104
    }
105
}
106