CsvExtractorStep   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 61
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A next() 0 6 1
A describe() 0 3 1
A count() 0 5 1
A valid() 0 3 1
A configureOptionResolver() 0 6 1
A execute() 0 10 1
A getProgress() 0 3 1
1
<?php
2
3
namespace Darkilliant\ImportBundle\Step;
4
5
use Darkilliant\ImportBundle\Extractor\CsvExtractor;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Darkilliant\ProcessBundle\State\ProcessState;
8
use Darkilliant\ProcessBundle\Step\AbstractConfigurableStep;
9
use Darkilliant\ProcessBundle\Step\IterableStepInterface;
10
11
class CsvExtractorStep extends AbstractConfigurableStep implements IterableStepInterface
12
{
13
    /** @var CsvExtractor */
14
    private $extractor;
15
16
    /** @var \Generator */
17
    private $iterator;
18
19 8
    public function __construct(CsvExtractor $extractor)
20
    {
21 8
        $this->extractor = $extractor;
22 8
    }
23
24 1
    public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver
25
    {
26 1
        $resolver->setRequired(['filepath', 'delimiter', 'colums_names', 'skip_first_line']);
27 1
        $resolver->setDefault('skip_first_line', true);
28
29 1
        return parent::configureOptionResolver($resolver);
30
    }
31
32 4
    public function execute(ProcessState $state)
33
    {
34 4
        $this->iterator = $this->extractor->extract(
35 4
            $state->getOption('filepath'),
36 4
            $state->getOption('delimiter'),
37 4
            $state->getOption('colums_names'),
38 4
            $state->getOption('skip_first_line', true)
39
        );
40
41 4
        $state->setContext('filepath', $state->getOption('filepath'));
42 4
    }
43
44 2
    public function next(ProcessState $state)
45
    {
46 2
        $state->setContext('line_csv', $this->iterator->key());
47 2
        $state->setData($this->iterator->current());
48
49 2
        $this->iterator->next();
50 2
    }
51
52 1
    public function count(ProcessState $state)
53
    {
54 1
        $file = $state->getOptions()['filepath'];
55
56 1
        return (int) `cat $file | wc -l`;
57
    }
58
59 1
    public function valid(ProcessState $state)
60
    {
61 1
        return $this->iterator->valid();
62
    }
63
64 1
    public function getProgress(ProcessState $state)
65
    {
66 1
        return $this->iterator->key();
67
    }
68
69 1
    public function describe(ProcessState $state)
70
    {
71 1
        $state->info('iterate on each line of {filepath} and transform into array', $state->getOptions());
72 1
    }
73
}
74