Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

SimpleGroupingOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\ChainOperation\Grouping;
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\ChainBreakItem;
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\Item\StopItem;
13
14
/**
15
 * Class SimpleGrouping
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2018 Oliverde8
19
 * @package Oliverde8\Component\PhpEtl\ChainOperation\Grouping
20
 */
21
class SimpleGroupingOperation extends AbstractChainOperation implements DataChainOperationInterface
22
{
23
    public $groupKey = [];
24
25
    public $groupIdentifierKey = [];
26
27
    public $data = [];
28
29
    /**
30
     * SimpleGrouping constructor.
31
     *
32
     * @param array $groupKey
33
     */
34
    public function __construct(array $groupKey, array $groupIdentifierKey = [])
35
    {
36
        $this->groupKey = $groupKey;
37
        $this->groupIdentifierKey = $groupIdentifierKey;
38
    }
39
40
41
    public function processData(DataItemInterface $item, array &$context): ItemInterface
42
    {
43
        $groupingValue = AssociativeArray::getFromKey($item->getData(), $this->groupKey);
44
45
        if (!empty($this->groupIdentifierKey)) {
46
            $groupIdValue = AssociativeArray::getFromKey($item->getData(), $this->groupIdentifierKey);
47
            $this->data[$groupingValue][$groupIdValue] = $item->getData();
48
        } else {
49
            $this->data[$groupingValue][] = $item->getData();
50
        }
51
52
53
        return new ChainBreakItem();
54
    }
55
56
    public function processStop(StopItem $stopItem, array &$context): ItemInterface
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
    public function processStop(StopItem $stopItem, /** @scrutinizer ignore-unused */ array &$context): ItemInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        if (empty($this->data)) {
59
            return $stopItem;
60
        }
61
62
        $data = $this->data;
63
        $this->data = [];
64
65
        return new GroupedItem(new \ArrayIterator($data));
66
    }
67
}