Completed
Push — master ( 8e38ff...6c002e )
by Aimeos
04:25
created

Standard::process()   D

Complexity

Conditions 11
Paths 214

Size

Total Lines 75
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 4.8103
c 0
b 0
f 0
cc 11
eloc 44
nc 214
nop 2

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), 2015-2017
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Price;
12
13
14
/**
15
 * Price 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/price/name
25
	 * Name of the price processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Price\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
	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\Product\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/common/product/import/csv/processor/price/listtypes
51
		 * Names of the product list types for prices that are updated or removed
52
		 *
53
		 * If you want to associate price items manually via the administration
54
		 * interface to products and don't want these to be touched during the
55
		 * import, you can specify the product list types for these prices
56
		 * that shouldn't be updated or removed.
57
		 *
58
		 * @param array|null List of product list type names or null for all
59
		 * @since 2015.05
60
		 * @category Developer
61
		 * @category User
62
		 * @see controller/common/product/import/csv/domains
63
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
64
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
65
		 * @see controller/common/product/import/csv/processor/media/listtypes
66
		 * @see controller/common/product/import/csv/processor/product/listtypes
67
		 * @see controller/common/product/import/csv/processor/text/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/product/import/csv/processor/price/listtypes' );
70
	}
71
72
73
	/**
74
	 * Saves the product related data to the storage
75
	 *
76
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product 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\Product\Item\Iface $product, array $data )
81
	{
82
		$listManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/lists' );
83
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'price' );
84
		$manager->begin();
85
86
		try
87
		{
88
			$delete = $listMap = [];
89
			$map = $this->getMappedChunk( $data, $this->getMapping() );
90
			$listItems = $product->getListItems( 'price', $this->listTypes );
91
92
			foreach( $listItems as $listItem )
93
			{
94
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
95
					$listMap[ $refItem->getValue() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
96
				}
97
			}
98
99
			foreach( $map as $pos => $list )
100
			{
101
				if( $this->checkEntry( $list ) === false ) {
102
					continue;
103
				}
104
105
				$value = trim( isset( $list['price.value'] ) ? $list['price.value'] : '0.00' );
106
				$type = trim( isset( $list['price.type'] ) ? $list['price.type'] : 'default' );
107
				$typecode = trim( isset( $list['product.lists.type'] ) ? $list['product.lists.type'] : 'default' );
108
109
				if( isset( $listMap[$value][$type][$typecode] ) )
110
				{
111
					$listItem = $listMap[$value][$type][$typecode];
112
					$refItem = $listItem->getRefItem();
113
					unset( $listItems[ $listItem->getId() ] );
114
				}
115
				else
116
				{
117
					$listItem = $listManager->createItem();
118
					$refItem = $manager->createItem();
119
				}
120
121
				$list['price.typeid'] = $this->getTypeId( 'price/type', 'product', $type );
122
				$list['price.domain'] = 'product';
123
124
				$refItem->fromArray( $this->addItemDefaults( $list ) );
125
				$refItem = $manager->saveItem( $refItem );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $refItem is correct as $manager->saveItem($refItem) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
126
127
				$list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'price', $typecode );
128
				$list['product.lists.parentid'] = $product->getId();
129
				$list['product.lists.refid'] = $refItem->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $refItem (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
				$list['product.lists.domain'] = 'price';
131
132
				$listItem->fromArray( $this->addListItemDefaults( $list, $pos ) );
133
				$listManager->saveItem( $listItem, false );
134
			}
135
136
			foreach( $listItems as $listItem ) {
137
				$delete[] = $listItem->getRefId();
138
			}
139
140
			$manager->deleteItems( $delete );
141
			$listManager->deleteItems( array_keys( $listItems ) );
142
143
			$data = $this->getObject()->process( $product, $data );
144
145
			$manager->commit();
146
		}
147
		catch( \Exception $e )
148
		{
149
			$manager->rollback();
150
			throw $e;
151
		}
152
153
		return $data;
154
	}
155
156
157
	/**
158
	 * Adds the text item default values and returns the resulting array
159
	 *
160
	 * @param array $list Associative list of domain item keys and their values, e.g. "price.status" => 1
161
	 * @return array Given associative list enriched by default values if they were not already set
162
	 */
163
	protected function addItemDefaults( array $list )
164
	{
165
		if( !isset( $list['price.currencyid'] ) ) {
166
			$list['price.currencyid'] = $this->getContext()->getLocale()->getCurrencyId();
167
		}
168
169
		if( !isset( $list['price.label'] ) ) {
170
			$list['price.label'] = $list['price.currencyid'] . ' ' . $list['price.value'];
171
		}
172
173
		if( !isset( $list['price.status'] ) ) {
174
			$list['price.status'] = 1;
175
		}
176
177
		return $list;
178
	}
179
180
181
	/**
182
	 * Checks if an entry can be used for updating a media item
183
	 *
184
	 * @param array $list Associative list of key/value pairs from the mapping
185
	 * @return boolean True if valid, false if not
186
	 */
187
	protected function checkEntry( array $list )
188
	{
189
		if( !isset( $list['price.value'] ) || trim( $list['price.value'] ) === '' || isset( $list['product.lists.type'] )
190
			&& $this->listTypes !== null && !in_array( trim( $list['product.lists.type'] ), (array) $this->listTypes )
191
		) {
192
			return false;
193
		}
194
195
		return true;
196
	}
197
}
198