Passed
Push — master ( 547952...cdc5f0 )
by Aimeos
04:14
created

Standard::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Stock;
12
13
14
/**
15
 * Product stock 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
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/product/import/csv/processor/stock/name
25
	 * Name of the stock processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Stock\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 stock 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::create( $this->getContext(), 'stock' );
46
		$manager->begin();
47
48
		try
49
		{
50
			$map = $this->getMappedChunk( $data, $this->getMapping() );
51
			$items = $this->getStockItems( $product->getCode() );
52
53
			foreach( $map as $pos => $list )
54
			{
55
				if( !array_key_exists( 'stock.stocklevel', $list ) ) {
56
					continue;
57
				}
58
59
				$list['stock.productcode'] = $product->getCode();
60
				$list['stock.dateback'] = $this->getValue( $list, 'stock.dateback' );
61
				$list['stock.stocklevel'] = $this->getValue( $list, 'stock.stocklevel' );
62
				$list['stock.type'] = $this->getValue( $list, 'stock.type', 'default' );
63
64
				$this->addType( 'stock/type', 'product', $list['stock.type'] );
65
66
				if( ( $item = array_pop( $items ) ) === null ) {
67
					$item = $manager->createItem();
68
				}
69
70
				$manager->saveItem( $item->fromArray( $list ), false );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
				$manager->/** @scrutinizer ignore-call */ 
71
              saveItem( $item->fromArray( $list ), false );

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...
71
			}
72
73
			$manager->deleteItems( array_keys( $items ) );
74
75
			$data = $this->getObject()->process( $product, $data );
76
77
			$manager->commit();
78
		}
79
		catch( \Exception $e )
80
		{
81
			$manager->rollback();
82
			throw $e;
83
		}
84
85
		return $data;
86
	}
87
88
89
	/**
90
	 * Returns the stock items for the given product code
91
	 *
92
	 * @param string $code Unique product code
93
	 * @return \Aimeos\MShop\Stock\Item\Iface[] Associative list of stock items
94
	 */
95
	protected function getStockItems( $code )
96
	{
97
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
98
99
		$search = $manager->createSearch();
100
		$search->setConditions( $search->compare( '==', 'stock.productcode', $code ) );
101
102
		return $manager->searchItems( $search );
103
	}
104
}
105