Passed
Push — master ( aa616b...308afb )
by Aimeos
03:25
created

Standard::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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