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

RuleTransformOperation::processData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl\ChainOperation\Transformer;
4
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
5
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
6
use Oliverde8\Component\PhpEtl\Item\DataItem;
7
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
8
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
9
use Oliverde8\Component\RuleEngine\RuleApplier;
10
11
/**
12
 * Class RuleTransformOperation
13
 *
14
 * @author    de Cramer Oliver<[email protected]>
15
 * @copyright 2018 Oliverde8
16
 * @package Oliverde8\Component\PhpEtl\ChainOperation\Transformer
17
 */
18
class RuleTransformOperation extends AbstractChainOperation implements DataChainOperationInterface
19
{
20
    /** @var RuleApplier */
21
    protected $ruleApplier;
22
23
    /** @var array */
24
    protected $rules;
25
26
    /** @var boolean */
27
    protected $add;
28
29
    /**
30
     * RuleTransformOperation constructor.
31
     *
32
     * @param RuleApplier $ruleApplier
33
     * @param array $rules
34
     * @param boolean $add
35
     */
36 2
    public function __construct(RuleApplier $ruleApplier, array $rules, $add)
37
    {
38 2
        $this->ruleApplier = $ruleApplier;
39 2
        $this->rules = $rules;
40 2
        $this->add = $add;
41 2
    }
42
43
    /**
44
     * @param DataItemInterface $item
45
     * @param array $context
46
     *
47
     * @return ItemInterface
48
     * @throws \Oliverde8\Component\RuleEngine\Exceptions\RuleException
49
     * @throws \Oliverde8\Component\RuleEngine\Exceptions\UnknownRuleException
50
     */
51 2
    public function processData(DataItemInterface $item, array &$context): ItemInterface
52
    {
53 2
        $data = $item->getData();
54 2
        $newData = [];
55
56
        // We add data and don't send new data.
57 2
        if ($this->add) {
58 1
            $newData = $data;
59
        }
60
61 2
        foreach ($this->rules as $column => $rule) {
62 2
            $newData[$column] = $this->ruleApplier->apply($data, $newData, $rule['rules'], []);
63
        }
64
65 2
        return new DataItem($newData);
66
    }
67
}