Completed
Push — master ( 984043...a9b00f )
by Aimeos
02:00
created

Standard::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Property;
12
13
14
/**
15
 * Product property processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/product/import/csv/processor/property/name
25
	 * Name of the property processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Property\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2015.10
32
	 * @category Developer
33
	 */
34
35
	private $types = [];
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
43
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'product/property/type' );
51
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
52
53
		foreach( $manager->searchItems( $search ) as $item ) {
54
			$this->types[$item->getCode()] = $item->getCode();
55
		}
56
	}
57
58
59
	/**
60
	 * Saves the product property related data to the storage
61
	 *
62
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
63
	 * @param array $data List of CSV fields with position as key and data as value
64
	 * @return array List of data which hasn't been imported
65
	 */
66
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
67
	{
68
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/property' );
69
70
		$propMap = [];
71
		$items = $product->getPropertyItems( null, false );
72
		$map = $this->getMappedChunk( $data, $this->getMapping() );
73
74
		foreach( $items as $item ) {
75
			$propMap[ $item->getValue() ][ $item->getType() ] = $item;
76
		}
77
78
		foreach( $map as $list )
79
		{
80
			$typecode = trim( $list['product.property.type'] );
81
82
			if( ( $value = trim( $list['product.property.value'] ) ) == '' ) {
83
				continue;
84
			}
85
86
			if( !in_array( $typecode, $this->types ) )
87
			{
88
				$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $typecode, 'product property' );
89
				throw new \Aimeos\Controller\Common\Exception( $msg );
90
			}
91
92
			if( isset( $propMap[$value][$typecode] ) )
93
			{
94
				$item = $propMap[$value][$typecode];
95
				unset( $items[ $item->getId() ] );
96
			}
97
			else
98
			{
99
				$item = $manager->createItem( $typecode, 'product' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $typecode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
			}
101
102
			$item->fromArray( $list );
103
104
			$product->addPropertyItem( $item );
105
		}
106
107
		$product->deletePropertyItems( $items );
108
109
		return $this->getObject()->process( $product, $data );
110
	}
111
}
112