Passed
Push — master ( e3c28a...29f10b )
by Aimeos
02:31
created

Standard   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 29 5
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\Address;
12
13
14
/**
15
 * Address 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/address/name
25
	 * Name of the address processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\Processor\Address\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\AddressRef\Iface::class, $item );
46
47
		$manager = \Aimeos\MShop::create( $this->getContext(), $item->getResourceType() . '/address' );
48
		$addrItems = array_reverse( $item->getAddressItems(), true );
49
50
		foreach( $node->childNodes as $addrNode )
51
		{
52
			if( $addrNode->nodeName !== 'addressitem' ) {
53
				continue;
54
			}
55
56
			$list = [];
57
58
			foreach( $addrNode->childNodes as $tagNode ) {
59
				$list[$tagNode->nodeName] = $tagNode->nodeValue;
60
			}
61
62
			if( ( $addrItem = array_pop( $addrItems ) ) === null ) {
63
				$addrItem = $manager->createItem();
64
			} else {
65
				unset( $addrItems[$addrItem->getId()] );
66
			}
67
68
			$item = $item->addAddressItem( $addrItem->fromArray( $list ), $addrItem->getId() );
69
		}
70
71
		return $item->deleteAddressItems( $addrItems );
72
	}
73
}
74