Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

RuleTransformOperation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A processData() 0 15 3
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
    public function __construct(RuleApplier $ruleApplier, array $rules, $add)
37
    {
38
        $this->ruleApplier = $ruleApplier;
39
        $this->rules = $rules;
40
        $this->add = $add;
41
    }
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
    public function processData(DataItemInterface $item, array &$context): ItemInterface
52
    {
53
        $data = $item->getData();
54
        $newData = [];
55
56
        // We add data and don't send new data.
57
        if ($this->add) {
58
            $newData = $data;
59
        }
60
61
        foreach ($this->rules as $column => $rule) {
62
            $newData[$column] = $this->ruleApplier->apply($data, $newData, $rule['rules'], []);
63
        }
64
65
        return new DataItem($newData);
66
    }
67
}