Completed
Push — master ( 0ed5fa...41d654 )
by Aimeos
02:01
created

Standard::process()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 8.0484
c 0
b 0
f 0
cc 7
nc 15
nop 2

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), 2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media;
12
13
14
/**
15
 * Media processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/catalog/import/csv/processor/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\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 2018.04
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
43
	 * @param \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
			\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/common/catalog/import/csv/processor/media/listtypes
51
		 * Names of the catalog list types for media that are updated or removed
52
		 *
53
		 * If you want to associate media items manually via the administration
54
		 * interface to catalogs and don't want these to be touched during the
55
		 * import, you can specify the catalog list types for these media
56
		 * that shouldn't be updated or removed.
57
		 *
58
		 * @param array|null List of catalog list type names or null for all
59
		 * @since 2018.04
60
		 * @category Developer
61
		 * @category User
62
		 * @see controller/common/catalog/import/csv/domains
63
		 * @see controller/common/catalog/import/csv/processor/attribute/listtypes
64
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
65
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/catalog/import/csv/processor/price/listtypes
67
		 * @see controller/common/catalog/import/csv/processor/text/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/catalog/import/csv/processor/media/listtypes' );
70
	}
71
72
73
	/**
74
	 * Saves the catalog related data to the storage
75
	 *
76
	 * @param \Aimeos\MShop\Catalog\Item\Iface $catalog Catalog item with associated items
77
	 * @param array $data List of CSV fields with position as key and data as value
78
	 * @return array List of data which hasn't been imported
79
	 */
80
	public function process( \Aimeos\MShop\Catalog\Item\Iface $catalog, array $data )
81
	{
82
		$context = $this->getContext();
83
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
84
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' );
85
		$separator = $context->getConfig()->get( 'controller/common/catalog/import/csv/separator', "\n" );
86
87
		$delete = $listMap = [];
0 ignored issues
show
Unused Code introduced by
$delete is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
		$map = $this->getMappedChunk( $data, $this->getMapping() );
89
		$listItems = $catalog->getListItems( 'media', $this->listTypes );
90
91
		foreach( $listItems as $listItem )
92
		{
93
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
94
				$listMap[ $refItem->getUrl() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
95
			}
96
		}
97
98
		foreach( $map as $pos => $list )
99
		{
100
			if( $this->checkEntry( $list ) === false ) {
101
				continue;
102
			}
103
104
			$urls = explode( $separator, trim( $list['media.url'] ) );
105
			$type = trim( $this->getValue( $list, 'media.type', 'default' ) );
106
			$typecode = trim( $this->getValue( $list, 'catalog.lists.type', 'default' ) );
107
108
			foreach( $urls as $url )
109
			{
110
				if( isset( $listMap[$url][$type][$typecode] ) )
111
				{
112
					$listItem = $listMap[$url][$type][$typecode];
113
					$refItem = $listItem->getRefItem();
114
					unset( $listItems[ $listItem->getId() ] );
115
				}
116
				else
117
				{
118
					$listItem = $listManager->createItem( $typecode, 'media' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $typecode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
					$refItem = $manager->createItem( $type, 'catalog' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
				}
121
122
				$list['media.url'] = $url;
123
124
				$list = $refItem->fromArray( $this->addItemDefaults( $list ) );
125
				$list = $listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
126
127
				$catalog->addListItem( 'media', $listItem, $refItem );
128
			}
129
		}
130
131
		$catalog->deleteListItems( $listItems, true );
132
133
		return $this->getObject()->process( $catalog, $data );
134
	}
135
136
137
	/**
138
	 * Adds the text item default values and returns the resulting array
139
	 *
140
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
141
	 * @return array Given associative list enriched by default values if they were not already set
142
	 */
143
	protected function addItemDefaults( array $list )
144
	{
145
		if( !isset( $list['media.label'] ) ) {
146
			$list['media.label'] = $list['media.url'];
147
		}
148
149
		if( !isset( $list['media.preview'] ) ) {
150
			$list['media.preview'] = $list['media.url'];
151
		}
152
153
		if( !isset( $list['media.status'] ) ) {
154
			$list['media.status'] = 1;
155
		}
156
157
		return $list;
158
	}
159
160
161
	/**
162
	 * Checks if an entry can be used for updating a media item
163
	 *
164
	 * @param array $list Associative list of key/value pairs from the mapping
165
	 * @return boolean True if valid, false if not
166
	 */
167
	protected function checkEntry( array $list )
168
	{
169
		if( !isset( $list['media.url'] ) || trim( $list['media.url'] ) === '' || isset( $list['catalog.lists.type'] )
170
				&& $this->listTypes !== null && !in_array( trim( $list['catalog.lists.type'] ), (array) $this->listTypes )
171
		) {
172
			return false;
173
		}
174
175
		return true;
176
	}
177
}
178