Passed
Pull Request — master (#21)
by De Cramer
02:27 queued 49s
created

CsvExtractOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
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\GroupedItem;
11
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
12
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
13
14
class CsvExtractOperation extends AbstractChainOperation implements DataChainOperationInterface
15
{
16
    protected string $delimiter;
17
18
    protected string $enclosure;
19
20
    protected string $escape;
21
22
    protected string $fileKey;
23
24
    public function __construct(string $delimiter, string $enclosure, string $escape, string $fileKey)
25
    {
26
        $this->delimiter = $delimiter;
27
        $this->enclosure = $enclosure;
28
        $this->escape = $escape;
29
        $this->fileKey = $fileKey;
30
    }
31
32
33
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
34
    {
35
        $filename = $item->getData();
36
        if (is_array($filename)) {
37
            $filename = AssociativeArray::getFromKey($filename, $this->fileKey);
38
        }
39
40
        $fileIterator = new Csv($filename, $this->delimiter, $this->enclosure, $this->escape);
41
        return new GroupedItem($fileIterator);
42
    }
43
}