Passed
Push — master ( e9900e...f5d98d )
by Aimeos
02:27
created

Standard::process()   B

Complexity

Conditions 10
Paths 98

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 2
b 0
f 0
nc 98
nop 2
dl 0
loc 54
rs 7.6666

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\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\Common\Common\Import\Xml\Processor\Base
22
	implements \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface
23
{
24
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
25
26
27
	/** controller/common/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\Common\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
	 * @category Developer
36
	 */
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 ) : \Aimeos\MShop\Common\Item\Iface
47
	{
48
		\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListRef\Iface::class, $item );
49
50
		$listItems = array_reverse( $item->getListItems( 'media', null, null, false ), true );
51
		$resource = $item->getResourceType();
52
		$context = $this->getContext();
53
54
		$listManager = \Aimeos\MShop::create( $context, $resource . '/lists' );
55
		$manager = \Aimeos\MShop::create( $context, 'media' );
56
57
		foreach( $node->childNodes as $refNode )
58
		{
59
			if( $refNode->nodeName !== 'mediaitem' ) {
60
				continue;
61
			}
62
63
			if( ( $listItem = array_pop( $listItems ) ) === null ) {
64
				$listItem = $listManager->createItem();
65
			}
66
67
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
68
				$refItem = $manager->createItem();
69
			}
70
71
			$list = [];
72
73
			foreach( $refNode->childNodes as $tag )
74
			{
75
				if( in_array( $tag->nodeName, ['lists', 'property'] ) ) {
76
					$refItem = $this->getProcessor( $tag->nodeName )->process( $refItem, $tag );
77
				} else {
78
					$list[$tag->nodeName] = $tag->nodeValue;
79
				}
80
			}
81
82
			$refItem = $this->update( $refItem, $list );
83
84
			foreach( $refNode->attributes as $attrName => $attrNode ) {
85
				$list[$resource . '.' . $attrName] = $attrNode->nodeValue;
86
			}
87
88
			$name = $resource . '.lists.config';
89
			$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] );
90
			$name = $resource . '.lists.type';
91
			$list[$name] = ( isset( $list[$name] ) ? $list[$name] : 'default' );
92
93
			$this->addType( $resource . '/lists/type', 'media', $list[$resource . '.lists.type'] );
94
95
			$listItem = $listItem->fromArray( $list );
96
			$item->addListItem( 'media', $listItem, $refItem );
97
		}
98
99
		return $item->deleteListItems( $listItems );
100
	}
101
102
103
	/**
104
	 * Updates the media item with the given key/value pairs
105
	 *
106
	 * @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update
107
	 * @param array &$list Associative list of key/value pairs, matching pairs are removed
108
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item
109
	 */
110
	protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list )
111
	{
112
		try
113
		{
114
			$refItem = $refItem->setUrl( $list['media.url'] ?? '' );
115
116
			if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) {
117
				$refItem->setPreviews( $map );
118
			} elseif( isset( $list['media.preview'] ) ) {
119
				$refItem->setPreview( $list['media.preview'] );
120
			} elseif( $refItem->isModified() ) {
121
				$refItem = \Aimeos\Controller\Common\Media\Factory::create( $this->getContext() )->scale( $refItem );
122
			}
123
124
			unset( $list['media.previews'], $list['media.preview'] );
125
		}
126
		catch( \Aimeos\Controller\Common\Exception $e )
127
		{
128
			$msg = sprintf( 'Scaling image "%1$s" failed: %2$s', $refItem->getUrl(), $e->getMessage() );
129
			$this->getContext()->getLogger()->log( $msg );
130
		}
131
132
		return $refItem->fromArray( $list );
133
	}
134
}
135