Standard::process()   B
last analyzed

Complexity

Conditions 9
Paths 20

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 31
nc 20
nop 2
dl 0
loc 53
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

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-2025
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Common\Import\Xml\Processor\Lists\Attribute;
12
13
14
/**
15
 * Attribute list processor for XML imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Common\Import\Xml\Processor\Base
22
	implements \Aimeos\Controller\Jobs\Common\Import\Xml\Processor\Iface
23
{
24
	use \Aimeos\Controller\Jobs\Common\Import\Xml\Traits;
25
26
27
	/** controller/jobs/common/import/xml/processor/lists/attribute/name
28
	 * Name of the lists processor implementation
29
	 *
30
	 * Use "Myname" if your class is named "\Aimeos\Controller\Jobs\Common\Import\Xml\Processor\Lists\Attribute\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
	 */
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 ) : \Aimeos\MShop\Common\Item\Iface
46
	{
47
		\Aimeos\Utils::implements( $item, \Aimeos\MShop\Common\Item\ListsRef\Iface::class );
48
49
		$context = $this->context();
50
		$resource = $item->getResourceType();
51
		$listItems = $item->getListItems( 'attribute', null, null, false );
52
		$manager = \Aimeos\MShop::create( $context, $resource );
53
		$map = $this->getItems( $node->childNodes );
54
55
		foreach( $node->childNodes as $node )
56
		{
57
			$attributes = $node->attributes;
58
59
			if( $node->nodeName !== 'attributeitem' ) {
60
				continue;
61
			}
62
63
			if( ( $attr = $attributes->getNamedItem( 'ref' ) ) === null ) {
64
				continue;
65
			}
66
67
			$attrValue = \Aimeos\Base\Str::decode( $attr->nodeValue );
68
69
			if( !isset( $map[$attrValue] ) ) {
70
				continue;
71
			}
72
73
			$list = [];
74
			$refItem = $map[$attrValue];
75
			$type = ( $attr = $attributes->getNamedItem( 'lists.type' ) ) !== null ? $attr->nodeValue : 'default';
76
77
			if( ( $listItem = $item->getListItem( 'attribute', $type, $refItem->getId() ) ) === null ) {
78
				$listItem = $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
				/** @scrutinizer ignore-call */ 
79
    $listItem = $manager->createListItem();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
			} else {
80
				unset( $listItems[$listItem->getId()] );
81
			}
82
83
			foreach( $attributes as $attrName => $attrNode ) {
84
				$list[$resource . '.' . $attrName] = \Aimeos\Base\Str::decode( $attrNode->nodeValue );
85
			}
86
87
			$name = $resource . '.lists.config';
88
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
89
			$list[$resource . '.lists.type'] = $type;
90
91
			$this->addType( $resource . '/lists/type', 'attribute', $type );
92
93
			$listItem = $listItem->fromArray( $list );
94
			$item->addListItem( 'attribute', $listItem, $refItem );
95
		}
96
97
		return $item->deleteListItems( $listItems->toArray() );
98
	}
99
100
101
	/**
102
	 * Returns the attribute items for the given nodes
103
	 *
104
	 * @param \DomNodeList $nodes List of XML attribute item nodes
105
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute items with codes as keys
106
	 */
107
	protected function getItems( \DomNodeList $nodes ) : array
108
	{
109
		$keys = $map = [];
110
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
111
112
		foreach( $nodes as $node )
113
		{
114
			if( $node->nodeName === 'attributeitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
115
				$keys[\Aimeos\Base\Str::decode( $attr->nodeValue )] = null;
116
			}
117
		}
118
119
		$search = $manager->filter()->slice( 0, count( $keys ) );
120
		$search->setConditions( $search->compare( '==', 'attribute.key', array_keys( $keys ) ) );
121
122
		foreach( $manager->search( $search, [] ) as $item ) {
123
			$map[$item->getKey()] = $item;
124
		}
125
126
		return $map;
127
	}
128
}
129