Passed
Push — master ( f40c64...9a1840 )
by Aimeos
01:59
created

Standard::process()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 35
nc 36
nop 2
dl 0
loc 59
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Product\Import\Xml\Processor\Catalog;
12
13
14
/**
15
 * Catalog processor for product 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/catalog/name
25
	 * Name of the catalog processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Xml\Processor\Catalog\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\ListRef\Iface::class, $item );
46
47
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
48
49
		$search = $listManager->createSearch()->setSlice( 0, 10000 );
50
		$search->setConditions( $search->combine( '&&', [
51
			$search->compare( '==', 'catalog.lists.domain', 'product' ),
52
			$search->compare( '==', 'catalog.lists.refid', $item->getId() ),
53
		] ) );
54
55
		$listItems = $listManager->searchItems( $search );
56
		$catItems = $this->getCatalogItems( $node );
57
		$map = [];
58
59
		foreach( $listItems as $listItem ) {
60
			$map[$listItem->getParentId()][$listItem->getType()] = $listItem;
61
		}
62
63
64
		foreach( $node->childNodes as $node )
65
		{
66
			if( $node->nodeName !== 'catalogitem'
67
				|| ( $refattr = $node->attributes->getNamedItem( 'ref' ) ) === null
68
				|| !isset( $catItems[$refattr->nodeValue] )
69
			) {
70
				continue;
71
			}
72
73
			$list = [];
74
			$catcode = $refattr->nodeValue;
75
			$parentid = $catItems[$refattr->nodeValue]->getId();
76
			$typeattr = $node->attributes->getNamedItem( 'lists.type' );
77
			$type = ( $typeattr !== null ? $typeattr->nodeValue : 'default' );
78
79
			foreach( $node->attributes as $attrName => $attrNode ) {
80
				$list['catalog.' . $attrName] = $attrNode->nodeValue;
81
			}
82
83
			$name = 'catalog.lists.config';
84
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
85
			$list['catalog.lists.type'] = $type;
86
87
88
			if( isset( $map[$catcode][$type] ) ) {
89
				$listItem = $map[$catcode][$type]; unset( $map[$catcode][$type] );
90
			} else {
91
				$listItem = $listManager->createItem();
92
			}
93
94
			$listItem = $listItem->fromArray( $list )->setDomain( 'product' )
95
				->setRefId( $item->getId() )->setParentId( $parentid );
96
			$listManager->saveItem( $listItem, false );
97
		}
98
99
		$listManager->deleteItems( array_keys( $listItems ) );
100
101
		return $item;
102
	}
103
104
105
	/**
106
	 * Returns the catalog items referenced in the DOM node
107
	 *
108
	 * @param \DOMNode $node XML document node containing a list of nodes to process
109
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] List of referenced catalog items
110
	 */
111
	protected function getCatalogItems( \DOMNode $node )
112
	{
113
		foreach( $node->childNodes as $node )
114
		{
115
			if( $node->nodeName === 'catalogitem'
116
				&& ( $refAttr = $node->attributes->getNamedItem( 'ref' ) ) !== null
117
			) {
118
				$codes[] = $refAttr->nodeValue;
119
			}
120
		}
121
122
		$items = [];
123
124
		if( !empty( $codes ) )
125
		{
126
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
127
128
			$search = $manager->createSearch()->setSlice( 0, count( $codes ) );
129
			$search->setConditions( $search->compare( '==', 'catalog.code', $codes ) );
130
131
			foreach( $manager->searchItems( $search ) as $item ) {
132
				$items[$item->getCode()] = $item;
133
			}
134
		}
135
136
		return $items;
137
	}
138
}
139