Passed
Push — master ( b7d939...326a3e )
by Aimeos
02:59
created

Standard::run()   B

Complexity

Conditions 9
Paths 23

Size

Total Lines 72
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 44
nc 23
nop 0
dl 0
loc 72
rs 7.6604
c 2
b 0
f 0

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), 2018-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Catalog\Import\Csv;
12
13
use \Aimeos\Base\Logger\Base as Log;
0 ignored issues
show
Bug introduced by
The type \Aimeos\Base\Logger\Base 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...
14
15
16
/**
17
 * Job controller for CSV catalog imports.
18
 *
19
 * @package Controller
20
 * @subpackage Jobs
21
 */
22
class Standard
23
	extends \Aimeos\Controller\Common\Catalog\Import\Csv\Base
24
	implements \Aimeos\Controller\Jobs\Iface
25
{
26
	/** controller/jobs/catalog/import/csv/name
27
	 * Class name of the used catalog CSV importer implementation
28
	 *
29
	 * Each default job controller can be replace by an alternative imlementation.
30
	 * To use this implementation, you have to set the last part of the class
31
	 * name as configuration value so the controller factory knows which class it
32
	 * has to instantiate.
33
	 *
34
	 * For example, if the name of the default class is
35
	 *
36
	 *  \Aimeos\Controller\Jobs\Catalog\Import\Csv\Standard
37
	 *
38
	 * and you want to replace it with your own version named
39
	 *
40
	 *  \Aimeos\Controller\Jobs\Catalog\Import\Csv\Mycsv
41
	 *
42
	 * then you have to set the this configuration option:
43
	 *
44
	 *  controller/jobs/catalog/import/csv/name = Mycsv
45
	 *
46
	 * The value is the last part of your own class name and it's case sensitive,
47
	 * so take care that the configuration value is exactly named like the last
48
	 * part of the class name.
49
	 *
50
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
51
	 * characters are possible! You should always start the last part of the class
52
	 * name with an upper case character and continue only with lower case characters
53
	 * or numbers. Avoid chamel case names like "MyCsv"!
54
	 *
55
	 * @param string Last part of the class name
56
	 * @since 2018.04
57
	 */
58
59
	/** controller/jobs/catalog/import/csv/decorators/excludes
60
	 * Excludes decorators added by the "common" option from the catalog import CSV job controller
61
	 *
62
	 * Decorators extend the functionality of a class by adding new aspects
63
	 * (e.g. log what is currently done), executing the methods of the underlying
64
	 * class only in certain conditions (e.g. only for logged in users) or
65
	 * modify what is returned to the caller.
66
	 *
67
	 * This option allows you to remove a decorator added via
68
	 * "controller/jobs/common/decorators/default" before they are wrapped
69
	 * around the job controller.
70
	 *
71
	 *  controller/jobs/catalog/import/csv/decorators/excludes = array( 'decorator1' )
72
	 *
73
	 * This would remove the decorator named "decorator1" from the list of
74
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
75
	 * "controller/jobs/common/decorators/default" to the job controller.
76
	 *
77
	 * @param array List of decorator names
78
	 * @since 2018.04
79
	 * @see controller/jobs/common/decorators/default
80
	 * @see controller/jobs/catalog/import/csv/decorators/global
81
	 * @see controller/jobs/catalog/import/csv/decorators/local
82
	 */
83
84
	/** controller/jobs/catalog/import/csv/decorators/global
85
	 * Adds a list of globally available decorators only to the catalog import CSV job controller
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
94
	 *
95
	 *  controller/jobs/catalog/import/csv/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2018.04
102
	 * @see controller/jobs/common/decorators/default
103
	 * @see controller/jobs/catalog/import/csv/decorators/excludes
104
	 * @see controller/jobs/catalog/import/csv/decorators/local
105
	 */
106
107
	/** controller/jobs/catalog/import/csv/decorators/local
108
	 * Adds a list of local decorators only to the catalog import CSV job controller
109
	 *
110
	 * Decorators extend the functionality of a class by adding new aspects
111
	 * (e.g. log what is currently done), executing the methods of the underlying
112
	 * class only in certain conditions (e.g. only for logged in users) or
113
	 * modify what is returned to the caller.
114
	 *
115
	 * This option allows you to wrap local decorators
116
	 * ("\Aimeos\Controller\Jobs\Catalog\Import\Csv\Decorator\*") around the job
117
	 * controller.
118
	 *
119
	 *  controller/jobs/catalog/import/csv/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Jobs\Catalog\Import\Csv\Decorator\Decorator2"
123
	 * only to the job controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2018.04
127
	 * @see controller/jobs/common/decorators/default
128
	 * @see controller/jobs/catalog/import/csv/decorators/excludes
129
	 * @see controller/jobs/catalog/import/csv/decorators/global
130
	 */
131
132
133
	/**
134
	 * Returns the localized name of the job.
135
	 *
136
	 * @return string Name of the job
137
	 */
138
	public function getName() : string
139
	{
140
		return $this->context()->translate( 'controller/jobs', 'Catalog import CSV' );
141
	}
142
143
144
	/**
145
	 * Returns the localized description of the job.
146
	 *
147
	 * @return string Description of the job
148
	 */
149
	public function getDescription() : string
150
	{
151
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing categories from CSV files' );
152
	}
153
154
155
	/**
156
	 * Executes the job.
157
	 *
158
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
159
	 */
160
	public function run()
161
	{
162
		if( file_exists( $this->location() ) === false ) {
163
			return;
164
		}
165
166
		$total = $errors = 0;
167
		$logger = $this->context()->logger();
168
169
		try
170
		{
171
			$mappings = $this->mapping();
172
			$codePos = $this->getCodePosition( $mappings['item'] );
173
			$processor = $this->getProcessors( $mappings );
174
			$catalogMap = $this->getCatalogMap( $this->domains() );
175
			$container = $this->getContainer();
176
			$path = $container->getName();
177
178
			$maxcnt = $this->max();
179
			$strict = $this->strict();
180
			$skiplines = $this->skip();
181
182
			$msg = sprintf( 'Started catalog import from "%1$s" (%2$s)', $path, __CLASS__ );
183
			$logger->notice( $msg, 'import/csv/catalog' );
184
185
			foreach( $container as $content )
186
			{
187
				$name = $content->getName();
188
189
				for( $i = 0; $i < $skiplines; $i++ ) {
190
					$content->next();
191
				}
192
193
				while( ( $data = $this->getData( $content, $maxcnt, $codePos ) ) !== [] )
194
				{
195
					$errcnt = $this->import( $catalogMap, $data, $mappings['item'], $processor, $strict );
196
					$chunkcnt = count( $data );
197
198
					$str = 'Imported catalog lines from "%1$s": %2$d/%3$d (%4$s)';
199
					$msg = sprintf( $str, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ );
200
					$logger->notice( $msg, 'import/csv/catalog' );
201
202
					$errors += $errcnt;
203
					$total += $chunkcnt;
204
					unset( $data );
205
				}
206
			}
207
208
			$container->close();
209
		}
210
		catch( \Exception $e )
211
		{
212
			$logger->error( 'Catalog import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/catalog' );
213
			$this->mail( 'Catalog CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
214
			throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() );
215
		}
216
217
		$str = 'Finished catalog import from "%1$s": %2$d successful, %3$s errors, %4$s total (%5$s)';
218
		$msg = sprintf( $str, $path, $total - $errors, $errors, $total, __CLASS__ );
219
		$logger->notice( $msg, 'import/csv/catalog' );
220
221
		if( $errors > 0 )
222
		{
223
			$msg = sprintf( 'Invalid catalog lines in "%1$s": %2$d/%3$d', $path, $errors, $total );
224
			$this->mail( 'Catalog CSV import error', $msg );
225
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
226
		}
227
228
		if( !empty( $backup = $this->backup() ) && @rename( $path, $backup = \Aimeos\Base\Str::strtime( $backup ) ) === false )
229
		{
230
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $path, $backup );
231
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
232
		}
233
	}
