Completed
Push — master ( a3e1ac...484d40 )
by Aimeos
02:36
created

Standard::process()   B

Complexity

Conditions 7
Paths 47

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.5251
c 0
b 0
f 0
cc 7
eloc 29
nc 47
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), 2015-2016
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Property;
12
13
14
/**
15
 * Product property processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/** controller/common/product/import/csv/processor/property/name
25
	 * Name of the property processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Property\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 2015.10
32
	 * @category Developer
33
	 */
34
35
36
	/**
37
	 * Saves the product property related data to the storage
38
	 *
39
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
40
	 * @param array $data List of CSV fields with position as key and data as value
41
	 * @return array List of data which hasn't been imported
42
	 */
43
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
44
	{
45
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/property' );
46
		$manager->begin();
47
48
		try
49
		{
50
			$propPap = array();
0 ignored issues
show
Unused Code introduced by
$propPap 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...
51
			$map = $this->getMappedChunk( $data );
52
			$items = $this->getPropertyItems( $product->getId() );
53
54
			foreach( $items as $item ) {
55
				$propMap[ $item->getValue() ][ $item->getType() ] = $item;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$propMap was never initialized. Although not strictly required by PHP, it is generally a good practice to add $propMap = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
56
			}
57
58
			foreach( $map as $list )
59
			{
60
				if( $list['product.property.type'] == '' || $list['product.property.value'] == '' ) {
61
					continue;
62
				}
63
64
				$typecode = $list['product.property.type'];
65
				$list['product.property.typeid'] = $this->getTypeId( 'product/property/type', 'product', $typecode );
66
				$list['product.property.parentid'] = $product->getId();
67
68
				if( isset( $propMap[ $list['product.property.value'] ][$typecode] ) )
69
				{
70
					$item = $propMap[ $list['product.property.value'] ][$typecode];
71
					unset( $items[ $item->getId() ] );
72
				}
73
				else
74
				{
75
					$item = $manager->createItem();
76
				}
77
78
				$item->fromArray( $list );
79
				$manager->saveItem( $item );
80
			}
81
82
			$manager->deleteItems( array_keys( $items ) );
83
84
			$remaining = $this->getObject()->process( $product, $data );
85
86
			$manager->commit();
87
		}
88
		catch( \Exception $e )
89
		{
90
			$manager->rollback();
91
			throw $e;
92
		}
93
94
		return $remaining;
95
	}
96
97
98
	/**
99
	 * Returns the product properties for the given product ID
100
	 *
101
	 * @param string $prodid Unique product ID
102
	 * @return array Associative list of product property items
103
	 */
104
	protected function getPropertyItems( $prodid )
105
	{
106
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/property' );
107
108
		$search = $manager->createSearch();
109
		$search->setConditions( $search->compare( '==', 'product.property.parentid', $prodid ) );
110
111
		return $manager->searchItems( $search );
112
	}
113
}
114