Passed
Push — master ( ce780a...b27fbc )
by Aimeos
03:18
created

Standard   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 514
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 92
c 2
b 0
f 0
dl 0
loc 514
rs 9.76
wmc 33

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getDescription() 0 3 1
A getCodePosition() 0 10 3
A location() 0 20 1
A skip() 0 20 1
A mapping() 0 34 3
B run() 0 70 10
A domains() 0 20 1
A backup() 0 32 1
A max() 0 21 1
B import() 0 49 6
A checkType() 0 15 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Product\Import\Csv;
12
13
14
/**
15
 * Job controller for CSV product imports.
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/** controller/jobs/product/import/csv/name
25
	 * Class name of the used product suggestions scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Product\Import\Csv\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Product\Import\Csv\Mycsv
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/product/import/csv/name = Mycsv
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyCsv"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2015.01
55
	 */
56
57
	/** controller/jobs/product/import/csv/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the product import CSV job controller
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/product/import/csv/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to the job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2015.01
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/product/import/csv/decorators/global
79
	 * @see controller/jobs/product/import/csv/decorators/local
80
	 */
81
82
	/** controller/jobs/product/import/csv/decorators/global
83
	 * Adds a list of globally available decorators only to the product import CSV job controller
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/product/import/csv/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2015.01
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/product/import/csv/decorators/excludes
102
	 * @see controller/jobs/product/import/csv/decorators/local
103
	 */
104
105
	/** controller/jobs/product/import/csv/decorators/local
106
	 * Adds a list of local decorators only to the product import CSV job controller
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/product/import/csv/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2015.01
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/product/import/csv/decorators/excludes
127
	 * @see controller/jobs/product/import/csv/decorators/global
128
	 */
129
130
131
	private $types;
132
133
134
	/**
135
	 * Returns the localized name of the job.
136
	 *
137
	 * @return string Name of the job
138
	 */
139
	public function getName() : string
140
	{
141
		return $this->context()->translate( 'controller/jobs', 'Product import CSV' );
142
	}
143
144
145
	/**
146
	 * Returns the localized description of the job.
147
	 *
148
	 * @return string Description of the job
149
	 */
150
	public function getDescription() : string
151
	{
152
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing products from CSV files' );
153
	}
154
155
156
	/**
157
	 * Executes the job.
158
	 *
159
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
160
	 */
161
	public function run()
162
	{
163
		$context = $this->context();
164
		$logger = $context->logger();
165
166
		try
167
		{
168
			$location = $this->location();
169
			$fs = $context->fs( 'fs-import' );
170
171
			if( $fs->isDir( $location ) === false ) {
172
				return;
173
			}
174
175
			$maxcnt = $this->max();
176
			$skiplines = $this->skip();
177
			$domains = $this->domains();
178
179
			$mappings = $this->mapping();
180
			$processor = $this->getProcessors( $mappings );
181
			$codePos = $this->getCodePosition( $mappings['item'] );
182
183
			foreach( map( $fs->scan( $location ) )->sort() as $filename )
184
			{
185
				$path = $location . '/' . $filename;
186
187
				if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) {
188
					continue;
189
				}
190
191
				$logger->info( sprintf( 'Started product import from "%1$s"', $path ), 'import/csv/product' );
192
193
				$total = $errors = 0;
194
				$fh = $fs->reads( $path );
195
196
				for( $i = 0; $i < $skiplines; $i++ ) {
197
					fgetcsv( $fh );
198
				}
199
200
				while( ( $data = $this->getData( $fh, $maxcnt, $codePos ) ) !== [] )
201
				{
202
					$products = $this->getProducts( array_keys( $data ), $domains );
203
					$errors += $this->import( $products, $data, $mappings['item'], [], $processor );
204
205
					$total += count( $data );
206
					unset( $products, $data );
207
				}
208
209
				fclose( $fh );
210
211
				$str = sprintf( 'Finished product import from "%1$s" (%2$d/%3$d)', $path, $errors, $total );
212
				$logger->info( $str, 'import/csv/product' );
213
214
215
				if( !empty( $backup = \Aimeos\Base\Str::strtime( $this->backup() ) ) ) {
216
					$fs->move( $path, $backup );
217
				}
218
219
				if( $errors > 0 ) {
220
					$this->mail( 'Product CSV import', sprintf( 'Invalid product lines in "%1$s": %2$d/%3$d', $path, $errors, $total ) );
221
				}
222
			}
223
224
			$processor->finish();
225
		}
