Passed
Push — refactor-03-simple-xml-replace ( b520e3...e5037f )
by John
07:31
created

ParserDocument::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldSorter;
6
use Graze\CiffRenderer\Parser\FieldParser\FieldParserFactory;
7
use Graze\CiffRenderer\Parser\FieldParserRegistry;
8
use Graze\CiffRenderer\Parser\SimpleXMLElementInterface;
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
    public function __construct(
33
        FieldSorter $fieldSorter,
34
        FieldParserFactory $fieldParserFactory,
35
        FieldParserRegistry $fieldParserRegistry
36
    ) {
37
        $this->fieldSorter = $fieldSorter;
38
        $this->fieldParserFactory = $fieldParserFactory;
39
        $this->fieldParserRegistry = $fieldParserRegistry;
40
    }
41
42
    /**
43
     * @param SimpleXmlElementInterface $xml
44
     * @return ParserInterface[]
45
     */
46
    public function getFieldParsers(SimpleXmlElementInterface $xml)
47
    {
48
        $subImage = (array) $xml->SubImage; // @codingStandardsIgnoreLine
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Parse...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
49
        $fields = $subImage['Field'];
50
        if (!is_array($fields)) {
51
            // ensure we have an array even if only a single field is present
52
            $fields = [$fields];
53
        }
54
55
        $fields = $this->fieldSorter->sort($fields);
56
        $parsers = [];
57
        foreach ($fields as $field) {
58
            $parser = $this->fieldParserFactory->getFieldParser($field);
59
            // @codingStandardsIgnoreLine
60
            $parser->parse($this->fieldParserRegistry, $field, $xml->Header, $this->getScale($xml));
0 ignored issues
show
Bug introduced by
Accessing Header on the interface Graze\CiffRenderer\Parse...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
            $parsers[] = $parser;
62
        }
63
64
        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...
65
    }
66
67
    /**
68
     * @param SimpleXmlElementInterface $xml
69
     * @return float
70
     */
71
    public function getWidth(SimpleXmlElementInterface $xml)
72
    {
73
        // @codingStandardsIgnoreLine
74
        return (float) $xml->SubImage->SubHeader->ImageWidth * $this->getScale($xml);
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Parse...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
    }
76
77
    /**
78
     * @param SimpleXmlElementInterface $xml
79
     * @return float
80
     */
81
    public function getHeight(SimpleXmlElementInterface $xml)
82
    {
83
        // @codingStandardsIgnoreLine
84
        return (float) $xml->SubImage->SubHeader->ImageHeight * $this->getScale($xml);
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Parse...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
85
    }
86
87
    /**
88
     * @param SimpleXmlElementInterface $xml
89
     * @return float
90
     */
91
    public function getScale(SimpleXmlElementInterface $xml)
92
    {
93
        // @codingStandardsIgnoreLine
94
        return (float) $xml->SubImage->SubHeader->XRes;
0 ignored issues
show
Bug introduced by
Accessing SubImage on the interface Graze\CiffRenderer\Parse...mpleXmlElementInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
95
    }
96
97
    /**
98
     * @return ParserDocument
99
     */
100
    public static function factory()
101
    {
102
        return new static(
103
            new FieldSorter(),
104
            new FieldParserFactory(),
105
            new FieldParserRegistry()
106
        );
107
    }
108
}
109