Passed
Pull Request — master (#21)
by De Cramer
07:15
created

ChainMergeOperation::processStop()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 10
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->processItem($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->processItem($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