226
		catch( \Exception $e )
227
		{
228
			$logger->error( 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/product' );
229
			$this->mail( 'Product CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
230
			throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() );
231
		}
232
	}
233
234
235
	/**
236
	 * Returns the directory for storing imported files
237
	 *
238
	 * @return string Directory for storing imported files
239
	 */
240
	protected function backup() : string
241
	{
242
		/** controller/jobs/product/import/csv/backup
243
		 * Name of the backup for sucessfully imported files
244
		 *
245
		 * After a CSV file was imported successfully, you can move it to another
246
		 * location, so it won't be imported again and isn't overwritten by the
247
		 * next file that is stored at the same location in the file system.
248
		 *
249
		 * You should use an absolute path to be sure but can be relative path
250
		 * if you absolutely know from where the job will be executed from. The
251
		 * name of the new backup location can contain placeholders understood
252
		 * by the PHP DateTime::format() method (with percent signs prefix) to
253
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
254
		 * "backup/2000-01-01". For more information about the date() placeholders,
255
		 * please have a look  into the PHP documentation of the
256
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
257
		 *
258
		 * **Note:** If no backup name is configured, the file or directory
259
		 * won't be moved away. Please make also sure that the parent directory
260
		 * and the new directory are writable so the file or directory could be
261
		 * moved.
262
		 *
263
		 * @param integer Name of the backup file, optionally with date/time placeholders
264
		 * @since 2018.04
265
		 * @see controller/jobs/product/import/csv/domains
266
		 * @see controller/jobs/product/import/csv/location
267
		 * @see controller/jobs/product/import/csv/mapping
268
		 * @see controller/jobs/product/import/csv/max-size
269
		 * @see controller/jobs/product/import/csv/skip-lines
270
		 */
271
		return (string) $this->context()->config()->get( 'controller/jobs/product/import/csv/backup' );
272
	}
273
274
275
	/**
276
	 * Checks the given product type for validity
277
	 *
278
	 * @param string|null $type Product type or null for no type
279
	 * @return string New product type
280
	 */
281
	protected function checkType( string $type = null ) : string
282
	{
283
		if( !isset( $this->types ) )
284
		{
285
			$this->types = [];
286
287
			$manager = \Aimeos\MShop::create( $this->context(), 'product/type' );
288
			$search = $manager->filter()->slice( 0, 10000 );
289
290
			foreach( $manager->search( $search ) as $item ) {
291
				$this->types[$item->getCode()] = $item->getCode();
292
			}
293
		}
294
295
		return ( isset( $this->types[$type] ) ? $this->types[$type] : 'default' );
296
	}
297
298
299
	/**
300
	 * Returns the list of domain names that should be retrieved along with the attribute items
301
	 *
302
	 * @return array List of domain names
303
	 */
304
	protected function domains() : array
305
	{
306
		/** controller/jobs/product/import/csv/domains
307
		 * List of item domain names that should be retrieved along with the product items
308
		 *
309
		 * For efficient processing, the items associated to the products can be
310
		 * fetched to, minimizing the number of database queries required. To be
311
		 * most effective, the list of item domain names should be used in the
312
		 * mapping configuration too, so the retrieved items will be used during
313
		 * the import.
314
		 *
315
		 * @param array Associative list of MShop item domain names
316
		 * @since 2018.04
317
		 * @see controller/jobs/product/import/csv/backup
318
		 * @see controller/jobs/product/import/csv/location
319
		 * @see controller/jobs/product/import/csv/mapping
320
		 * @see controller/jobs/product/import/csv/max-size
321
		 * @see controller/jobs/product/import/csv/skip-lines
322
		 */
323
		return $this->context()->config()->get( 'controller/jobs/product/import/csv/domains', ['media', 'text'] );
324
	}
