Passed
Push — master ( dda91a...58f742 )
by Aimeos
03:00
created

Standard::process()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 2
dl 0
loc 22
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Aimeos GmbH (aimeos.com), 2019
5
 * @package Controller
6
 * @subpackage Common
7
 */
8
9
10
namespace Aimeos\Controller\Common\Common\Import\Xml\Processor\Group;
11
12
13
/**
14
 * Customer group processor for XML imports
15
 *
16
 * @package Controller
17
 * @subpackage Common
18
 */
19
class Standard
20
	extends \Aimeos\Controller\Common\Common\Import\Xml\Processor\Base
21
	implements \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface
22
{
23
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
24
25
26
	/** controller/common/common/import/xml/processor/group/name
27
	 * Name of the group processor implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\Processor\Group\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the processor class name
33
	 * @since 2019.04
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Updates the given item using the data from the DOM node
40
	 *
41
	 * @param \Aimeos\MShop\Common\Item\Iface $item Item which should be updated
42
	 * @param \DOMNode $node XML document node containing a list of nodes to process
43
	 * @return \Aimeos\MShop\Common\Item\Iface Updated item
44
	 */
45
	public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node )
46
	{
47
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Customer\Item\Iface::class, $item );
48
49
		$map = $this->getItems( $node->childNodes );
50
		$context = $this->getContext();
0 ignored issues
show
Unused Code introduced by
The assignment to $context is dead and can be removed.
Loading history...
51
		$list = [];
52
53
		foreach( $node->childNodes as $node )
54
		{
55
			if( $node->nodeName !== 'groupitem' ) {
56
				continue;
57
			}
58
59
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) === null || !isset( $map[$attr->nodeValue] ) ) {
60
				continue;
61
			}
62
63
			$list[] = $map[$attr->nodeValue]->getId();
64
		}
65
66
		return $item->setGroups( $list );
67
	}
68
69
70
	/**
71
	 * Returns the attribute items for the given nodes
72
	 *
73
	 * @param \DomNodeList $nodes List of XML attribute item nodes
74
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of customer group items with codes as keys
75
	 */
76
	protected function getItems( \DomNodeList $nodes )
77
	{
78
		$keys = $map = [];
79
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer/group' );
80
81
		foreach( $nodes as $node )
82
		{
83
			if( $node->nodeName === 'groupitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
84
				$keys[$attr->nodeValue] = null;
85
			}
86
		}
87
88
		$search = $manager->createSearch()->setSlice( 0, count( $keys ) );
89
		$search->setConditions( $search->compare( '==', 'customer.group.code', array_keys( $keys ) ) );
90
91
		foreach( $manager->searchItems( $search, [] ) as $item ) {
92
			$map[$item->getCode()] = $item;
93
		}
94
95
		return $map;
96
	}
97
}
98