ChainMergeOperation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processStop() 0 12 3
A processData() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oliverde8\Component\PhpEtl\ChainOperation;
6
7
use Oliverde8\Component\PhpEtl\ChainProcessor;
8
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
9
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
10
use Oliverde8\Component\PhpEtl\Item\MixItem;
11
use Oliverde8\Component\PhpEtl\Item\StopItem;
12
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
13
14
/**
15
 * Class ChainSplitOperation
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2018 Oliverde8
19
 * @package Oliverde8\Component\PhpEtl\ChainOperation
20
 */
21
class ChainMergeOperation extends AbstractChainOperation implements DataChainOperationInterface
22
{
23
    /** @var ChainProcessor[] */
24
    protected array $chainProcessors;
25
26
    /**
27
     * ChainSplitOperation constructor.
28
     *
29
     * @param ChainProcessor[] $chainProcessors
30
     */
31
    public function __construct(array $chainProcessors)
32
    {
33
        $this->chainProcessors = $chainProcessors;
34
    }
35
36
    public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
37
    {
38
        $returnItems = [];
39
        foreach ($this->chainProcessors as $chainProcessor) {
40
            $returnItems[] = $chainProcessor->processItemWithChain($item, 0,  $context);
41
        }
42
43
        // Nothing to process.
44
        return new MixItem($returnItems);
45
    }
46
47
    public function processStop(StopItem $item, ExecutionContext $context): ItemInterface
48
    {
49
        foreach ($this->chainProcessors as $chainProcessor) {
50
            $result = $chainProcessor->processItemWithChain($item, 0,  $context);
51
52
            if ($result !== $item) {
53
                // Return a new stop item in order to continue flushing out data with stop items.
54
                $item = new StopItem();
55
            }
56
        }
57
58
        return $item;
59
    }
60
}
61