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; |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.