JsonExtractOperation   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A processData() 0 14 3
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\Item\DataItemInterface;
9
use Oliverde8\Component\PhpEtl\Item\FileExtractedItem;
10
use Oliverde8\Component\PhpEtl\Item\GroupedItem;
11
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
12
use Oliverde8\Component\PhpEtl\Item\MixItem;
13
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
14
15
class JsonExtractOperation extends AbstractChainOperation implements DataChainOperationInterface
16
{
17
    protected string $fileKey;
18
19
    protected bool $scoped;
20
21
    public function __construct(string $fileKey, bool $scoped)
22
    {
23
        $this->fileKey = $fileKey;
24
        $this->scoped = $scoped;
25
    }
26
27
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
28
    {
29
        $filename = $item->getData();
30
        if (is_array($filename)) {
31
            $filename = AssociativeArray::getFromKey($filename, $this->fileKey);
32
        }
33
34
        if ($this->scoped) {
35
            $data = json_decode($context->getFileSystem()->read($filename), true);
36
        } else {
37
            $data = json_decode(file_get_contents($filename), true);
38
        }
39
40
        return new MixItem([new GroupedItem(new \ArrayIterator($data)), new FileExtractedItem($item->getData())]);
41
    }
42
}
43