234
235
236
	/**
237
	 * Returns the directory for storing imported files
238
	 *
239
	 * @return string Directory for storing imported files
240
	 */
241
	protected function backup() : string
242
	{
243
		/** controller/jobs/catalog/import/csv/backup
244
		 * Name of the backup for sucessfully imported files
245
		 *
246
		 * After a CSV file was imported successfully, you can move it to another
247
		 * location, so it won't be imported again and isn't overwritten by the
248
		 * next file that is stored at the same location in the file system.
249
		 *
250
		 * You should use an absolute path to be sure but can be relative path
251
		 * if you absolutely know from where the job will be executed from. The
252
		 * name of the new backup location can contain placeholders understood
253
		 * by the PHP DateTime::format() method (with percent signs prefix) to
254
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
255
		 * "backup/2000-01-01". For more information about the date() placeholders,
256
		 * please have a look  into the PHP documentation of the
257
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
258
		 *
259
		 * **Note:** If no backup name is configured, the file will be removed!
260
		 *
261
		 * @param integer Name of the backup file, optionally with date/time placeholders
262
		 * @since 2018.04
263
		 * @see controller/jobs/catalog/import/csv/converter
264
		 * @see controller/jobs/catalog/import/csv/domains
265
		 * @see controller/jobs/catalog/import/csv/location
266
		 * @see controller/jobs/catalog/import/csv/mapping
267
		 * @see controller/jobs/catalog/import/csv/max-size
268
		 * @see controller/jobs/catalog/import/csv/skip-lines
269
		 * @see controller/jobs/catalog/import/csv/strict
270
		 */
271
		$backup = $this->context()->config()->get( 'controller/jobs/catalog/import/csv/backup' );
272
		return \Aimeos\Base\Str::strtime( (string) $backup );
273
	}
