Mappings::apply()   C
last analyzed

Complexity

Conditions 13
Paths 86

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 13.8659

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 24
cts 29
cp 0.8276
rs 6.6166
c 0
b 0
f 0
cc 13
nc 86
nop 2
crap 13.8659

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 Mathielen\ImportEngine\Mapping;
4
5
use Ddeboer\DataImport\ValueConverter\ValueConverterInterface;
6
use Ddeboer\DataImport\ItemConverter\MappingItemConverter;
7
use Ddeboer\DataImport\ItemConverter\ItemConverterInterface;
8
use Ddeboer\DataImport\Workflow;
9
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
10
use Mathielen\ImportEngine\Mapping\Converter\Provider\ConverterProviderInterface;
11
12
class Mappings extends \ArrayObject
13
{
14
    public function getTargetFields()
15
    {
16
        $fields = array();
17
        foreach ($this as $row) {
18
            $fields[] = $row->to;
19
        }
20
21
        return $fields;
22
    }
23
24
    /**
25
     * @return Mappings
26
     */
27 7
    public function add($from, $to, $converter = null)
28
    {
29 7
        $this->getOrCreateMapping($from)->to = $to;
30
31 7
        if ($converter) {
32 4
            $this->setConverter($converter, $from);
33
        }
34
35 7
        return $this;
36
    }
37
38 4
    public function setConverter($converter, $from = null)
39
    {
40 4
        if (!(is_string($converter) || $converter instanceof ValueConverterInterface || $converter instanceof ItemConverterInterface)) {
41
            throw new \InvalidArgumentException('Converter must be an id (string) or of type ValueConverterInterface or ItemConverterInterface');
42
        }
43
44 4
        if ($from) {
45 4
            $this->getOrCreateMapping($from)->converter = $converter;
46
        } else {
47
            $this->append($converter);
48
        }
49 4
    }
50
51
    /**
52
     * @return \Mathielen\ImportEngine\Mapping\Mapping
53
     */
54 7
    private function getOrCreateMapping($from)
55
    {
56 7
        if (!isset($this[$from])) {
57 7
            $this[$from] = new Mapping($from);
58
        }
59
60 7
        return $this[$from];
61
    }
62
63
    public function get($from)
64
    {
65
        if (!isset($this[$from])) {
66
            return;
67
        }
68
69
        return $this[$from];
70
    }
71
72 15
    public function apply(Workflow $workflow, ConverterProviderInterface $converterProvider)
73
    {
74 15
        $fieldMapping = array();
75
76 15
        foreach ($this as $mapping) {
77 3
            $to = null;
78 3
            $from = null;
79 3
            $converter = null;
0 ignored issues
show
Unused Code introduced by
$converter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
81 3
            if ($mapping instanceof Mapping) {
82 3
                $to = $mapping->to;
83 3
                $from = $mapping->from;
84 3
                $converter = $mapping->converter;
85
            } else {
86
                $converter = $mapping;
87
            }
88
89 3
            if (!empty($to) && !empty($from)) {
90 3
                $fieldMapping[$from] = $to;
91
            }
92
93 3
            if ($converter) {
94 1
                if (is_string($converter)) {
95 1
                    if (!$converterProvider->has($converter)) {
96
                        throw new InvalidConfigurationException("Converter with id '$converter' not found in configured converters.");
97
                    }
98
99 1
                    $converter = $converterProvider->get($converter);
100
                }
101
102 1
                if ($converter instanceof ValueConverterInterface) {
103 1
                    $targetField = empty($to) ? $from : $to;
104
105 1
                    if (empty($targetField)) {
106
                        throw new InvalidConfigurationException('Cannot use ValueConverter '.get_class($converter).' without target-field.');
107
                    }
108
109 1
                    $workflow->addValueConverter($targetField, $converter);
110
                } elseif ($converter instanceof ItemConverterInterface) {
111
                    $workflow->addItemConverter($converter);
112
                } else {
113 3
                    throw new \LogicException('Invalid converter supplied: '.get_class($converter));
114
                }
115
            }
116
        }
117
118 15
        if (!empty($fieldMapping)) {
119 3
            $workflow->addItemConverter(new MappingItemConverter($fieldMapping));
120
        }
121 15
    }
122
}
123