Passed
Pull Request — 0.3 (#22)
by jean
04:00
created

XmlExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A extract() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Extractor;
6
7
class XmlExtractor
8
{
9
    // http://drib.tech/programming/parse-large-xml-files-php
10 2
    public function extract(string $filepath, string $nodeName): \Traversable
11
    {
12 2
        $xml = new \XMLReader();
13
14 2
        if (false !== strpos($filepath, '.gz')) {
15 1
            $xml->open('compress.zlib://'.$filepath);
16
        } else {
17 1
            $xml->open($filepath);
18
        }
19
20 2
        $itemCount = 0;
21 2
        while ($xml->read()) {
22 2
            if ($nodeName != $xml->name) {
23 2
                continue;
24
            }
25
26 2
            ++$itemCount;
27 2
            $element = new \SimpleXMLElement($xml->readOuterXml());
28 2
            $data = json_decode(json_encode($element), true);
29
30 2
            yield $itemCount => $data;
31 2
            $xml->next();
32
        }
33 1
    }
34
}
35