Passed
Push — master ( f0002b...c777f5 )
by Aimeos
08:04 queued 04:27
created

Standard   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getItems() 0 20 5
B process() 0 47 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2023
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\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\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/catalog/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\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
	 */
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( 'catalog', 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 !== 'catalogitem' ) {
60
				continue;
61
			}
62
63
			if( ( $attr = $attributes->getNamedItem( 'ref' ) ) === null || !isset( $map[$attr->nodeValue] ) ) {
64
				continue;
65
			}
66
67
			$list = [];
68
			$refId = $map[$attr->nodeValue]->getId();
69
			$type = ( $attr = $attributes->getNamedItem( 'lists.type' ) ) !== null ? $attr->nodeValue : 'default';
70
71
			if( ( $listItem = $item->getListItem( 'catalog', $type, $refId ) ) === null ) {
72
				$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

72
				/** @scrutinizer ignore-call */ 
73
    $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...
73
			} else {
74
				unset( $listItems[$listItem->getId()] );
75
			}
76
77
			foreach( $attributes as $attrName => $attrNode ) {
78
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
79
			}
80
81
			$name = $resource . '.lists.config';
82
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
83
			$list[$resource . '.lists.type'] = $type;
84
85
			$this->addType( $resource . '/lists/type', 'catalog', $type );
86
87
			$listItem = $listItem->fromArray( $list )->setRefId( $refId );
88
			$item = $item->addListItem( 'catalog', $listItem );
89
		}
90
91
		return $item->deleteListItems( $listItems->toArray() );
92
	}
93
94
95
	/**
96
	 * Returns the catalog items for the given nodes
97
	 *
98
	 * @param \DomNodeList $nodes List of XML catalog item nodes
99
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of catalog items with codes as keys
100
	 */
101
	protected function getItems( \DomNodeList $nodes ) : array
102
	{
103
		$codes = $map = [];
104
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
105
106
		foreach( $nodes as $node )
107
		{
108
			if( $node->nodeName === 'catalogitem' && ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
109
				$codes[$attr->nodeValue] = null;
110
			}
111
		}
112
113
		$search = $manager->filter()->slice( 0, count( $codes ) );
114
		$search->setConditions( $search->compare( '==', 'catalog.code', array_keys( $codes ) ) );
115
116
		foreach( $manager->search( $search, [] ) as $item ) {
117
			$map[$item->getCode()] = $item;
118
		}
119
120
		return $map;
121
	}
122
}
123