ValueConverterStep::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Ddeboer\DataImport\Step;
4
5
use Ddeboer\DataImport\Step;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
8
/**
9
 * @author Markus Bachmann <[email protected]>
10
 */
11
class ValueConverterStep implements Step
12
{
13
    /**
14
     * @var array
15
     */
16
    private $converters = [];
17
18
    /**
19
     * @param string   $property
20
     * @param callable $converter
21
     *
22
     * @return $this
23
     */
24 2
    public function add($property, callable $converter)
25
    {
26 2
        $this->converters[$property][] = $converter;
27
28 2
        return $this;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function process(&$item)
35
    {
36 2
        $accessor = new PropertyAccessor();
37
38 2
        foreach ($this->converters as $property => $converters) {
39 2
            foreach ($converters as $converter) {
40 2
                $orgValue = $accessor->getValue($item, $property);
41 2
                $value = call_user_func($converter, $orgValue);
42 2
                $accessor->setValue($item,$property,$value);
43 2
            }
44 2
        }
45
46 2
        return true;
47
    }
48
}
49