Passed
Pull Request — master (#21)
by De Cramer
01:43
created

CsvExtractOperation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A processData() 0 9 2
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\ChainOperation\Extract;
4
5
use oliverde8\AssociativeArraySimplified\AssociativeArray;
6
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
7
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
8
use Oliverde8\Component\PhpEtl\Extract\File\Csv;
9
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
10
use Oliverde8\Component\PhpEtl\Item\FileExtractedItem;
11
use Oliverde8\Component\PhpEtl\Item\GroupedItem;
12
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
13
use Oliverde8\Component\PhpEtl\Item\MixItem;
14
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
15
16
class CsvExtractOperation extends AbstractChainOperation implements DataChainOperationInterface
17
{
18
    protected string $delimiter;
19
20
    protected string $enclosure;
21
22
    protected string $escape;
23
24
    protected string $fileKey;
25
26
    public function __construct(string $delimiter, string $enclosure, string $escape, string $fileKey)
27
    {
28
        $this->delimiter = $delimiter;
29
        $this->enclosure = $enclosure;
30
        $this->escape = $escape;
31
        $this->fileKey = $fileKey;
32
    }
33
34
35
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
36
    {
37
        $filename = $item->getData();
38
        if (is_array($filename)) {
39
            $filename = AssociativeArray::getFromKey($filename, $this->fileKey);
40
        }
41
42
        $fileIterator = new Csv($filename, $this->delimiter, $this->enclosure, $this->escape);
43
        return new MixItem([new GroupedItem($fileIterator), new FileExtractedItem($filename)]);
44
    }
45
}