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

Standard::process()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 31
nc 36
nop 2
dl 0
loc 52
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\Common\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\Common\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
	private $listTypes = [];
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
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
51
		$listItems = $this->getListItems( $item->getResourceType(), $item->getId() );
52
		$catItems = $this->getCatalogItems( $node );
53
		$map = [];
54
55
		foreach( $listItems as $listItem ) {
56
			$map[$listItem->getParentId()][$listItem->getType()] = $listItem;
57
		}
58
59
60
		foreach( $node->childNodes as $node )
61
		{
62
			if( $node->nodeName !== 'catalogitem'
63
				|| ( $refattr = $node->attributes->getNamedItem( 'ref' ) ) === null
64
				|| !isset( $catItems[$refattr->nodeValue] )
65
			) {
66
				continue;
67
			}
68
69
			$list = [];
70
			$catcode = $refattr->nodeValue;
0 ignored issues
show
Unused Code introduced by
The assignment to $catcode is dead and can be removed.
Loading history...
71
			$parentid = $catItems[$refattr->nodeValue]->getId();
72
			$typeattr = $node->attributes->getNamedItem( 'lists.type' );
73
			$type = ( $typeattr !== null ? $typeattr->nodeValue : 'default' );
74
75
			foreach( $node->attributes as $attrName => $attrNode ) {
76
				$list['catalog.' . $attrName] = $attrNode->nodeValue;
77
			}
78
79
			$name = 'catalog.lists.config';
80
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
81
			$list['catalog.lists.type'] = $type;
82
83
84
			if( isset( $map[$parentid][$type] ) ) {
85
				$listItem = $map[$parentid][$type]; unset( $map[$parentid][$type] );
86
			} else {
87
				$listItem = $listManager->createItem();
88
			}
89
90
			$listItem = $listItem->fromArray( $list )->setDomain( $item->getResourceType() )
91
				->setRefId( $item->getId() )->setParentId( $parentid );
92
			$listManager->saveItem( $listItem, false );
93
		}
94
95
		$listManager->deleteItems( array_keys( $listItems ) );
96
97
		return $item;
98
	}
99
100
101
	/**
102
	 * Returns the catalog items referenced in the DOM node
103
	 *
104
	 * @param \DOMNode $node XML document node containing a list of nodes to process
105
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] List of referenced catalog items
106
	 */
107
	protected function getCatalogItems( \DOMNode $node )
108
	{
109
		foreach( $node->childNodes as $node )
110
		{
111
			if( $node->nodeName === 'catalogitem'
112
				&& ( $refAttr = $node->attributes->getNamedItem( 'ref' ) ) !== null
113
			) {
114
				$codes[] = $refAttr->nodeValue;
115
			}
116
		}
117
118
		$items = [];
119
120
		if( !empty( $codes ) )
121
		{
122
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' );
123
124
			$search = $manager->createSearch()->setSlice( 0, count( $codes ) );
125
			$search->setConditions( $search->compare( '==', 'catalog.code', $codes ) );
126
127
			foreach( $manager->searchItems( $search ) as $item ) {
128
				$items[$item->getCode()] = $item;
129
			}
130
		}
131
132
		return $items;
133
	}
134
135
136
	/**
137
	 * Returns the catalog list items for the given referenced ID
138
	 *
139
	 * @param string $domain Domain name the referenced ID belongs to
140
	 * @param string $id ID of the referenced domain item
141
	 * @return array List of catalog list items
142
	 */
143
	protected function getListItems( $domain, $id )
144
	{
145
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
146
		$search = $manager->createSearch()->setSlice( 0, 10000 );
147
		$expr = [];
148
149
		foreach( $this->getListTypes( $domain ) as $type ) {
150
			$expr[] = $search->compare( '==', 'catalog.lists.key', $domain . '|' . $type . '|' . $id );
151
		}
152
153
		return $manager->searchItems( $search->setConditions( $search->combine( '||', $expr ) ) );
154
	}
155
156
157
	/**
158
	 * Returns the available catalog list types for the given domain
159
	 *
160
	 * @param $domain Domain name the list types belong to
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Common...rocessor\Catalog\Domain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
	 * @return string[] List of list type codes
162
	 */
163
	protected function getListTypes( $domain )
164
	{
165
		if( !isset( $this->getListTypes[$domain] ) )
166
		{
167
			$this->getListTypes[$domain] = [];
0 ignored issues
show
Bug Best Practice introduced by
The property getListTypes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
168
169
			$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists/type' );
170
171
			$search = $manager->createSearch()->setSlice( 0, 10000 );
172
			$search->setConditions( $search->compare( '==', 'catalog.lists.type.domain', $domain ) );
173
174
			foreach( $manager->searchItems( $search ) as $item ) {
175
				$this->listTypes[$domain][] = $item->getCode();
176
			}
177
		}
178
179
		return $this->listTypes[$domain];
180
	}
181
}
182