Passed
Push — master ( 417598...532336 )
by Aimeos
03:26
created

Standard::process()   B

Complexity

Conditions 9
Paths 19

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 28
c 1
b 0
f 0
nc 19
nop 2
dl 0
loc 47
rs 8.0555
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2021
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Common\Import\Xml\Processor\Lists\Catalog;
12
13
14
/**
15
 * Catalog 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/catalog/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\Catalog\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 ) : \Aimeos\MShop\Common\Item\Iface
47
	{
48
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListsRef\Iface::class, $item );
49
50
		$context = $this->context();
51
		$resource = $item->getResourceType();
52
		$listItems = $item->getListItems( 'catalog', 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 !== 'catalogitem' ) {
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( 'catalog', $type, $refId ) ) === null ) {
73
				$listItem = $listManager->create();
74
			} else {
75
				unset( $listItems[$listItem->getId()] );
76
			}
77
78
			foreach( $attributes as $attrName => $attrNode ) {
79
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
80
			}
81
82
			$name = $resource . '.lists.config';
83
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
84
			$list[$resource . '.lists.type'] = $type;
85
86
			$this->addType( $resource . '/lists/type', 'catalog', $type );
87
88
			$listItem = $listItem->fromArray( $list )->setRefId( $refId );
89
			$item = $item->addListItem( 'catalog', $listItem );
90
		}
91
92
		return $item->deleteListItems( $listItems->toArray() );
93
	}
94
95
96
	/**
97
	 * Returns the catalog items for the given nodes
98
	 *
99
	 * @param \DomNodeList $nodes List of XML catalog item nodes
100
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of catalog items with codes as keys
101
	 */
102
	protected function getItems( \DomNodeList $nodes ) : array
103
	{
104
		$codes = $map = [];
105
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
106
107
		foreach( $nodes as $node )
108
		{
109
			if( $node->nodeName === 'catalogitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
110
				$codes[$attr->nodeValue] = null;
111
			}
112
		}
113
114
		$search = $manager->filter()->slice( 0, count( $codes ) );
115
		$search->setConditions( $search->compare( '==', 'catalog.code', array_keys( $codes ) ) );
116
117
		foreach( $manager->search( $search, [] ) as $item ) {
118
			$map[$item->getCode()] = $item;
119
		}
120
121
		return $map;
122
	}
123
}
124