Standard   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 568
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 112
dl 0
loc 568
rs 9.36
c 3
b 0
f 0
wmc 38

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
A backup() 0 31 1
A getCodePosition() 0 10 3
A getCategories() 0 11 2
A getParentId() 0 17 5
B run() 0 40 8
A domains() 0 21 1
A skip() 0 21 1
A location() 0 22 1
A import() 0 45 4
A mapping() 0 35 3
A max() 0 22 1
B importCategories() 0 53 6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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\Jobs\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
		$context = $this->context();
163
		$logger = $context->logger();
164
165
		try
166
		{
167
			$errors = 0;
168
			$fs = $context->fs( 'fs-import' );
169
			$site = $context->locale()->getSiteItem()->getCode();
170
			$location = $this->location() . '/' . $site;
171
172
			if( $fs->isDir( $location ) === false ) {
173
				return;
174
			}
175
176
			$logger->info( sprintf( 'Started catalog import from "%1$s"', $location ), 'import/csv/catalog' );
177
178
			foreach( map( $fs->scan( $location ) )->sort() as $filename )
179
			{
180
				$path = $location . '/' . $filename;
181
182
				if( $filename[0] === '.' || $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) {
183
					continue;
184
				}
185
186
				$errors = $this->import( $path );
187
			}
188
189
			if( $errors > 0 ) {
190
				$this->mail( 'Catalog CSV import', sprintf( 'Invalid catalog lines during import: %1$d', $errors ) );
191
			}
192
193
			$logger->info( sprintf( 'Finished catalog import from "%1$s"', $location ), 'import/csv/catalog' );
194
		}
195
		catch( \Exception $e )
196
		{
197
			$logger->error( 'Catalog import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/catalog' );
198
			$this->mail( 'Catalog CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
199
			throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() );
200
		}
201
	}
202
203
204
	/**
205
	 * Returns the directory for storing imported files
206
	 *
207
	 * @return string Directory for storing imported files
208
	 */
209
	protected function backup() : string
210
	{
211
		/** controller/jobs/catalog/import/csv/backup
212
		 * Name of the backup for sucessfully imported files
213
		 *
214
		 * After a CSV file was imported successfully, you can move it to another
215
		 * location, so it won't be imported again and isn't overwritten by the
216
		 * next file that is stored at the same location in the file system.
217
		 *
218
		 * You should use an absolute path to be sure but can be relative path
219
		 * if you absolutely know from where the job will be executed from. The
220
		 * name of the new backup location can contain placeholders understood
221
		 * by the PHP DateTime::format() method (with percent signs prefix) to
222
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
223
		 * "backup/2000-01-01". For more information about the date() placeholders,
224
		 * please have a look  into the PHP documentation of the
225
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
226
		 *
227
		 * **Note:** If no backup name is configured, the file will be removed!
228
		 *
229
		 * @param integer Name of the backup file, optionally with date/time placeholders
230
		 * @since 2018.04
231
		 * @see controller/jobs/catalog/import/csv/converter
232
		 * @see controller/jobs/catalog/import/csv/domains
233
		 * @see controller/jobs/catalog/import/csv/location
234
		 * @see controller/jobs/catalog/import/csv/mapping
235
		 * @see controller/jobs/catalog/import/csv/max-size
236
		 * @see controller/jobs/catalog/import/csv/skip-lines
237
		 */
238
		$backup = $this->context()->config()->get( 'controller/jobs/catalog/import/csv/backup' );
239
		return \Aimeos\Base\Str::strtime( (string) $backup );
240
	}
241
242
243
	/**
244
	 * Returns the list of domain names that should be retrieved along with the attribute items
245
	 *
246
	 * @return array List of domain names
247
	 */
248
	protected function domains() : array
