Passed
Push — master ( 3581f7...005bcc )
by De Cramer
05:10 queued 03:31
created

CsvExtractOperation::processData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 13
rs 10
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
    protected bool $scoped;
27
28
    public function __construct(string $delimiter, string $enclosure, string $escape, string $fileKey, bool $scoped)
29
    {
30
        $this->delimiter = $delimiter;
31
        $this->enclosure = $enclosure;
32
        $this->escape = $escape;
33
        $this->fileKey = $fileKey;
34
        $this->scoped = $scoped;
35
    }
36
37
38
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
39
    {
40
        $filename = $item->getData();
41
        if (is_array($filename)) {
42
            $filename = AssociativeArray::getFromKey($filename, $this->fileKey);
43
        }
44
45
        $fileIterator = new Csv($filename, $this->delimiter, $this->enclosure, $this->escape);
46
        if ($this->scoped) {
47
            $context->getFileSystem()->readStream($filename);
48
        }
49
50
        return new MixItem([new GroupedItem($fileIterator), new FileExtractedItem($filename)]);
51
    }
52
}