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

Standard::import()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 28
nc 8
nop 1
dl 0
loc 45
rs 9.472
c 1
b 0
f 0
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
			$errors = 0;
169
			$location = $this->location();
170
			$fs = $context->fs( 'fs-import' );
171
172
			if( $fs->isDir( $location ) === false ) {
173
				return;
174
			}
175
176
			foreach( map( $fs->scan( $location ) )->sort() as $filename )
177
			{
178
				$path = $location . '/' . $filename;
179
180
				if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) {
181
					continue;
182
				}
183
184
				$errors = $this->import( $path );
185
			}
186
187
			if( $errors > 0 ) {
188
				$this->mail( 'Product CSV import', sprintf( 'Invalid product lines during import: %1$d', $errors ) );
189
			}
190
		}
191
		catch( \Exception $e )
192
		{
193
			$logger->error( 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/product' );
194
			$this->mail( 'Product CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
195
			throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() );
196
		}
197
	}
198
199
200
	/**
201
	 * Returns the directory for storing imported files
202
	 *
203
	 * @return string Directory for storing imported files
204
	 */
205
	protected function backup() : string
206
	{
207
		/** controller/jobs/product/import/csv/backup
208
		 * Name of the backup for sucessfully imported files
209
		 *
210
		 * After a CSV file was imported successfully, you can move it to another
211
		 * location, so it won't be imported again and isn't overwritten by the
212
		 * next file that is stored at the same location in the file system.
213
		 *
214
		 * You should use an absolute path to be sure but can be relative path
215
		 * if you absolutely know from where the job will be executed from. The
216
		 * name of the new backup location can contain placeholders understood
217
		 * by the PHP DateTime::format() method (with percent signs prefix) to
218
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
219
		 * "backup/2000-01-01". For more information about the date() placeholders,
220
		 * please have a look  into the PHP documentation of the
221
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
222
		 *
223
		 * **Note:** If no backup name is configured, the file will be removed!
224
		 *
225
		 * @param integer Name of the backup file, optionally with date/time placeholders
226
		 * @since 2018.04
227
		 * @see controller/jobs/product/import/csv/domains
228
		 * @see controller/jobs/product/import/csv/location
229
		 * @see controller/jobs/product/import/csv/mapping
230
		 * @see controller/jobs/product/import/csv/max-size
231
		 * @see controller/jobs/product/import/csv/skip-lines
232
		 */
233
		$backup = $this->context()->config()->get( 'controller/jobs/product/import/csv/backup' );
234
		return \Aimeos\Base\Str::strtime( (string) $backup );
235
	}
236
237
238
	/**
239
	 * Checks the given product type for validity
240
	 *
241
	 * @param string|null $type Product type or null for no type
242
	 * @return string New product type
243
	 */
244
	protected function checkType( string $type = null ) : string
245
	{
246
		if( !isset( $this->types ) )
247
		{
248
			$this->types = [];
249
250
			$manager = \Aimeos\MShop::create( $this->context(), 'product/type' );
251
			$search = $manager->filter()->slice( 0, 10000 );
252
253
			foreach( $manager->search( $search ) as $item ) {
254
				$this->types[$item->getCode()] = $item->getCode();
255
			}
256
		}
257
258
		return ( isset( $this->types[$type] ) ? $this->types[$type] : 'default' );
259
	}
260
261
262
	/**
263
	 * Returns the list of domain names that should be retrieved along with the attribute items
264
	 *
265
	 * @return array List of domain names
266
	 */
267
	protected function domains() : array
268
	{
269
		/** controller/jobs/product/import/csv/domains
270
		 * List of item domain names that should be retrieved along with the product items
271
		 *
272
		 * For efficient processing, the items associated to the products can be
273
		 * fetched to, minimizing the number of database queries required. To be
274
		 * most effective, the list of item domain names should be used in the
275
		 * mapping configuration too, so the retrieved items will be used during
276
		 * the import.
277
		 *
278
		 * @param array Associative list of MShop item domain names
279
		 * @since 2018.04
280
		 * @see controller/jobs/product/import/csv/backup
281
		 * @see controller/jobs/product/import/csv/location
282
		 * @see controller/jobs/product/import/csv/mapping
283
		 * @see controller/jobs/product/import/csv/max-size
284
		 * @see controller/jobs/product/import/csv/skip-lines
285
		 */
286
		return $this->context()->config()->get( 'controller/jobs/product/import/csv/domains', ['media', 'text'] );
287
	}
