Passed
Push — master ( add725...ffb802 )
by Aimeos
04:14
created

Base::getDefaultMapping()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 55
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 59
rs 8.9818

How to fix   Long Method   

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
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2025
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Common\Customer\Import\Csv;
12
13
14
/**
15
 * Common class for CSV customer import job controllers and processors.
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Base
21
	extends \Aimeos\Controller\Jobs\Base
22
{
23
	/**
24
	 * Returns the cache object for the given type
25
	 *
26
	 * @param string $type Type of the cached data
27
	 * @param string|null $name Name of the cache implementation
28
	 * @return \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Cache\Iface Cache object
29
	 * @throws \LogicException If class can't be instantiated
30
	 */
31
	protected function getCache( string $type, ?string $name = null ) : \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Cache\Iface
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Jobs\C...\Import\Csv\Cache\Iface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
	{
33
		$context = $this->context();
34
		$config = $context->config();
35
36
		if( ctype_alnum( $type ) === false ) {
37
			throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $type ), 400 );
38
		}
39
40
		$name = $name ?: $config->get( 'controller/jobs/customer/import/csv/cache/' . $type . '/name', 'Standard' );
41
42
		if( ctype_alnum( $name ) === false ) {
43
			throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 );
44
		}
45
46
		$classname = '\\Aimeos\\Controller\\Jobs\\Common\\Customer\\Import\\Csv\\Cache\\' . ucfirst( $type ) . '\\' . $name;
47
		$interface = \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Cache\Iface::class;
48
49
		return \Aimeos\Utils::create( $classname, [$context], $interface );
50
	}
51
52
53
	/**
54
	 * Returns the rows from the CSV file up to the maximum count
55
	 *
56
	 * @param resource $fh File handle to CSV file
57
	 * @param int $maxcnt Maximum number of rows that should be retrieved at once
58
	 * @param int $codePos Column position which contains the unique customer code (starting from 0)
59
	 * @return array List of arrays with customer codes as keys and list of values from the CSV file
60
	 */
61
	protected function getData( $fh, int $maxcnt, int $codePos ) : array
62
	{
63
		$data = [];
64
		$count = 0;
65
66
		while( $count++ < $maxcnt && ( $row = fgetcsv( $fh, null, ',', '"', '' ) ) !== false ) {
67
			$data[$row[$codePos]] = $row;
68
		}
69
70
		return $data;
71
	}
72
73
74
	/**
75
	 * Returns the default mapping for the CSV fields to the domain item keys
76
	 *
77
	 * Example:
78
	 *  'item' => array(
79
	 *      0 => 'customer.code', // e.g. unique EAN code
80
	 *      1 => 'customer.label', // UTF-8 encoded text, also used as customer name
81
	 *      2 => 'customer.salutation',
82
	 *      3 => 'customer.company',
83
	 *      4 => 'customer.vatid',
84
	 *      5 => 'customer.title',
85
	 *      6 => 'customer.firstname',
86
	 *      7 => 'customer.lastname',
87
	 *      8 => 'customer.address1',
88
	 *      9 => 'customer.address2',
89
	 *      10 => 'customer.address3',
90
	 *      11 => 'customer.postal',
91
	 *      12 => 'customer.city',
92
	 *      13 => 'customer.state',
93
	 *      14 => 'customer.languageid',
94
	 *      15 => 'customer.countryid',
95
	 *      16 => 'customer.telephone',
96
	 *      17 => 'customer.telefax',
97
	 *      18 => 'customer.mobile',
98
	 *      19 => 'customer.email',
99
	 *      20 => 'customer.website',
100
	 *      21 => 'customer.longitude',
101
	 *      22 => 'customer.latitude',
102
	 *      23 => 'customer.birthday',
103
	 *      24 => 'customer.status', // Status value (-2, -1, 0, 1)
104
	 *      25 => 'customer.groups',
105
	 *  ),
106
	 *	'address' => array(
107
	 *      26 => 'customer.address.salutation',
108
	 *      27 => 'customer.address.company',
109
	 *      28 => 'customer.address.vatid',
110
	 *      29 => 'customer.address.title',
111
	 *      30 => 'customer.address.firstname',
112
	 *      31 => 'customer.address.lastname',
113
	 *      32 => 'customer.address.address1',
114
	 *      33 => 'customer.address.address2',
115
	 *      34 => 'customer.address.address3',
116
	 *      35 => 'customer.address.postal',
117
	 *      36 => 'customer.address.city',
118
	 *      37 => 'customer.address.state',
119
	 *      38 => 'customer.address.languageid',
120
	 *      39 => 'customer.address.countryid',
121
	 *      40 => 'customer.address.telephone',
122
	 *      41 => 'customer.address.telefax',
123
	 *      42 => 'customer.address.mobile',
124
	 *      43 => 'customer.address.email',
125
	 *      44 => 'customer.address.website',
126
	 *      45 => 'customer.address.longitude',
127
	 *      46 => 'customer.address.latitude',
128
	 *      47 => 'customer.address.birthday',
129
	 *	),
130
	 *  'property' => array(
131
	 *      48 => 'customer.property.type',
132
	 *      49 => 'customer.property.languageid',
133
	 *      50 => 'customer.property.value',
134
	 *  ),
135
	 *
136
	 * @return array Associative list of domains as keys ("item" is special for the customer itself) and a list of
137
	 * 	positions and the domain item keys as values.
138
	 */