325
326
327
	/**
328
	 * Returns the position of the "product.code" column from the product item mapping
329
	 *
330
	 * @param array $mapping Mapping of the "item" columns with position as key and code as value
331
	 * @return int Position of the "product.code" column
332
	 * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "product.code" is found
333
	 */
334
	protected function getCodePosition( array $mapping ) : int
335
	{
336
		foreach( $mapping as $pos => $key )
337
		{
338
			if( $key === 'product.code' ) {
339
				return $pos;
340
			}
341
		}
342
343
		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "product.code" column in CSV mapping found' ) );
344
	}
345
346
347
	/**
348
	 * Imports the CSV data and creates new products or updates existing ones
349
	 *
350
	 * @param \Aimeos\Map $products List of products items implementing \Aimeos\MShop\Product\Item\Iface
351
	 * @param array $data Associative list of import data as index/value pairs
352
	 * @param array $mapping Associative list of positions and domain item keys
353
	 * @param array $types List of allowed product type codes
354
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor Processor object
355
	 * @return int Number of products that couldn't be imported
356
	 * @throws \Aimeos\Controller\Jobs\Exception
357
	 */
358
	protected function import( \Aimeos\Map $products, array $data, array $mapping, array $types,
359
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor ) : int
360
	{
361
		$items = [];
362
		$errors = 0;
363
		$context = $this->context();
364
		$manager = \Aimeos\MShop::create( $context, 'index' );
365
366
		foreach( $data as $code => $list )
367
		{
368
			$manager->begin();
369
370
			try
371
			{
372
				$code = trim( $code );
373
				$product = $products[$code] ?? $manager->create();
374
				$map = current( $this->getMappedChunk( $list, $mapping ) ); // there can only be one chunk for the base product data
375
376
				if( $map )
377
				{
378
					$type = $this->checkType( $this->val( $map, 'product.type', $product->getType() ) );
379
380
					if( $config = $this->val( $map, 'product.config' ) ) {
381
						$map['product.config'] = json_decode( $config ) ?: [];
382
					}
383
384
					$product = $product->fromArray( $map, true );
385
					$product = $manager->save( $product->setType( $type ) );
386
387
					$processor->process( $product, $list );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Common...cessor\Iface::process() does only seem to accept Aimeos\MShop\Product\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

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

387
					$processor->process( /** @scrutinizer ignore-type */ $product, $list );
Loading history...
388
389
					$product = $manager->save( $product );
390
					$items[$product->getId()] = $product;
391
				}
392
393
				$manager->commit();
394
			}
395
			catch( \Exception $e )
396
			{
397
				$manager->rollback();
398
399
				$msg = sprintf( 'Unable to import product with code "%1$s": %2$s', $code, $e->getMessage() );
400
				$context->logger()->error( $msg, 'import/csv/product' );
401
402
				$errors++;
403
			}
404
		}
405
406
		return $errors;
407
	}
408
409
410
	/**
411
	 * Returns the path to the directory with the CSV file
412
	 *
413
	 * @return string Path to the directory with the CSV file
414
	 */
415
	protected function location() : string
416
	{
417
		/** controller/jobs/product/import/csv/location
418
		 * File or directory where the content is stored which should be imported
419
		 *
420
		 * You need to configure the CSV file or directory with the CSV files that
421
		 * should be imported. It should be an absolute path to be sure but can be
422
		 * relative path if you absolutely know from where the job will be executed
423
		 * from.
424
		 *
425
		 * @param string Relative path to the CSV files
426
		 * @since 2015.08
427
		 * @see controller/jobs/product/import/csv/backup
428
		 * @see controller/jobs/product/import/csv/domains
429
		 * @see controller/jobs/product/import/csv/location
430
		 * @see controller/jobs/product/import/csv/mapping
431
		 * @see controller/jobs/product/import/csv/max-size
432
		 * @see controller/jobs/product/import/csv/skip-lines
433
		 */
434
		return (string) $this->context()->config()->get( 'controller/jobs/product/import/csv/location', 'product' );
435
	}