274
275
276
	/**
277
	 * Returns the list of domain names that should be retrieved along with the attribute items
278
	 *
279
	 * @return array List of domain names
280
	 */
281
	protected function domains() : array
282
	{
283
		/** controller/jobs/catalog/import/csv/domains
284
		 * List of item domain names that should be retrieved along with the catalog items
285
		 *
286
		 * For efficient processing, the items associated to the catalogs can be
287
		 * fetched to, minimizing the number of database queries required. To be
288
		 * most effective, the list of item domain names should be used in the
289
		 * mapping configuration too, so the retrieved items will be used during
290
		 * the import.
291
		 *
292
		 * @param array Associative list of MShop item domain names
293
		 * @since 2018.04
294
		 * @see controller/jobs/catalog/import/csv/backup
295
		 * @see controller/jobs/catalog/import/csv/converter
296
		 * @see controller/jobs/catalog/import/csv/location
297
		 * @see controller/jobs/catalog/import/csv/mapping
298
		 * @see controller/jobs/catalog/import/csv/max-size
299
		 * @see controller/jobs/catalog/import/csv/skip-lines
300
		 * @see controller/jobs/catalog/import/csv/strict
301
		 */
302
		return $this->context()->config()->get( 'controller/jobs/catalog/import/csv/domains', ['media', 'text'] );
303
	}
304
305
306
	/**
307
	 * Returns the position of the "catalog.code" column from the catalog item mapping
308
	 *
309
	 * @param array $mapping Mapping of the "item" columns with position as key and code as value
310
	 * @return int Position of the "catalog.code" column
311
	 * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "catalog.code" is found
312
	 */
313
	protected function getCodePosition( array $mapping ) : int
314
	{
315
		foreach( $mapping as $pos => $key )
316
		{
317
			if( $key === 'catalog.code' ) {
318
				return $pos;
319
			}
320
		}
321
322
		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "catalog.code" column in CSV mapping found' ) );
323
	}
324
325
326
	/**
327
	 * Opens and returns the container which includes the catalog data
328
	 *
329
	 * @return \Aimeos\MW\Container\Iface Container object
330
	 */
331
	protected function getContainer() : \Aimeos\MW\Container\Iface