249
	{
250
		/** controller/jobs/catalog/import/csv/domains
251
		 * List of item domain names that should be retrieved along with the catalog items
252
		 *
253
		 * For efficient processing, the items associated to the catalogs can be
254
		 * fetched to, minimizing the number of database queries required. To be
255
		 * most effective, the list of item domain names should be used in the
256
		 * mapping configuration too, so the retrieved items will be used during
257
		 * the import.
258
		 *
259
		 * @param array Associative list of MShop item domain names
260
		 * @since 2018.04
261
		 * @see controller/jobs/catalog/import/csv/backup
262
		 * @see controller/jobs/catalog/import/csv/converter
263
		 * @see controller/jobs/catalog/import/csv/location
264
		 * @see controller/jobs/catalog/import/csv/mapping
265
		 * @see controller/jobs/catalog/import/csv/max-size
266
		 * @see controller/jobs/catalog/import/csv/skip-lines
267
		 */
268
		return $this->context()->config()->get( 'controller/jobs/catalog/import/csv/domains', ['media', 'text'] );
269
	}
270
271
272
	/**
273
	 * Returns the position of the "catalog.code" column from the catalog item mapping
274
	 *
275
	 * @param array $mapping Mapping of the "item" columns with position as key and code as value
276
	 * @return int Position of the "catalog.code" column
277
	 * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "catalog.code" is found
278
	 */
279
	protected function getCodePosition( array $mapping ) : int
280
	{
281
		foreach( $mapping as $pos => $key )
282
		{
283
			if( $key === 'catalog.code' ) {
284
				return $pos;
285
			}
286
		}
287
288
		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "catalog.code" column in CSV mapping found' ) );
289
	}
290
291
292
	/**
293
	 * Returns the catalog items building the tree as list
294
	 *
295
	 * @param array $codes List of catalog item codes
296
	 * @param array $domains List of domain names whose items should be fetched too
297
	 * @return array Associative list of catalog codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
298
	 */
299
	protected function getCategories( array $codes, array $domains ) : array
300
	{
301
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
302
		$search = $manager->filter()->add( ['catalog.code' => $codes] )->slice( 0, count( $codes ) );
303
304
		$map = [];
305
		foreach( $manager->search( $search, $domains ) as $item ) {
306
			$map[$item->getCode()] = $item;
307
		}
308
309
		return $map;
310
	}
311
312
313
	/**
314
	 * Returns the parent ID of the catalog node for the given code
315
	 *
316
	 * @param array $catalogItems Associative list of catalog items with codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
317
	 * @param array $map Associative list of catalog item key/value pairs
318
	 * @param string $code Catalog item code of the parent category
319
	 * @return string|null ID of the parent category or null for top level nodes
320
	 */
321
	protected function getParentId( array $catalogItems, array $map, string $code ) : ?string
322
	{
323
		if( !isset( $map['catalog.parent'] ) )
324
		{
325
			$msg = sprintf( 'Required column "%1$s" not found for code "%2$s"', 'catalog.parent', $code );
326
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
327
		}
328
329
		$parent = trim( $map['catalog.parent'] );
330
331
		if( $parent != '' && !isset( $catalogItems[$parent] ) )
332
		{
333
			$msg = sprintf( 'Parent node for code "%1$s" not found', $parent );
334
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
335
		}
336
337
		return ( $parent != '' ? $catalogItems[$parent]->getId() : null );
338
	}
339
340
341
	/**
342
	 * Imports the CSV file from the given path
343
	 *
344
	 * @param string $path Relative path to the CSV file
345
	 * @return int Number of lines which couldn't be imported
346
	 */
347
	protected function import( string $path ) : int
348
	{
349
		$context = $this->context();
350
		$logger = $context->logger();
351
352
		$logger->info( sprintf( 'Started catalog import from "%1$s"', $path ), 'import/csv/catalog' );
353
354
		$maxcnt = $this->max();
355
		$skiplines = $this->skip();
356
		$domains = $this->domains();
357
358
		$mappings = $this->mapping();
359
		$processor = $this->getProcessors( $mappings );
360
		$codePos = $this->getCodePosition( $mappings['item'] );
361
362
		$fs = $context->fs( 'fs-import' );
363
		$fh = $fs->reads( $path );
364
		$total = $errors = 0;
365
366
		for( $i = 0; $i < $skiplines; $i++ ) {
367
			fgetcsv( $fh, null, ',', '"', '' );
368
		}
369
370
		while( ( $data = $this->getData( $fh, $maxcnt, $codePos ) ) !== [] )
371
		{
372
			$catalogItems = $this->getCategories( array_keys( $data ), $domains );
373
			$errors += $this->importCategories( $catalogItems, $data, $mappings['item'], $processor );
374
375
			$total += count( $data );
376
			unset( $catalogItems, $data );
377
		}
378
379
		$processor->finish();
380
		fclose( $fh );
381
382
		if( !empty( $backup = $this->backup() ) ) {
383
			$fs->move( $path, $backup );
384
		} else {
385
			$fs->rm( $path );
386
		}
387
388
		$str = sprintf( 'Finished catalog import from "%1$s" (%2$d/%3$d)', $path, $errors, $total );
389
		$logger->info( $str, 'import/csv/catalog' );
390
391
		return $errors;
392
	}