288
289
290
	/**
291
	 * Returns the position of the "product.code" column from the product item mapping
292
	 *
293
	 * @param array $mapping Mapping of the "item" columns with position as key and code as value
294
	 * @return int Position of the "product.code" column
295
	 * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "product.code" is found
296
	 */
297
	protected function getCodePosition( array $mapping ) : int
298
	{
299
		foreach( $mapping as $pos => $key )
300
		{
301
			if( $key === 'product.code' ) {
302
				return $pos;
303
			}
304
		}
305
306
		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "product.code" column in CSV mapping found' ) );
307
	}
308
309
310
	/**
311
	 * Returns the product items for the given codes
312
	 *
313
	 * @param array $codes List of product codes
314
	 * @param array $domains List of domains whose items should be fetched too
315
	 * @return \Aimeos\Map Associative list of product codes as key and product items as value
316
	 */
317
	protected function getProducts( array $codes, array $domains ) : \Aimeos\Map
318
	{
319
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
320
		$search = $manager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) );
321
322
		return $manager->search( $search, $domains )->col( null, 'product.code' );
323
	}
324
325
326
	/**
327
	 * Imports the CSV file from the given path
328
	 *
329
	 * @param string $path Relative path to the CSV file
330
	 * @return int Number of lines which couldn't be imported
331
	 */
332
	protected function import( string $path ) : int
333
	{
334
		$context = $this->context();
335
		$logger = $context->logger();
336
337
		$logger->info( sprintf( 'Started product import from "%1$s"', $path ), 'import/csv/product' );
338
339
		$maxcnt = $this->max();
340
		$skiplines = $this->skip();
341
		$domains = $this->domains();
342
343
		$mappings = $this->mapping();
344
		$processor = $this->getProcessors( $mappings );
345
		$codePos = $this->getCodePosition( $mappings['item'] );
346
347
		$fs = $context->fs( 'fs-import' );
348
		$fh = $fs->reads( $path );
349
		$total = $errors = 0;
350
351
		for( $i = 0; $i < $skiplines; $i++ ) {
352
			fgetcsv( $fh );
353
		}
354
355
		while( ( $data = $this->getData( $fh, $maxcnt, $codePos ) ) !== [] )
356
		{
357
			$products = $this->getProducts( array_keys( $data ), $domains );
358
			$errors += $this->importProducts( $products, $data, $mappings['item'], [], $processor );
359
360
			$total += count( $data );
361
			unset( $products, $data );
362
		}
363
364
		$processor->finish();
365
		fclose( $fh );
366
367
		if( !empty( $backup = $this->backup() ) ) {
368
			$fs->move( $path, $backup );
369
		} else {
370
			$fs->rm( $path );
371
		}
372
373
		$str = sprintf( 'Finished product import from "%1$s" (%2$d/%3$d)', $path, $errors, $total );
374
		$logger->info( $str, 'import/csv/product' );
375
376
		return $errors;
377
	}
378
379
380
	/**
381
	 * Imports the CSV data and creates new products or updates existing ones
382
	 *
383
	 * @param \Aimeos\Map $products List of products items implementing \Aimeos\MShop\Product\Item\Iface
384
	 * @param array $data Associative list of import data as index/value pairs
385
	 * @param array $mapping Associative list of positions and domain item keys
386
	 * @param array $types List of allowed product type codes
387
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor Processor object
388
	 * @return int Number of products that couldn't be imported
389
	 * @throws \Aimeos\Controller\Jobs\Exception
390
	 */
391
	protected function importProducts( \Aimeos\Map $products, array $data, array $mapping, array $types,
392
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor ) : int
393
	{
394
		$items = [];
395
		$errors = 0;
396
		$context = $this->context();
397
		$manager = \Aimeos\MShop::create( $context, 'index' );
398
399
		foreach( $data as $code => $list )
400
		{
401
			$manager->begin();
402
403
			try
404
			{
405
				$code = trim( $code );
406
				$product = $products[$code] ?? $manager->create();
407
				$map = current( $this->getMappedChunk( $list, $mapping ) ); // there can only be one chunk for the base product data
408
409
				if( $map )
410
				{
411
					$type = $this->checkType( $this->val( $map, 'product.type', $product->getType() ) );
412
413
					if( $config = $this->val( $map, 'product.config' ) ) {
414
						$map['product.config'] = json_decode( $config ) ?: [];
415
					}
416
417
					$product = $product->fromArray( $map, true );
418
					$product = $manager->save( $product->setType( $type ) );
419
420
					$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

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