Passed
Push — master ( c40152...87de23 )
by Aimeos
06:15
created

Standard   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 41 8
A getItems() 0 20 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\Lists\Attribute;
12
13
14
/**
15
 * Attribute list 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
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
25
26
27
	/** controller/common/common/import/xml/processor/lists/text/name
28
	 * Name of the lists processor implementation
29
	 *
30
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\Processor\Lists\Attribute\Myname".
31
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
32
	 *
33
	 * @param string Last part of the processor class name
34
	 * @since 2019.04
35
	 * @category Developer
36
	 */
37
38
39
	/**
40
	 * Updates the given item using the data from the DOM node
41
	 *
42
	 * @param \Aimeos\MShop\Common\Item\Iface $item Item which should be updated
43
	 * @param \DOMNode $node XML document node containing a list of nodes to process
44
	 * @return \Aimeos\MShop\Common\Item\Iface Updated item
45
	 */
46
	public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node )
47
	{
48
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListRef\Iface::class, $item );
49
50
		$context = $this->getContext();
51
		$resource = $item->getResourceType();
52
		$listItems = $item->getListItems( 'attribute', null, null, false );
53
		$listManager = \Aimeos\MShop::create( $context, $resource . '/lists' );
54
		$map = $this->getItems( $node->childNodes );
55
56
		foreach( $node->childNodes as $node )
57
		{
58
			$attributes = $node->attributes;
59
60
			if( $node->nodeName !== 'attributeitem' ) {
61
				continue;
62
			}
63
64
			if( ( $attr = $attributes->getNamedItem( 'ref' ) ) === null || !isset( $map[$attr->nodeValue] ) ) {
65
				continue;
66
			}
67
68
			$list = [];
69
			$refId = $map[$attr->nodeValue]->getId();
70
			$type = ( $attr = $attributes->getNamedItem( 'lists.type' ) ) !== null ? $attr->nodeValue : 'default';
71
72
			if( ( $listItem = $item->getListItem( 'attribute', $type, $refId ) ) === null ) {
73
				$listItem = $listManager->createItem();
74
			} else {
75
				unset( $listItems[$listItem->getId()] );
76
			}
77
78
			foreach( $attributes as $attrName => $attrNode ) {
79
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
80
			}
81
82
			$listItem = $listItem->fromArray( $list )->setRefId( $refId );
83
			$item = $item->addListItem( 'attribute', $listItem );
84
		}
85
86
		return $item->deleteListItems( $listItems );
87
	}
88
89
90
	/**
91
	 * Returns the attribute items for the given nodes
92
	 *
93
	 * @param \DomNodeList $nodes List of XML attribute item nodes
94
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute items with codes as keys
95
	 */
96
	protected function getItems( \DomNodeList $nodes )
97
	{
98
		$keys = $map = [];
99
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
100
101
		foreach( $nodes as $node )
102
		{
103
			if( $node->nodeName === 'attributeitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
104
				$keys[$attr->nodeValue] = null;
105
			}
106
		}
107
108
		$search = $manager->createSearch()->setSlice( 0, count( $keys ) );
109
		$search->setConditions( $search->compare( '==', 'attribute.key', array_keys( $keys ) ) );
110
111
		foreach( $manager->searchItems( $search, [] ) as $item ) {
112
			$map[$item->getKey()] = $item;
113
		}
114
115
		return $map;
116
	}
117
}
118