393
394
395
	/**
396
	 * Imports the CSV data and creates new categories or updates existing ones
397
	 *
398
	 * @param array $catalogItems Associative list of catalog items with codes as keys and items implementing \Aimeos\MShop\Catalog\Item\Iface as values
399
	 * @param array $data Associative list of import data as index/value pairs
400
	 * @param array $mapping Associative list of positions and domain item keys
401
	 * @param \Aimeos\Controller\Jobs\Common\Catalog\Import\Csv\Processor\Iface $processor Processor object
402
	 * @return int Number of catalogs that couldn't be imported
403
	 * @throws \Aimeos\Controller\Jobs\Exception
404
	 */
405
	protected function importCategories( array $catalogItems, array $data, array $mapping,
406
		\Aimeos\Controller\Jobs\Common\Catalog\Import\Csv\Processor\Iface $processor ) : int
407
	{
408
		$errors = 0;
409
		$context = $this->context();
410
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
411
412
		foreach( $data as $code => $list )
413
		{
414
			$manager->begin();
415
416
			try
417
			{
418
				$code = trim( $code );
419
				$item = $catalogItems[$code] ?? $manager->create();
420
				$map = current( $this->getMappedChunk( $list, $mapping ) ); // there can only be one chunk for the base catalog data
421
422
				if( $map )
423
				{
424
					$map['catalog.config'] = json_decode( $map['catalog.config'] ?? '[]', true ) ?: [];
425
					$parentid = $this->getParentId( $catalogItems, $map, $code );
426
					$item->fromArray( $map, true );
427
428
					if( isset( $catalogItems[$code] ) )
429
					{
430
						$manager->move( $item->getId(), $item->getParentId(), $parentid );
431
						$item = $manager->save( $item );
432
					}
433
					else
434
					{
435
						$item = $manager->insert( $item, $parentid );
436
					}
437
438
					$processor->process( $item, $list );
439
					$catalogItems[$code] = $item;
440
441
					$manager->save( $item );
442
				}
443
444
				$manager->commit();
445
			}
446
			catch( \Exception $e )
447
			{
448
				$manager->rollback();
449
450
				$msg = sprintf( 'Unable to import catalog with code "%1$s": %2$s', $code, $e->getMessage() );
451
				$context->logger()->error( $msg, 'import/csv/catalog' );
452
453
				$errors++;
454
			}
455
		}
456
457
		return $errors;
458
	}
459
460
461
	/**
462
	 * Returns the path to the directory with the CSV file
463
	 *
464
	 * @return string Path to the directory with the CSV file
465
	 */
466
	protected function location() : string
467
	{
468
		/** controller/jobs/catalog/import/csv/location
469
		 * Directory where the CSV files are stored which should be imported
470
		 *
471
		 * It's the relative path inside the "fs-import" virtual file system
472
		 * configuration. The default location of the "fs-import" file system is:
473
		 *
474
		 * * Laravel: ./storage/import/
475
		 * * TYPO3: /uploads/tx_aimeos/.secure/import/
476
		 *
477
		 * @param string Relative path to the CSV files
478
		 * @since 2015.08
479
		 * @see controller/jobs/catalog/import/csv/backup
480
		 * @see controller/jobs/catalog/import/csv/converter
481
		 * @see controller/jobs/catalog/import/csv/domains
482
		 * @see controller/jobs/catalog/import/csv/location
483
		 * @see controller/jobs/catalog/import/csv/mapping
484
		 * @see controller/jobs/catalog/import/csv/max-size
485
		 * @see controller/jobs/catalog/import/csv/skip-lines
486
		 */
487
		return (string) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/location', 'catalog' );
488
	}
