recursivelyConvertValues()   C
last analyzed

Complexity

Conditions 11
Paths 12

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
rs 5.2653
cc 11
eloc 24
nc 12
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Jh\DataImportMagento;
4
5
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
6
use Ddeboer\DataImport\ValueConverter\ValueConverterInterface;
7
use Ddeboer\DataImport\Workflow;
8
9
/**
10
 * Class NestedValueConverterWorkflow
11
 * @package Ddeboer\DataImport
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class NestedValueConverterWorkflow extends Workflow
15
{
16
17
    /**
18
     * Add a value converter to the workflow
19
     *
20
     * @param string|array            $field     Field
21
     * @param ValueConverterInterface $converter ValueConverter
22
     *
23
     * @return $this
24
     */
25
    public function addValueConverter($field, ValueConverterInterface $converter)
26
    {
27
        //allow to attach the same converter to multiple fields in one go
28
        if (is_array($field)) {
29
            $fields = $field;
30
            foreach ($fields as $field) {
31
                $this->valueConverters[$field][] = $converter;
32
            }
33
        } else {
34
            $this->valueConverters[$field][] = $converter;
35
        }
36
37
        return $this;
38
    }
39
40
    /**
41
     * Convert the item
42
     *
43
     * @param string $item Original item values
44
     *
45
     * @return array                   Converted item values
46
     * @throws UnexpectedTypeException
47
     */
48
    protected function convertItem($item)
49
    {
50
        foreach ($this->itemConverters as $converter) {
51
            $item = $converter->convert($item);
52
            if ($item && !(is_array($item) || ($item instanceof \ArrayAccess && $item instanceof \Traversable))) {
53
                throw new UnexpectedTypeException($item, 'false or array');
54
            }
55
56
            if (!$item) {
57
                return $item;
58
            }
59
        }
60
61
        if ($item && !(is_array($item) || ($item instanceof \ArrayAccess && $item instanceof \Traversable))) {
62
            throw new UnexpectedTypeException($item, 'false or array');
63
        }
64
65
        foreach ($this->valueConverters as $property => $converters) {
66
            //is this is targeting a nested field
67
            if (strpos($property, '/') !== false) {
68
                $properties = explode('/', $property);
69
                $item = $this->recursivelyConvertValues($properties, $item, $converters);
70
71
            } else {
72
                $item = $this->recursivelyConvertValues([$property], $item, $converters);
73
            }
74
        }
75
76
        return $item;
77
    }
78
79
    /**
80
     * Recursively run value converters on nested data
81
     *
82
     * @param array $properties
83
     * @param array $data
84
     * @param array $converters
85
     * @return array
86
     */
87
    protected function recursivelyConvertValues(array $properties, array $data, array $converters)
88
    {
89
        $property = array_shift($properties);
90
        $isCollection = false;
91
        if (substr($property, -2) === "[]") {
92
            $isCollection = true;
93
            $property = substr($property, 0, -2);
94
        }
95
96
        if (!count($properties)) {
97
            //this is the deepest field
98
99
            //apply to all properties
100
            if ($property === '*') {
101
                $data = array_map(function ($value) use ($converters) {
102
                    foreach ($converters as $converter) {
103
                        $value = $converter->convert($value);
104
                    }
105
                    return $value;
106
                }, $data);
107
            } elseif (isset($data[$property]) || array_key_exists($property, $data)) {
108
                //This is an associative array
109
                foreach ($converters as $converter) {
110
                    $data[$property] = $converter->convert($data[$property]);
111
                }
112
            }
113
        } else {
114
            if (isset($data[$property])) {
115
                if ($isCollection) {
116
                    foreach ($data[$property] as $key => $item) {
117
                        $data[$property][$key] = $this->recursivelyConvertValues($properties, $item, $converters);
118
                    }
119
120
                } else {
121
                    $data[$property] = $this->recursivelyConvertValues($properties, $data[$property], $converters);
122
                }
123
            }
124
        }
125
        return $data;
126
    }
127
}
128