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

XmlExtractor::extract()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 4
rs 9.568
c 0
b 0
f 0
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