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

XmlExtractorStep::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Step;
6
7
use Darkilliant\ImportBundle\Extractor\XmlExtractor;
8
use Darkilliant\ProcessBundle\State\ProcessState;
9
use Darkilliant\ProcessBundle\Step\AbstractConfigurableStep;
10
use Darkilliant\ProcessBundle\Step\IterableStepInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class XmlExtractorStep extends AbstractConfigurableStep implements IterableStepInterface
14
{
15
    private $extractor;
16
17 8
    public function __construct(XmlExtractor $extractor)
18
    {
19 8
        $this->extractor = $extractor;
20 8
    }
21
22 1
    public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver
23
    {
24 1
        $resolver->setRequired(['filepath', 'node_name']);
25
26 1
        return parent::configureOptionResolver($resolver);
27
    }
28
29 1
    public function execute(ProcessState $state)
30
    {
31 1
        $state->setIterator($this->extractor->extract(
32 1
            $state->getOptions()['filepath'],
33 1
            $state->getOptions()['node_name']
34
        ));
35 1
    }
36
37 3
    public function next(ProcessState $state)
38
    {
39 3
        $state->setData($state->getIterator()->current());
40
41 3
        $state->setContext('item_index', $state->getIterator()->key());
42
43 3
        $state->getIterator()->next();
44 3
    }
45
46 1
    public function getProgress(ProcessState $state)
47
    {
48 1
        return $state->getIterator()->key();
49
    }
50
51 1
    public function valid(ProcessState $state)
52
    {
53 1
        return $state->getIterator()->valid();
54
    }
55
56 3
    public function count(ProcessState $state)
57
    {
58 3
        $file = $state->getOptions()['filepath'];
59 3
        $node = '<'.$state->getOptions()['node_name'];
60
61 3
        if (!file_exists($file) || !is_file($file)) {
62 1
            return 0;
63
        }
64
65 2
        $cat = (false !== strpos($file, '.gz')) ? 'zcat' : 'cat';
66 2
        $count = `$cat $file | grep "$node" | wc -l`;
67
68 2
        if ((int) $count < 1) {
69 1
            return 0;
70
        }
71
72 1
        return (int) $count;
73
    }
74
}
75