Passed
Push — master ( e68c42...a842b1 )
by Aimeos
01:54
created

Standard::process()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 11
nop 2
dl 0
loc 43
rs 8.4444
c 0
b 0
f 0
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\Product;
12
13
14
/**
15
 * Product 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\Product\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 = array_reverse( $item->getListItems( 'product', null, null, false ), true );
53
54
		$listManager = \Aimeos\MShop::create( $context, $resource . '/lists' );
55
		$manager = \Aimeos\MShop::create( $context, 'product' );
0 ignored issues
show
Unused Code introduced by
The assignment to $manager is dead and can be removed.
Loading history...
56
		$map = $this->getItems( $node->childNodes );
57
58
		foreach( $node->childNodes as $node )
59
		{
60
			$attributes = $node->attributes;
61
62
			if( $node->nodeName !== 'productitem' ) {
63
				continue;
64
			}
65
66
			if( ( $attr = $attributes->getNamedItem( 'ref' ) ) === null || !isset( $map[$attr->nodeValue] ) ) {
67
				continue;
68
			}
69
70
			$list = [];
71
			$refId = $map[$attr->nodeValue]->getId();
72
			$type = ( $attr = $attributes->getNamedItem( 'lists.type' ) ) !== null ? $attr->nodeValue : 'default';
73
74
			if( ( $listItem = $item->getListItem( 'product', $type, $refId ) ) === null ) {
75
				$listItem = $listManager->createItem();
76
			} else {
77
				unset( $listItems[$listItem->getId()] );
78
			}
79
80
			foreach( $attributes as $attrName => $attrNode ) {
81
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
82
			}
83
84
			$listItem = $listItem->fromArray( $list )->setRefId( $refId );
85
			$item = $item->addListItem( 'product', $listItem );
86
		}
87
88
		return $item->deleteListItems( $listItems );
89
	}
90
91
92
	/**
93
	 * Returns the product items for the given nodes
94
	 *
95
	 * @param \DomNodeList $nodes List of XML product item nodes
96
	 * @return \Aimeos\MShop\Product\Item\Iface[] Associative list of product items with codes as keys
97
	 */
98
	protected function getItems( \DomNodeList $nodes )
99
	{
100
		$codes = $map = [];
101
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
102
103
		foreach( $nodes as $node )
104
		{
105
			if( $node->nodeName === 'productitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
106
				$codes[$attr->nodeValue] = null;
107
			}
108
		}
109
110
		$search = $manager->createSearch()->setSlice( 0, count( $codes ) );
111
		$search->setConditions( $search->compare( '==', 'product.code', array_keys( $codes ) ) );
112
113
		foreach( $manager->searchItems( $search, [] ) as $item ) {
114
			$map[$item->getCode()] = $item;
115
		}
116
117
		return $map;
118
	}
119
}
120