332
	{
333
		$config = $this->context()->config();
334
335
		/** controller/jobs/catalog/import/csv/location
336
		 * File or directory where the content is stored which should be imported
337
		 *
338
		 * You need to configure the file or directory that acts as container
339
		 * for the CSV files that should be imported. It should be an absolute
340
		 * path to be sure but can be relative path if you absolutely know from
341
		 * where the job will be executed from.
342
		 *
343
		 * The path can point to any supported container format as long as the
344
		 * content is in CSV format, e.g.
345
		 *
346
		 * * Directory container / CSV file
347
		 * * Zip container / compressed CSV file
348
		 *
349
		 * @param string Absolute file or directory path
350
		 * @since 2018.04
351
		 * @see controller/jobs/catalog/import/csv/container/type
352
		 * @see controller/jobs/catalog/import/csv/container/content
353
		 * @see controller/jobs/catalog/import/csv/container/options
354
		 */
355
		$location = $config->get( 'controller/jobs/catalog/import/csv/location' );
356
357
		/** controller/jobs/catalog/import/csv/container/type
358
		 * Nave of the container type to read the data from
359
		 *
360
		 * The container type tells the importer how it should retrieve the data.
361
		 * There are currently three container types that support the necessary
362
		 * CSV content:
363
		 *
364
		 * * Directory
365
		 * * Zip
366
		 *
367
		 * @param string Container type name
368
		 * @since 2018.04
369
		 * @see controller/jobs/catalog/import/csv/location
370
		 * @see controller/jobs/catalog/import/csv/container/content
371
		 * @see controller/jobs/catalog/import/csv/container/options
372
		 */
373
		$container = $config->get( 'controller/jobs/catalog/import/csv/container/type', 'Directory' );
374
375
		/** controller/jobs/catalog/import/csv/container/content
376
		 * Name of the content type inside the container to read the data from
377
		 *
378
		 * The content type must always be a CSV-like format and there are
379
		 * currently two format types that are supported:
380
		 *
381
		 * * CSV
382
		 *
383
		 * @param array Content type name
384
		 * @since 2018.04
385
		 * @see controller/jobs/catalog/import/csv/location
386
		 * @see controller/jobs/catalog/import/csv/container/type
387
		 * @see controller/jobs/catalog/import/csv/container/options
388
		 */
389
		$content = $config->get( 'controller/jobs/catalog/import/csv/container/content', 'CSV' );
390
391
		/** controller/jobs/catalog/import/csv/container/options
392
		 * List of file container options for the catalog import files
393
		 *
394
		 * Some container/content type allow you to hand over additional settings
395
		 * for configuration. Please have a look at the article about
396
		 * {@link http://aimeos.org/docs/Developers/Utility/Create_and_read_files container/content files}
397
		 * for more information.
398
		 *
399
		 * @param array Associative list of option name/value pairs
400
		 * @since 2018.04
401
		 * @see controller/jobs/catalog/import/csv/location
402
		 * @see controller/jobs/catalog/import/csv/container/content
403
		 * @see controller/jobs/catalog/import/csv/container/type
404
		 */
405
		$options = $config->get( 'controller/jobs/catalog/import/csv/container/options', [] );
406
407
		if( $location === null )
408
		{
409
			$msg = sprintf( 'Required configuration for "%1$s" is missing', 'controller/jobs/catalog/import/csv/location' );
410
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
411
		}
412
413
		return \Aimeos\MW\Container\Factory::getContainer( $location, $container, $content, $options );
414
	}
415
416
417
	/**
418
	 * Returns the catalog items building the tree as list
419
	 *
420
	 * @param array $domains List of domain names whose items should be fetched too
421
	 * @return array Associative list of catalog codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
422
	 */
423
	protected function getCatalogMap( array $domains ) : array
424
	{
425
		$map = [];
426
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
427
		$search = $manager->filter()->slice( 0, 0x7fffffff );
428
429
		foreach( $manager->search( $search, $domains ) as $item ) {
430
			$map[$item->getCode()] = $item;
431
		}
432
433
		return $map;
434
	}
435
436
437
	/**
438
	 * Returns the parent ID of the catalog node for the given code
439
	 *
440
	 * @param array $catalogMap Associative list of catalog items with codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
441
	 * @param array $map Associative list of catalog item key/value pairs
442
	 * @param string $code Catalog item code of the parent category
443
	 * @return string|null ID of the parent category or null for top level nodes
444
	 */
445
	protected function getParentId( array $catalogMap, array $map, string $code ) : ?string
446
	{
447
		if( !isset( $map['catalog.parent'] ) )
448
		{
449
			$msg = sprintf( 'Required column "%1$s" not found for code "%2$s"', 'catalog.parent', $code );
450
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
451
		}
452
453
		$parent = trim( $map['catalog.parent'] );
454
455
		if( $parent != '' && !isset( $catalogMap[$parent] ) )
456
		{
457
			$msg = sprintf( 'Parent node for code "%1$s" not found', $parent );
458
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
459
		}
460
461
		return ( $parent != '' ? $catalogMap[$parent]->getId() : null );
462
	}
463
464
465
	/**
466
	 * Imports the CSV data and creates new categories or updates existing ones
467
	 *
468
	 * @param array &$catalogMap Associative list of catalog items with codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
469
	 * @param array $data Associative list of import data as index/value pairs
470
	 * @param array $mapping Associative list of positions and domain item keys
471
	 * @param \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $processor Processor object
472
	 * @param bool $strict Log columns not mapped or silently ignore them
473
	 * @return int Number of catalogs that couldn't be imported
474
	 * @throws \Aimeos\Controller\Jobs\Exception
475
	 */
476
	protected function import( array &$catalogMap, array $data, array $mapping,
477
		\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $processor, bool $strict ) : int
