Passed
Push — master ( 2bfb13...b8de7b )
by De Cramer
02:14
created

SimpleGroupingOperation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processData() 0 13 2
A __construct() 0 4 1
A processStop() 0 10 2
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 2
    public function __construct(array $groupKey, array $groupIdentifierKey = [])
35
    {
36 2
        $this->groupKey = $groupKey;
37 2
        $this->groupIdentifierKey = $groupIdentifierKey;
38 2
    }
39
40
41 2
    public function processData(DataItemInterface $item, array &$context): ItemInterface
42
    {
43 2
        $groupingValue = AssociativeArray::getFromKey($item->getData(), $this->groupKey);
44
45 2
        if (!empty($this->groupIdentifierKey)) {
46 2
            $groupIdValue = AssociativeArray::getFromKey($item->getData(), $this->groupIdentifierKey);
47 2
            $this->data[$groupingValue][$groupIdValue] = $item->getData();
48
        } else {
49 1
            $this->data[$groupingValue][] = $item->getData();
50
        }
51
52
53 2
        return new ChainBreakItem();
54
    }
55
56 2
    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 2
        if (empty($this->data)) {
59 2
            return $stopItem;
60
        }
61
62 2
        $data = $this->data;
63 2
        $this->data = [];
64
65 2
        return new GroupedItem(new \ArrayIterator($data));
66
    }
67
}