Standard::process()   B
last analyzed

Complexity

Conditions 9
Paths 50

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 50
nop 2
dl 0
loc 54
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\Media;
12
13
14
/**
15
 * Media 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/media/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\Media\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
		$listItems = $item->getListItems( 'media', null, null, false )->reverse();
50
		$resource = $item->getResourceType();
51
		$context = $this->context();
52
53
		$manager = \Aimeos\MShop::create( $context, $resource );
54
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
55
56
		foreach( $node->childNodes as $refNode )
57
		{
58
			if( $refNode->nodeName !== 'mediaitem' ) {
59
				continue;
60
			}
61
62
			if( ( $listItem = $listItems->pop() ) === null ) {
63
				$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

63
				/** @scrutinizer ignore-call */ 
64
    $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...
64
			}
65
66
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
67
				$refItem = $mediaManager->create();
68
			}
69
70
			$list = [];
71
72
			foreach( $refNode->childNodes as $tag )
73
			{
74
				if( in_array( $tag->nodeName, ['lists', 'property'] ) ) {
75
					$refItem = $this->getProcessor( $tag->nodeName )->process( $refItem, $tag );
76
				} else {
77
					$list[$tag->nodeName] = \Aimeos\Base\Str::decode( $tag->nodeValue );
78
				}
79
			}
80
81
			$refItem = $this->update( $refItem, $list );
82
83
			foreach( $refNode->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
			$name = $resource . '.lists.type';
90
			$list[$name] = $list[$name] ?? 'default';
91
92
			$this->addType( $resource . '/lists/type', 'media', $list[$resource . '.lists.type'] );
93
94
			$listItem = $listItem->fromArray( $list );
95
			$item->addListItem( 'media', $listItem, $refItem );
96
		}
97
98
		return $item->deleteListItems( $listItems->toArray() );
99
	}
100
101
102
	/**
103
	 * Updates the media item with the given key/value pairs
104
	 *
105
	 * @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update
106
	 * @param array &$list Associative list of key/value pairs, matching pairs are removed
107
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item
108
	 */
109
	protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list )
110
	{
111
		$url = $list['media.url'] ?? '';
112
113
		try
114
		{
115
			if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) {
116
				$refItem->setPreviews( $map )->setUrl( $url );
117
			} elseif( isset( $list['media.preview'] ) ) {
118
				$refItem->setPreview( $list['media.preview'] )->setUrl( $url );
119
			} elseif( $refItem->getUrl() !== $url ) {
120
				$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ), true );
121
			} else {
122
				$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ) );
123
			}
124
125
			unset( $list['media.previews'], $list['media.preview'] );
126
		}
127
		catch( \Aimeos\Controller\Jobs\Exception $e )
128
		{
129
			$msg = sprintf( 'Scaling image "%1$s" failed: %2$s', $url, $e->getMessage() );
130
			$this->context()->logger()->error( $msg, 'import/xml/product' );
131
		}
132
133
		return $refItem->fromArray( $list );
134
	}
135
}
136