478
	{
479
		$errors = 0;
480
		$context = $this->context();
481
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
482
483
		foreach( $data as $code => $list )
484
		{
485
			$manager->begin();
486
487
			try
488
			{
489
				$code = trim( $code );
490
491
				if( isset( $catalogMap[$code] ) ) {
492
					$item = $catalogMap[$code];
493
				} else {
494
					$item = $manager->create();
495
				}
496
497
				$map = $this->getMappedChunk( $list, $mapping );
498
499
				if( isset( $map[0] ) )
500
				{
501
					$map = $map[0]; // there can only be one chunk for the base catalog data
502
					$map['catalog.config'] = json_decode( $map['catalog.config'] ?? '[]', true ) ?: [];
503
					$parentid = $this->getParentId( $catalogMap, $map, $code );
504
					$item->fromArray( $map, true );
505
506
					if( isset( $catalogMap[$code] ) )
507
					{
508
						$manager->move( $item->getId(), $item->getParentId(), $parentid );
509
						$item = $manager->save( $item );
510
					}
511
					else
512
					{
513
						$item = $manager->insert( $item, $parentid );
514
					}
515
516
					$list = $processor->process( $item, $list );
517
					$catalogMap[$code] = $item;
518
519
					$manager->save( $item );
520
				}
521
522
				$manager->commit();
523
			}
524
			catch( \Exception $e )
525
			{
526
				$manager->rollback();
527
528
				$str = 'Unable to import catalog with code "%1$s": %2$s';
529
				$msg = sprintf( $str, $code, $e->getMessage() . "\n" . $e->getTraceAsString() );
530
				$context->logger()->error( $msg, 'import/csv/catalog' );
531
532
				$errors++;
533
			}
534
535
			if( $strict && !empty( $list ) ) {
536
				$context->logger()->error( 'Not imported: ' . print_r( $list, true ), 'import/csv/catalog' );
0 ignored issues
show
Bug introduced by
Are you sure print_r($list, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

536
				$context->logger()->error( 'Not imported: ' . /** @scrutinizer ignore-type */ print_r( $list, true ), 'import/csv/catalog' );
Loading history...
537
			}
538
		}
539
540
		return $errors;
541
	}
542
543
544
	/**
545
	 * Returns the path to the directory with the CSV file
546
	 *
547
	 * @return string Path to the directory with the CSV file
548
	 */
549
	protected function location() : string
550
	{
551
		/** controller/jobs/catalog/import/csv/location
552
		 * File or directory where the content is stored which should be imported
553
		 *
554
		 * You need to configure the CSV file or directory with the CSV files that
555
		 * should be imported. It should be an absolute path to be sure but can be
556
		 * relative path if you absolutely know from where the job will be executed
557
		 * from.
558
		 *
559
		 * @param string Relative path to the CSV files
560
		 * @since 2015.08
561
		 * @see controller/jobs/catalog/import/csv/backup
562
		 * @see controller/jobs/catalog/import/csv/converter
563
		 * @see controller/jobs/catalog/import/csv/domains
564
		 * @see controller/jobs/catalog/import/csv/location
565
		 * @see controller/jobs/catalog/import/csv/mapping
566
		 * @see controller/jobs/catalog/import/csv/max-size
567
		 * @see controller/jobs/catalog/import/csv/skip-lines
568
		 */
569
		return (string) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/location', 'catalog' );
570
	}
571
572
573
	/**
574
	 * Returns the CSV column mapping
575
	 *
576
	 * @return array CSV column mapping
577
	 */
578
	protected function mapping() : array
579
	{
580
		/** controller/jobs/catalog/import/csv/mapping
581
		 * List of mappings between the position in the CSV file and item keys
582
		 *
583
		 * The importer have to know which data is at which position in the CSV
584
		 * file. Therefore, you need to specify a mapping between each position
585
		 * and the MShop domain item key (e.g. "catalog.code") it represents.
586
		 *
587
		 * You can use all domain item keys which are used in the fromArray()
588
		 * methods of the item classes.
589
		 *
590
		 * These mappings are grouped together by their processor names, which
591
		 * are responsible for importing the data, e.g. all mappings in "item"
592
		 * will be processed by the base catalog importer while the mappings in
593
		 * "text" will be imported by the text processor.
594
		 *
595
		 * @param array Associative list of processor names and lists of key/position pairs
596
		 * @since 2018.04
597
		 * @see controller/jobs/catalog/import/csv/backup
598
		 * @see controller/jobs/catalog/import/csv/converter
599
		 * @see controller/jobs/catalog/import/csv/domains
600
		 * @see controller/jobs/catalog/import/csv/location
601
		 * @see controller/jobs/catalog/import/csv/max-size
602
		 * @see controller/jobs/catalog/import/csv/skip-lines
603
		 * @see controller/jobs/catalog/import/csv/strict
604
		 */
605
		$map = (array) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/mapping', $this->getDefaultMapping() );
606
607
		if( !isset( $map['item'] ) || !is_array( $map['item'] ) )
608
		{
609
			$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' );
610
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
611
		}
612
613
		return $map;
614
	}
615
616
617
	/**
618
	 * Returns the maximum number of CSV rows to import at once
619
	 *
620
	 * @return int Maximum number of CSV rows to import at once
621
	 */
622
	protected function max() : int
623
	{
624
		/** controller/jobs/catalog/import/csv/max-size
625
		 * Maximum number of CSV rows to import at once
626
		 *
627
		 * It's more efficient to read and import more than one row at a time
628
		 * to speed up the import. Usually, the bigger the chunk that is imported
629
		 * at once, the less time the importer will need. The downside is that
630
		 * the amount of memory required by the import process will increase as
631
		 * well. Therefore, it's a trade-off between memory consumption and
632
		 * import speed.
633
		 *
634
		 * @param integer Number of rows
635
		 * @since 2018.04
636
		 * @see controller/jobs/catalog/import/csv/backup
637
		 * @see controller/jobs/catalog/import/csv/converter
638
		 * @see controller/jobs/catalog/import/csv/domains
639
		 * @see controller/jobs/catalog/import/csv/location
640
		 * @see controller/jobs/catalog/import/csv/mapping
641
		 * @see controller/jobs/catalog/import/csv/skip-lines
642
		 * @see controller/jobs/catalog/import/csv/strict
643
		 */
644
		return (int) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/max-size', 1000 );
645
	}
646
647
648
	/**
649
	 * Returns the number of rows skipped in front of each CSV files
650
	 *
651
	 * @return int Number of rows skipped in front of each CSV files
652
	 */
653
	protected function skip() : int
654
	{
655
		/** controller/jobs/catalog/import/csv/skip-lines
656
		 * Number of rows skipped in front of each CSV files
657
		 *
658
		 * Some CSV files contain header information describing the content of
659
		 * the column values. These data is for informational purpose only and
660
		 * can't be imported into the database. Using this option, you can
661
		 * define the number of lines that should be left out before the import
662
		 * begins.
663
		 *
664
		 * @param integer Number of rows
665
		 * @since 2015.08
666
		 * @see controller/jobs/catalog/import/csv/backup
667
		 * @see controller/jobs/catalog/import/csv/converter
668
		 * @see controller/jobs/catalog/import/csv/domains
669
		 * @see controller/jobs/catalog/import/csv/location
670
		 * @see controller/jobs/catalog/import/csv/mapping
671
		 * @see controller/jobs/catalog/import/csv/max-size
672
		 * @see controller/jobs/catalog/import/csv/strict
673
		 */
674
		return (int) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/skip-lines', 0 );
675
	}
676
677
678
	/**
679
	 * Returns if all columns from the file should be logged that are not mapped and therefore not imported
680
	 */
681
	protected function strict() : bool
682
	{
683
		/** controller/jobs/catalog/import/csv/strict
684
		 * Log all columns from the file that are not mapped and therefore not imported
685
		 *
686
		 * Depending on the mapping, there can be more columns in the CSV file
687
		 * than those which will be imported. This can be by purpose if you want
688
		 * to import only selected columns or if you've missed to configure one
689
		 * or more columns. This configuration option will log all columns that
690
		 * have not been imported if set to true. Otherwise, the left over fields
691
		 * in the imported line will be silently ignored.
692
		 *
693
		 * @param boolen True if not imported columns should be logged, false if not
694
		 * @since 2015.08
695
		 * @see controller/jobs/catalog/import/csv/backup
696
		 * @see controller/jobs/catalog/import/csv/converter
697
		 * @see controller/jobs/catalog/import/csv/domains
698
		 * @see controller/jobs/catalog/import/csv/location
699
		 * @see controller/jobs/catalog/import/csv/mapping
700
		 * @see controller/jobs/catalog/import/csv/max-size
701
		 * @see controller/jobs/catalog/import/csv/skip-lines
702
		 */
703
		return (bool) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/strict', true );
704
	}
705
}
706