SimpleGroupingOperation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 58
rs 10
c 2
b 0
f 0
ccs 17
cts 17
cp 1
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processData() 0 12 2
A __construct() 0 4 1
A processStop() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oliverde8\Component\PhpEtl\ChainOperation\Grouping;
6
7
use oliverde8\AssociativeArraySimplified\AssociativeArray;
8
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
9
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
10
use Oliverde8\Component\PhpEtl\Item\ChainBreakItem;
11
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
12
use Oliverde8\Component\PhpEtl\Item\GroupedItem;
13
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
14
use Oliverde8\Component\PhpEtl\Item\StopItem;
15
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
16
17
/**
18
 * Class SimpleGrouping
19
 *
20
 * @author    de Cramer Oliver<[email protected]>
21
 * @copyright 2018 Oliverde8
22
 * @package Oliverde8\Component\PhpEtl\ChainOperation\Grouping
23
 */
24
class SimpleGroupingOperation extends AbstractChainOperation implements DataChainOperationInterface
25
{
26
    /** @var string[] Key to use for grouping, if array it will be used to read recursively inside the array.  */
27
    protected $groupKey = [];
28
29
    /** @var string[] Key to identify each individual data inside the group. */
30
    protected $groupIdentifierKey = [];
31
32
    /**
33
     * Grouped data kept in memory.
34
     *
35
     * @var array
36
     */
37
    protected $data = [];
38
39
    /**
40
     * SimpleGroupingOperation constructor.
41
     *
42 2
     * @param string[] $groupKey Key to use for grouping, if array it will be used to read recursively inside the array.
43
     * @param string[] $groupIdentifierKey key to identify each individual data inside the group.
44 2
     */
45 2
    public function __construct(array $groupKey, array $groupIdentifierKey = [])
46 2
    {
47
        $this->groupKey = $groupKey;
48
        $this->groupIdentifierKey = $groupIdentifierKey;
49
    }
50
51
52 2
    /**
53
     * @inheritdoc
54 2
     */
55
    public function processData(DataItemInterface $item, ExecutionContext $context): ChainBreakItem
56 2
    {
57 2
        $groupingValue = AssociativeArray::getFromKey($item->getData(), $this->groupKey);
58 2
59
        if (!empty($this->groupIdentifierKey)) {
60 1
            $groupIdValue = AssociativeArray::getFromKey($item->getData(), $this->groupIdentifierKey);
61
            $this->data[$groupingValue][$groupIdValue] = $item->getData();
62
        } else {
63
            $this->data[$groupingValue][] = $item->getData();
64 2
        }
65
66
        return new ChainBreakItem();
67
    }
68
69
    /**
70 2
     * @inheritdoc
71
     */
72 2
    public function processStop(StopItem $stopItem, ExecutionContext $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

72
    public function processStop(StopItem $stopItem, /** @scrutinizer ignore-unused */ ExecutionContext $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...
73 2
    {
74
        if (empty($this->data)) {
75
            return $stopItem;
76 2
        }
77 2
78
        $data = $this->data;
79 2
        $this->data = [];
80
81
        return new GroupedItem(new \ArrayIterator($data));
82
    }
83
}