Passed
Push — master ( 547952...cdc5f0 )
by Aimeos
04:14
created

Standard::process()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 37
rs 8.9777
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Common\Import\Xml\Processor\Property;
12
13
14
/**
15
 * Property processor for XML imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Common\Import\Xml\Processor\Base
22
	implements \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface
23
{
24
	/** controller/common/common/import/xml/processor/property/name
25
	 * Name of the property processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\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 2019.04
32
	 * @category Developer
33
	 */
34
35
36
	/**
37
	 * Updates the given item using the data from the DOM node
38
	 *
39
	 * @param \Aimeos\MShop\Common\Item\Iface $item Item which should be updated
40
	 * @param \DOMNode $node XML document node containing a list of nodes to process
41
	 * @return \Aimeos\MShop\Common\Item\Iface Updated item
42
	 */
43
	public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node )
44
	{
45
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\PropertyRef\Iface::class, $item );
46
47
		$resource = $item->getResourceType();
48
		$manager = \Aimeos\MShop::create( $this->getContext(), $resource . '/property' );
49
		$propItems = $item->getPropertyItems( null, false );
50
		$map = [];
51
52
		foreach( $propItems as $propItem ) {
53
			$map[$propItem->getType()][$propItem->getLanguageId()][$propItem->getValue()] = $propItem->getId();
54
		}
55
56
		foreach( $node->childNodes as $propNode )
57
		{
58
			if( $propNode->nodeName !== 'propertyitem' ) {
59
				continue;
60
			}
61
62
			$list = [];
63
64
			foreach( $propNode->childNodes as $tagNode ) {
65
				$list[$tagNode->nodeName] = $tagNode->nodeValue;
66
			}
67
68
			$propItem = $manager->createItem()->fromArray( $list );
69
70
			if( isset( $map[$propItem->getType()][$propItem->getLanguageId()][$propItem->getValue()] ) ) {
71
				unset( $propItems[$map[$propItem->getType()][$propItem->getLanguageId()][$propItem->getValue()]] );
72
			} else {
73
				$item->addPropertyItem( $propItem );
74
			}
75
76
			$this->addType( $resource . '/property/type', 'product', $propItem->getType() );
77
		}
78
79
		return $item->deletePropertyItems( $propItems );
80
	}
81
}
82