139
	protected function getDefaultMapping() : array
140
	{
141
		return array(
142
			'item' => array(
143
				0 => 'customer.code', // e.g. unique EAN code
144
				1 => 'customer.label', // UTF-8 encoded text, also used as customer name
145
				2 => 'customer.salutation',
146
				3 => 'customer.company',
147
				4 => 'customer.vatid',
148
				5 => 'customer.title',
149
				6 => 'customer.firstname',
150
				7 => 'customer.lastname',
151
				8 => 'customer.address1',
152
				9 => 'customer.address2',
153
				10 => 'customer.address3',
154
				11 => 'customer.postal',
155
				12 => 'customer.city',
156
				13 => 'customer.state',
157
				14 => 'customer.languageid',
158
				15 => 'customer.countryid',
159
				16 => 'customer.telephone',
160
				17 => 'customer.telefax',
161
				18 => 'customer.mobile',
162
				19 => 'customer.email',
163
				20 => 'customer.website',
164
				21 => 'customer.longitude',
165
				22 => 'customer.latitude',
166
				23 => 'customer.birthday',
167
				24 => 'customer.status', // Status value (-2, -1, 0, 1)
168
				25 => 'customer.groups',
169
			),
170
			'address' => array(
171
				26 => 'customer.address.salutation',
172
				27 => 'customer.address.company',
173
				28 => 'customer.address.vatid',
174
				29 => 'customer.address.title',
175
				30 => 'customer.address.firstname',
176
				31 => 'customer.address.lastname',
177
				32 => 'customer.address.address1',
178
				33 => 'customer.address.address2',
179
				34 => 'customer.address.address3',
180
				35 => 'customer.address.postal',
181
				36 => 'customer.address.city',
182
				37 => 'customer.address.state',
183
				38 => 'customer.address.languageid',
184
				39 => 'customer.address.countryid',
185
				40 => 'customer.address.telephone',
186
				41 => 'customer.address.telefax',
187
				42 => 'customer.address.mobile',
188
				43 => 'customer.address.email',
189
				44 => 'customer.address.website',
190
				45 => 'customer.address.longitude',
191
				46 => 'customer.address.latitude',
192
				47 => 'customer.address.birthday',
193
			),
194
			'property' => array(
195
				48 => 'customer.property.type',
196
				49 => 'customer.property.languageid',
197
				50 => 'customer.property.value',
198
			),
199
		);
200
	}
201
202
203
	/**
204
	 * Returns the mapped data from the CSV line
205
	 *
206
	 * @param array $data List of CSV fields with position as key and domain item key as value (mapped data is removed)
207
	 * @param array $mapping List of domain item keys with the CSV field position as key
208
	 * @return array List of associative arrays containing the chunked properties
209
	 */
210
	protected function getMappedChunk( array &$data, array $mapping ) : array
211
	{
212
		$idx = 0;
213
		$map = [];
214
215
		foreach( $mapping as $pos => $key )
216
		{
217
			if( isset( $map[$idx][$key] ) ) {
218
				$idx++;
219
			}
220
221
			if( isset( $data[$pos] ) ) {
222
				$map[$idx][$key] = $data[$pos];
223
			}
224
		}
225
226
		return $map;
227
	}
228
229
230
	/**
231
	 * Returns the processor object for saving the customer related information
232
	 *
233
	 * @param array $mappings Associative list of processor types as keys and index/data mappings as values
234
	 * @return \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Processor\Iface Processor object
235
	 * @throws \LogicException If class can't be instantiated
236
	 */
237
	protected function getProcessors( array $mappings ) : \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Processor\Iface
238
	{
239
		unset( $mappings['item'] );
240
241
		$context = $this->context();
242
		$config = $context->config();
243
244
		$object = new \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Processor\Done( $context, [] );
245
		$interface = \Aimeos\Controller\Jobs\Common\Customer\Import\Csv\Processor\Iface::class;
246
247
		foreach( $mappings as $type => $mapping )
248
		{
249
			if( ctype_alnum( $type ) === false ) {
250
				throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $type ), 400 );
251
			}
252
253
			$name = $config->get( 'controller/jobs/customer/import/csv/processor/' . $type . '/name', 'Standard' );
254
255
			if( ctype_alnum( $name ) === false ) {
256
				throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 );
257
			}
258
259
			$classname = '\\Aimeos\\Controller\\Jobs\\Common\\Customer\\Import\\Csv\\Processor\\' . ucfirst( $type ) . '\\' . $name;
260
261
			$object = \Aimeos\Utils::create( $classname, [$context, $mapping, $object], $interface );
262
		}
263
264
		return $object;
265
	}
266
}
267