489
490
491
	/**
492
	 * Returns the CSV column mapping
493
	 *
494
	 * @return array CSV column mapping
495
	 */
496
	protected function mapping() : array
497
	{
498
		/** controller/jobs/catalog/import/csv/mapping
499
		 * List of mappings between the position in the CSV file and item keys
500
		 *
501
		 * The importer have to know which data is at which position in the CSV
502
		 * file. Therefore, you need to specify a mapping between each position
503
		 * and the MShop domain item key (e.g. "catalog.code") it represents.
504
		 *
505
		 * You can use all domain item keys which are used in the fromArray()
506
		 * methods of the item classes.
507
		 *
508
		 * These mappings are grouped together by their processor names, which
509
		 * are responsible for importing the data, e.g. all mappings in "item"
510
		 * will be processed by the base catalog importer while the mappings in
511
		 * "text" will be imported by the text processor.
512
		 *
513
		 * @param array Associative list of processor names and lists of key/position pairs
514
		 * @since 2018.04
515
		 * @see controller/jobs/catalog/import/csv/backup
516
		 * @see controller/jobs/catalog/import/csv/converter
517
		 * @see controller/jobs/catalog/import/csv/domains
518
		 * @see controller/jobs/catalog/import/csv/location
519
		 * @see controller/jobs/catalog/import/csv/max-size
520
		 * @see controller/jobs/catalog/import/csv/skip-lines
521
		 */
522
		$map = (array) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/mapping', $this->getDefaultMapping() );
523
524
		if( !isset( $map['item'] ) || !is_array( $map['item'] ) )
525
		{
526
			$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' );
527
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
528
		}
529
530
		return $map;
531
	}
532
533
534
	/**
535
	 * Returns the maximum number of CSV rows to import at once
536
	 *
537
	 * @return int Maximum number of CSV rows to import at once
538
	 */
539
	protected function max() : int
540
	{
541
		/** controller/jobs/catalog/import/csv/max-size
542
		 * Maximum number of CSV rows to import at once
543
		 *
544
		 * It's more efficient to read and import more than one row at a time
545
		 * to speed up the import. Usually, the bigger the chunk that is imported
546
		 * at once, the less time the importer will need. The downside is that
547
		 * the amount of memory required by the import process will increase as
548
		 * well. Therefore, it's a trade-off between memory consumption and
549
		 * import speed.
550
		 *
551
		 * @param integer Number of rows
552
		 * @since 2018.04
553
		 * @see controller/jobs/catalog/import/csv/backup
554
		 * @see controller/jobs/catalog/import/csv/converter
555
		 * @see controller/jobs/catalog/import/csv/domains
556
		 * @see controller/jobs/catalog/import/csv/location
557
		 * @see controller/jobs/catalog/import/csv/mapping
558
		 * @see controller/jobs/catalog/import/csv/skip-lines
559
		 */
560
		return (int) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/max-size', 1000 );
561
	}
562
563
564
	/**
565
	 * Returns the number of rows skipped in front of each CSV files
566
	 *
567
	 * @return int Number of rows skipped in front of each CSV files
568
	 */
569
	protected function skip() : int
570
	{
571
		/** controller/jobs/catalog/import/csv/skip-lines
572
		 * Number of rows skipped in front of each CSV files
573
		 *
574
		 * Some CSV files contain header information describing the content of
575
		 * the column values. These data is for informational purpose only and
576
		 * can't be imported into the database. Using this option, you can
577
		 * define the number of lines that should be left out before the import
578
		 * begins.
579
		 *
580
		 * @param integer Number of rows
581
		 * @since 2015.08
582
		 * @see controller/jobs/catalog/import/csv/backup
583
		 * @see controller/jobs/catalog/import/csv/converter
584
		 * @see controller/jobs/catalog/import/csv/domains
585
		 * @see controller/jobs/catalog/import/csv/location
586
		 * @see controller/jobs/catalog/import/csv/mapping
587
		 * @see controller/jobs/catalog/import/csv/max-size
588
		 */
589
		return (int) $this->context()->config()->get( 'controller/jobs/catalog/import/csv/skip-lines', 0 );
590
	}
591
}
592