Completed
Push — master ( a9b00f...475749 )
by Aimeos
02:16
created
controller/jobs/src/Controller/Jobs/Product/Import/Csv/Standard.php 1 patch
Spacing   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -30,14 +30,14 @@  discard block
 block discarded – undo
30 30
 	 * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object
31 31
 	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap main object
32 32
 	 */
33
-	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Bootstrap $aimeos )
33
+	public function __construct(\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Bootstrap $aimeos)
34 34
 	{
35
-		parent::__construct( $context, $aimeos );
35
+		parent::__construct($context, $aimeos);
36 36
 
37
-		$manager = \Aimeos\MShop\Factory::createManager( $context, 'product/type' );
38
-		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
37
+		$manager = \Aimeos\MShop\Factory::createManager($context, 'product/type');
38
+		$search = $manager->createSearch()->setSlice(0, 0x7fffffff);
39 39
 
40
-		foreach( $manager->searchItems( $search ) as $item ) {
40
+		foreach ($manager->searchItems($search) as $item) {
41 41
 			$this->types[$item->getCode()] = $item->getCode();
42 42
 		}
43 43
 	}
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
50 50
 	 */
51 51
 	public function getName()
52 52
 	{
53
-		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Product import CSV' );
53
+		return $this->getContext()->getI18n()->dt('controller/jobs', 'Product import CSV');
54 54
 	}
55 55
 
56 56
 
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 	 */
62 62
 	public function getDescription()
63 63
 	{
64
-		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Imports new and updates existing products from CSV files' );
64
+		return $this->getContext()->getI18n()->dt('controller/jobs', 'Imports new and updates existing products from CSV files');
65 65
 	}
66 66
 
67 67
 
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 		$context = $this->getContext();
77 77
 		$config = $context->getConfig();
78 78
 		$logger = $context->getLogger();
79
-		$domains = array( 'attribute', 'media', 'price', 'product', 'product/property', 'text' );
79
+		$domains = array('attribute', 'media', 'price', 'product', 'product/property', 'text');
80 80
 		$mappings = $this->getDefaultMapping();
81 81
 
82 82
 
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 		 * @see controller/common/product/import/csv/converter
97 97
 		 * @see controller/common/product/import/csv/max-size
98 98
 		 */
99
-		$domains = $config->get( 'controller/common/product/import/csv/domains', $domains );
99
+		$domains = $config->get('controller/common/product/import/csv/domains', $domains);
100 100
 
101 101
 		/** controller/jobs/product/import/csv/domains
102 102
 		 * List of item domain names that should be retrieved along with the product items
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 		 * @see controller/jobs/product/import/csv/backup
117 117
 		 * @see controller/common/product/import/csv/max-size
118 118
 		 */
119
-		$domains = $config->get( 'controller/jobs/product/import/csv/domains', $domains );
119
+		$domains = $config->get('controller/jobs/product/import/csv/domains', $domains);
120 120
 
121 121
 
122 122
 		/** controller/common/product/import/csv/mapping
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
 		 * @see controller/common/product/import/csv/converter
142 142
 		 * @see controller/common/product/import/csv/max-size
143 143
 		 */
144
-		$mappings = $config->get( 'controller/common/product/import/csv/mapping', $mappings );
144
+		$mappings = $config->get('controller/common/product/import/csv/mapping', $mappings);
145 145
 
146 146
 		/** controller/jobs/product/import/csv/mapping
147 147
 		 * List of mappings between the position in the CSV file and item keys
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 		 * @see controller/jobs/product/import/csv/backup
162 162
 		 * @see controller/common/product/import/csv/max-size
163 163
 		 */
164
-		$mappings = $config->get( 'controller/jobs/product/import/csv/mapping', $mappings );
164
+		$mappings = $config->get('controller/jobs/product/import/csv/mapping', $mappings);
165 165
 
166 166
 
167 167
 		/** controller/common/product/import/csv/converter
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 		 * @see controller/common/product/import/csv/mapping
204 204
 		 * @see controller/common/product/import/csv/max-size
205 205
 		 */
206
-		$converters = $config->get( 'controller/common/product/import/csv/converter', [] );
206
+		$converters = $config->get('controller/common/product/import/csv/converter', []);
207 207
 
208 208
 		/** controller/jobs/product/import/csv/converter
209 209
 		 * List of converter names for the values at the position in the CSV file
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
 		 * @see controller/jobs/product/import/csv/backup
224 224
 		 * @see controller/common/product/import/csv/max-size
225 225
 		 */
226
-		$converters = $config->get( 'controller/jobs/product/import/csv/converter', $converters );
226
+		$converters = $config->get('controller/jobs/product/import/csv/converter', $converters);
227 227
 
228 228
 
229 229
 		/** controller/common/product/import/csv/max-size
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
 		 * @see controller/common/product/import/csv/mapping
244 244
 		 * @see controller/common/product/import/csv/converter
245 245
 		 */
246
-		$maxcnt = (int) $config->get( 'controller/common/product/import/csv/max-size', 1000 );
246
+		$maxcnt = (int) $config->get('controller/common/product/import/csv/max-size', 1000);
247 247
 
248 248
 
249 249
 		/** controller/jobs/product/import/csv/skip-lines
@@ -265,7 +265,7 @@  discard block
 block discarded – undo
265 265
 		 * @see controller/jobs/product/import/csv/backup
266 266
 		 * @see controller/common/product/import/csv/max-size
267 267
 		 */
268
-		$skiplines = (int) $config->get( 'controller/jobs/product/import/csv/skip-lines', 0 );
268
+		$skiplines = (int) $config->get('controller/jobs/product/import/csv/skip-lines', 0);
269 269
 
270 270
 
271 271
 		/** controller/jobs/product/import/csv/strict
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 		 * @see controller/jobs/product/import/csv/backup
290 290
 		 * @see controller/common/product/import/csv/max-size
291 291
 		 */
292
-		$strict = (bool) $config->get( 'controller/jobs/product/import/csv/strict', true );
292
+		$strict = (bool) $config->get('controller/jobs/product/import/csv/strict', true);
293 293
 
294 294
 
295 295
 		/** controller/jobs/product/import/csv/backup
@@ -322,74 +322,74 @@  discard block
 block discarded – undo
322 322
 		 * @see controller/jobs/product/import/csv/strict
323 323
 		 * @see controller/common/product/import/csv/max-size
324 324
 		 */
325
-		$backup = $config->get( 'controller/jobs/product/import/csv/backup' );
325
+		$backup = $config->get('controller/jobs/product/import/csv/backup');
326 326
 
327 327
 
328
-		if( !isset( $mappings['item'] ) || !is_array( $mappings['item'] ) )
328
+		if (!isset($mappings['item']) || !is_array($mappings['item']))
329 329
 		{
330
-			$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' );
331
-			throw new \Aimeos\Controller\Jobs\Exception( $msg );
330
+			$msg = sprintf('Required mapping key "%1$s" is missing or contains no array', 'item');
331
+			throw new \Aimeos\Controller\Jobs\Exception($msg);
332 332
 		}
333 333
 
334 334
 		try
335 335
 		{
336 336
 			$procMappings = $mappings;
337
-			unset( $procMappings['item'] );
337
+			unset($procMappings['item']);
338 338
 
339
-			$codePos = $this->getCodePosition( $mappings['item'] );
340
-			$convlist = $this->getConverterList( $converters );
341
-			$processor = $this->getProcessors( $procMappings );
339
+			$codePos = $this->getCodePosition($mappings['item']);
340
+			$convlist = $this->getConverterList($converters);
341
+			$processor = $this->getProcessors($procMappings);
342 342
 			$container = $this->getContainer();
343 343
 			$path = $container->getName();
344 344
 
345
-			$msg = sprintf( 'Started product import from "%1$s" (%2$s)', $path, __CLASS__ );
346
-			$logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE );
345
+			$msg = sprintf('Started product import from "%1$s" (%2$s)', $path, __CLASS__);
346
+			$logger->log($msg, \Aimeos\MW\Logger\Base::NOTICE);
347 347
 
348
-			foreach( $container as $content )
348
+			foreach ($container as $content)
349 349
 			{
350 350
 				$name = $content->getName();
351 351
 
352
-				for( $i = 0; $i < $skiplines; $i++ ) {
352
+				for ($i = 0; $i < $skiplines; $i++) {
353 353
 					$content->next();
354 354
 				}
355 355
 
356
-				while( ( $data = $this->getData( $content, $maxcnt, $codePos ) ) !== [] )
356
+				while (($data = $this->getData($content, $maxcnt, $codePos)) !== [])
357 357
 				{
358
-					$data = $this->convertData( $convlist, $data );
359
-					$products = $this->getProducts( array_keys( $data ), $domains );
360
-					$errcnt = $this->import( $products, $data, $mappings['item'], $processor, $strict );
361
-					$chunkcnt = count( $data );
358
+					$data = $this->convertData($convlist, $data);
359
+					$products = $this->getProducts(array_keys($data), $domains);
360
+					$errcnt = $this->import($products, $data, $mappings['item'], $processor, $strict);
361
+					$chunkcnt = count($data);
362 362
 
363 363
 					$msg = 'Imported product lines from "%1$s": %2$d/%3$d (%4$s)';
364
-					$logger->log( sprintf( $msg, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE );
364
+					$logger->log(sprintf($msg, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__), \Aimeos\MW\Logger\Base::NOTICE);
365 365
 
366 366
 					$errors += $errcnt;
367 367
 					$total += $chunkcnt;
368
-					unset( $products, $data );
368
+					unset($products, $data);
369 369
 				}
370 370
 			}
371 371
 
372 372
 			$container->close();
373 373
 		}
374
-		catch( \Exception $e )
374
+		catch (\Exception $e)
375 375
 		{
376
-			$logger->log( 'Product import error: ' . $e->getMessage() );
377
-			$logger->log( $e->getTraceAsString() );
376
+			$logger->log('Product import error: ' . $e->getMessage());
377
+			$logger->log($e->getTraceAsString());
378 378
 
379
-			throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() );
379
+			throw new \Aimeos\Controller\Jobs\Exception($e->getMessage());
380 380
 		}
381 381
 
382 382
 		$msg = 'Finished product import from "%1$s": %2$d successful, %3$s errors, %4$s total (%5$s)';
383
-		$logger->log( sprintf( $msg, $path, $total - $errors, $errors, $total, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE );
383
+		$logger->log(sprintf($msg, $path, $total - $errors, $errors, $total, __CLASS__), \Aimeos\MW\Logger\Base::NOTICE);
384 384
 
385
-		if( $errors > 0 )
385
+		if ($errors > 0)
386 386
 		{
387
-			$msg = sprintf( 'Invalid product lines in "%1$s": %2$d/%3$d', $path, $errors, $total );
388
-			throw new \Aimeos\Controller\Jobs\Exception( $msg );
387
+			$msg = sprintf('Invalid product lines in "%1$s": %2$d/%3$d', $path, $errors, $total);
388
+			throw new \Aimeos\Controller\Jobs\Exception($msg);
389 389
 		}
390 390
 
391
-		if( !empty( $backup ) && @rename( $path, strftime( $backup ) ) === false ) {
392
-			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Unable to move imported file' ) );
391
+		if (!empty($backup) && @rename($path, strftime($backup)) === false) {
392
+			throw new \Aimeos\Controller\Jobs\Exception(sprintf('Unable to move imported file'));
393 393
 		}
394 394
 	}
395 395
 
@@ -401,16 +401,16 @@  discard block
 block discarded – undo
401 401
 	 * @return integer Position of the "product.code" column
402 402
 	 * @throws \Aimeos\Controller\Jobs\Exception If no mapping for "product.code" is found
403 403
 	 */
404
-	protected function getCodePosition( array $mapping )
404
+	protected function getCodePosition(array $mapping)
405 405
 	{
406
-		foreach( $mapping as $pos => $key )
406
+		foreach ($mapping as $pos => $key)
407 407
 		{
408
-			if( $key === 'product.code' ) {
408
+			if ($key === 'product.code') {
409 409
 				return $pos;
410 410
 			}
411 411
 		}
412 412
 
413
-		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "product.code" column in CSV mapping found' ) );
413
+		throw new \Aimeos\Controller\Jobs\Exception(sprintf('No "product.code" column in CSV mapping found'));
414 414
 	}
415 415
 
416 416
 
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
 		 * @see controller/jobs/product/import/csv/container/content
446 446
 		 * @see controller/jobs/product/import/csv/container/options
447 447
 		 */
448
-		$location = $config->get( 'controller/jobs/product/import/csv/location' );
448
+		$location = $config->get('controller/jobs/product/import/csv/location');
449 449
 
450 450
 		/** controller/jobs/product/import/csv/container/type
451 451
 		 * Nave of the container type to read the data from
@@ -468,7 +468,7 @@  discard block
 block discarded – undo
468 468
 		 * @see controller/jobs/product/import/csv/container/content
469 469
 		 * @see controller/jobs/product/import/csv/container/options
470 470
 		 */
471
-		$container = $config->get( 'controller/jobs/product/import/csv/container/type', 'Directory' );
471
+		$container = $config->get('controller/jobs/product/import/csv/container/type', 'Directory');
472 472
 
473 473
 		/** controller/jobs/product/import/csv/container/content
474 474
 		 * Name of the content type inside the container to read the data from
@@ -489,7 +489,7 @@  discard block
 block discarded – undo
489 489
 		 * @see controller/jobs/product/import/csv/container/type
490 490
 		 * @see controller/jobs/product/import/csv/container/options
491 491
 		 */
492
-		$content = $config->get( 'controller/jobs/product/import/csv/container/content', 'CSV' );
492
+		$content = $config->get('controller/jobs/product/import/csv/container/content', 'CSV');
493 493
 
494 494
 		/** controller/jobs/product/import/csv/container/options
495 495
 		 * List of file container options for the product import files
@@ -507,15 +507,15 @@  discard block
 block discarded – undo
507 507
 		 * @see controller/jobs/product/import/csv/container/content
508 508
 		 * @see controller/jobs/product/import/csv/container/type
509 509
 		 */
510
-		$options = $config->get( 'controller/jobs/product/import/csv/container/options', [] );
510
+		$options = $config->get('controller/jobs/product/import/csv/container/options', []);
511 511
 
512
-		if( $location === null )
512
+		if ($location === null)
513 513
 		{
514
-			$msg = sprintf( 'Required configuration for "%1$s" is missing', 'controller/jobs/product/import/csv/location' );
515
-			throw new \Aimeos\Controller\Jobs\Exception( $msg );
514
+			$msg = sprintf('Required configuration for "%1$s" is missing', 'controller/jobs/product/import/csv/location');
515
+			throw new \Aimeos\Controller\Jobs\Exception($msg);
516 516
 		}
517 517
 
518
-		return \Aimeos\MW\Container\Factory::getContainer( $location, $container, $content, $options );
518
+		return \Aimeos\MW\Container\Factory::getContainer($location, $container, $content, $options);
519 519
 	}
520 520
 
521 521
 
@@ -530,69 +530,69 @@  discard block
 block discarded – undo
530 530
 	 * @return integer Number of products that couldn't be imported
531 531
 	 * @throws \Aimeos\Controller\Jobs\Exception
532 532
 	 */
533
-	protected function import( array $products, array $data, array $mapping,
534
-		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor, $strict )
533
+	protected function import(array $products, array $data, array $mapping,
534
+		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $processor, $strict)
535 535
 	{
536 536
 		$items = [];
537 537
 		$errors = 0;
538 538
 		$context = $this->getContext();
539
-		$manager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
540
-		$indexManager = \Aimeos\MShop\Factory::createManager( $context, 'index' );
539
+		$manager = \Aimeos\MShop\Factory::createManager($context, 'product');
540
+		$indexManager = \Aimeos\MShop\Factory::createManager($context, 'index');
541 541
 
542
-		foreach( $data as $code => $list )
542
+		foreach ($data as $code => $list)
543 543
 		{
544 544
 			$manager->begin();
545 545
 
546 546
 			try
547 547
 			{
548
-				$code = trim( $code );
548
+				$code = trim($code);
549 549
 
550
-				if( isset( $products[$code] ) ) {
550
+				if (isset($products[$code])) {
551 551
 					$product = $products[$code];
552 552
 				} else {
553 553
 					$product = $manager->createItem();
554 554
 				}
555 555
 
556
-				$map = $this->getMappedChunk( $list, $mapping );
556
+				$map = $this->getMappedChunk($list, $mapping);
557 557
 
558
-				if( isset( $map[0] ) )
558
+				if (isset($map[0]))
559 559
 				{
560 560
 					$map = $map[0]; // there can only be one chunk for the base product data
561
-					$map['product.type'] = $this->getValue( $map, 'product.type', 'default' );
561
+					$map['product.type'] = $this->getValue($map, 'product.type', 'default');
562 562
 
563
-					if( !in_array( $map['product.type'], $this->types ) )
563
+					if (!in_array($map['product.type'], $this->types))
564 564
 					{
565
-						$msg = sprintf( 'Invalid product type "%1$s"', $map['product.type'] );
566
-						throw new \Aimeos\Controller\Jobs\Exception( $msg );
565
+						$msg = sprintf('Invalid product type "%1$s"', $map['product.type']);
566
+						throw new \Aimeos\Controller\Jobs\Exception($msg);
567 567
 					}
568 568
 
569
-					$product->fromArray( $this->addItemDefaults( $map ) );
570
-					$product = $manager->saveItem( $product );
569
+					$product->fromArray($this->addItemDefaults($map));
570
+					$product = $manager->saveItem($product);
571 571
 
572
-					$list = $processor->process( $product, $list );
572
+					$list = $processor->process($product, $list);
573 573
 
574
-					$product = $manager->saveItem( $product );
574
+					$product = $manager->saveItem($product);
575 575
 					$items[$product->getId()] = $product;
576 576
 				}
577 577
 
578 578
 				$manager->commit();
579 579
 			}
580
-			catch( \Exception $e )
580
+			catch (\Exception $e)
581 581
 			{
582 582
 				$manager->rollback();
583 583
 
584
-				$msg = sprintf( 'Unable to import product with code "%1$s": %2$s', $code, $e->getMessage() );
585
-				$context->getLogger()->log( $msg );
584
+				$msg = sprintf('Unable to import product with code "%1$s": %2$s', $code, $e->getMessage());
585
+				$context->getLogger()->log($msg);
586 586
 
587 587
 				$errors++;
588 588
 			}
589 589
 
590
-			if( $strict && !empty( $list ) ) {
591
-				$context->getLogger()->log( 'Not imported: ' . print_r( $list, true ) );
590
+			if ($strict && !empty($list)) {
591
+				$context->getLogger()->log('Not imported: ' . print_r($list, true));
592 592
 			}
593 593
 		}
594 594
 
595
-		$indexManager->rebuildIndex( $items );
595
+		$indexManager->rebuildIndex($items);
596 596
 
597 597
 		return $errors;
598 598
 	}
@@ -604,9 +604,9 @@  discard block
 block discarded – undo
604 604
 	 * @param array $list Associative list of domain item keys and their values, e.g. "product.status" => 1
605 605
 	 * @return array Given associative list enriched by default values if they were not already set
606 606
 	 */
607
-	protected function addItemDefaults( array $list )
607
+	protected function addItemDefaults(array $list)
608 608
 	{
609
-		if( !isset( $list['product.status'] ) ) {
609
+		if (!isset($list['product.status'])) {
610 610
 			$list['product.status'] = 1;
611 611
 		}
612 612
 
Please login to merge, or discard this patch.