ParserDocument   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 86.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 94
ccs 25
cts 29
cp 0.8621
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeight() 0 4 1
A factory() 0 5 1
A __construct() 0 6 1
A getWidth() 0 4 1
A getFieldParsers() 0 26 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 \SimpleXMLElement;
8
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
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
     * @param FieldSorter $fieldSorter
24
     * @param FieldParserFactory $fieldParserFactory
25
     */
26 4
    public function __construct(
27
        FieldSorter $fieldSorter,
28
        FieldParserFactory $fieldParserFactory
29
    ) {
30 4
        $this->fieldSorter = $fieldSorter;
31 4
        $this->fieldParserFactory = $fieldParserFactory;
32 4
    }
33
34
    /**
35
     * @param SimpleXMLElement $xml
36
     * @return FieldParserInterface[]
37
     */
38 1
    public function getFieldParsers(SimpleXMLElement $xml)
39
    {
40
        // some type juggling required for SimpleXMLElement to work as required
41 1
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
42 1
        $fields = $subImage['Field'];
43
44 1
        if (!is_array($fields)) {
45
            // ensure we have an array even if only a single field is present
46 1
            $fields = [$fields];
47
        }
48
49 1
        $this->fieldSorter->sort($fields);
50
51 1
        $parsers = [];
52 1
        foreach ($fields as $field) {
53 1
            $scale = $this->getScale($xml);
54 1
            $parser = $this->fieldParserFactory->getFieldParser(
55 1
                $xml->Header, // @codingStandardsIgnoreLine
56 1
                $field,
57 1
                $scale
58
            );
59
60 1
            $parsers[] = $parser;
61
        }
62
63 1
        return $parsers;
64
    }
65
66
    /**
67
     * @param SimpleXMLElement $xml
68
     * @return int
69
     */
70 1
    public function getWidth(SimpleXMLElement $xml)
71
    {
72
        // @codingStandardsIgnoreLine
73 1
        return (int) round($xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml));
74
    }
75
76
    /**
77
     * @param SimpleXMLElement $xml
78
     * @return int
79
     */
80 1
    public function getHeight(SimpleXMLElement $xml)
81
    {
82
        // @codingStandardsIgnoreLine
83 1
        return (int) round($xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml));
84
    }
85
86
    /**
87
     * @param SimpleXMLElement $xml
88
     * @return float
89
     */
90 4
    public function getScale(SimpleXMLElement $xml)
91
    {
92
        // @codingStandardsIgnoreLine
93 4
        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
            FieldParserFactory::factory()
104
        );
105
    }
106
}
107