436
437
438
	/**
439
	 * Returns the CSV column mapping
440
	 *
441
	 * @return array CSV column mapping
442
	 */
443
	protected function mapping() : array
444
	{
445
		/** controller/jobs/product/import/csv/mapping
446
		 * List of mappings between the position in the CSV file and item keys
447
		 *
448
		 * The importer have to know which data is at which position in the CSV
449
		 * file. Therefore, you need to specify a mapping between each position
450
		 * and the MShop domain item key (e.g. "product.code") it represents.
451
		 *
452
		 * You can use all domain item keys which are used in the fromArray()
453
		 * methods of the item classes.
454
		 *
455
		 * These mappings are grouped together by their processor names, which
456
		 * are responsible for importing the data, e.g. all mappings in "item"
457
		 * will be processed by the base product importer while the mappings in
458
		 * "text" will be imported by the text processor.
459
		 *
460
		 * @param array Associative list of processor names and lists of key/position pairs
461
		 * @since 2018.04
462
		 * @see controller/jobs/product/import/csv/backup
463
		 * @see controller/jobs/product/import/csv/domains
464
		 * @see controller/jobs/product/import/csv/location
465
		 * @see controller/jobs/product/import/csv/max-size
466
		 * @see controller/jobs/product/import/csv/skip-lines
467
		 */
468
		$map = (array) $this->context()->config()->get( 'controller/jobs/product/import/csv/mapping', $this->getDefaultMapping() );
469
470
		if( !isset( $map['item'] ) || !is_array( $map['item'] ) )
471
		{
472
			$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' );
473
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
474
		}
475
476
		return $map;
477
	}
478
479
480
	/**
481
	 * Returns the maximum number of CSV rows to import at once
482
	 *
483
	 * @return int Maximum number of CSV rows to import at once
484
	 */
485
	protected function max() : int
486
	{
487
		/** controller/jobs/product/import/csv/max-size
488
		 * Maximum number of CSV rows to import at once
489
		 *
490
		 * It's more efficient to read and import more than one row at a time
491
		 * to speed up the import. Usually, the bigger the chunk that is imported
492
		 * at once, the less time the importer will need. The downside is that
493
		 * the amount of memory required by the import process will increase as
494
		 * well. Therefore, it's a trade-off between memory consumption and
495
		 * import speed.
496
		 *
497
		 * @param integer Number of rows
498
		 * @since 2018.04
499
		 * @see controller/jobs/product/import/csv/backup
500
		 * @see controller/jobs/product/import/csv/domains
501
		 * @see controller/jobs/product/import/csv/location
502
		 * @see controller/jobs/product/import/csv/mapping
503
		 * @see controller/jobs/product/import/csv/skip-lines
504
		 */
505
		return (int) $this->context()->config()->get( 'controller/jobs/product/import/csv/max-size', 1000 );
506
	}
507
508
509
	/**
510
	 * Returns the number of rows skipped in front of each CSV files
511
	 *
512
	 * @return int Number of rows skipped in front of each CSV files
513
	 */
514
	protected function skip() : int
515
	{
516
		/** controller/jobs/product/import/csv/skip-lines
517
		 * Number of rows skipped in front of each CSV files
518
		 *
519
		 * Some CSV files contain header information describing the content of
520
		 * the column values. These data is for informational purpose only and
521
		 * can't be imported into the database. Using this option, you can
522
		 * define the number of lines that should be left out before the import
523
		 * begins.
524
		 *
525
		 * @param integer Number of rows
526
		 * @since 2015.08
527
		 * @see controller/jobs/product/import/csv/backup
528
		 * @see controller/jobs/product/import/csv/domains
529
		 * @see controller/jobs/product/import/csv/location
530
		 * @see controller/jobs/product/import/csv/mapping
531
		 * @see controller/jobs/product/import/csv/max-size
532
		 */
533
		return (int) $this->context()->config()->get( 'controller/jobs/product/import/csv/skip-lines', 0 );
534
	}
535
}
536