|
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']); |
|
27
|
|
|
|
|
28
|
1 |
|
return parent::configureOptionResolver($resolver); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
4 |
|
public function execute(ProcessState $state) |
|
32
|
|
|
{ |
|
33
|
4 |
|
$this->iterator = $this->extractor->extract( |
|
34
|
4 |
|
$state->getOptions()['filepath'], |
|
35
|
4 |
|
$state->getOptions()['delimiter'], |
|
36
|
4 |
|
$state->getOptions()['colums_names'] |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
4 |
|
$state->setContext('filepath', $state->getOptions()['filepath']); |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function next(ProcessState $state) |
|
43
|
|
|
{ |
|
44
|
2 |
|
$state->setContext('line_csv', $this->iterator->key()); |
|
45
|
2 |
|
$state->setData($this->iterator->current()); |
|
46
|
|
|
|
|
47
|
2 |
|
$this->iterator->next(); |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function count(ProcessState $state) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$file = $state->getOptions()['filepath']; |
|
53
|
|
|
|
|
54
|
1 |
|
return (int) `cat $file | wc -l`; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function valid(ProcessState $state) |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->iterator->valid(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function getProgress(ProcessState $state) |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->iterator->key(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function describe(ProcessState $state) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$state->info('iterate on each line of {filepath} and transform into array', $state->getOptions()); |
|
70
|
1 |
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|