Standard   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 59
c 2
b 0
f 0
dl 0
loc 166
rs 10
wmc 19

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 50 4
B process() 0 52 8
B checkEntry() 0 19 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\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\Jobs\Common\Product\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface
23
{
24
	/** controller/jobs/product/import/csv/processor/price/name
25
	 * Name of the price processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Jobs\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
	 */
33
34
	private ?array $listTypes = null;
35
	private array $types = [];
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\ContextIface $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\Jobs\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping,
46
			?\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		$config = $context->config();
51
52
		/** controller/jobs/product/import/csv/price/listtypes
53
		 * Names of the product list types for prices that are updated or removed
54
		 *
55
		 * If you want to associate price items manually via the administration
56
		 * interface to products and don't want these to be touched during the
57
		 * import, you can specify the product list types for these prices
58
		 * that shouldn't be updated or removed.
59
		 *
60
		 * @param array|null List of product list type names or null for all
61
		 * @since 2015.05
62
		 * @see controller/jobs/product/import/csv/domains
63
		 * @see controller/jobs/product/import/csv/separator
64
		 * @see controller/jobs/product/import/csv/attribute/listtypes
65
		 * @see controller/jobs/product/import/csv/catalog/listtypes
66
		 * @see controller/jobs/product/import/csv/media/listtypes
67
		 * @see controller/jobs/product/import/csv/product/listtypes
68
		 * @see controller/jobs/product/import/csv/supplier/listtypes
69
		 * @see controller/jobs/product/import/csv/text/listtypes
70
		 */
71
		$default = $config->get( 'controller/jobs/product/import/csv/processor/price/listtypes' );
72
		$this->listTypes = $config->get( 'controller/jobs/product/import/csv/price/listtypes', $default );
73
74
		if( $this->listTypes === null )
75
		{
76
			$this->listTypes = [];
77
			$manager = \Aimeos\MShop::create( $context, 'product/lists/type' );
78
			$search = $manager->filter()->slice( 0, 0x7fffffff );
79
80
			foreach( $manager->search( $search ) as $item ) {
81
				$this->listTypes[$item->getCode()] = $item->getCode();
82
			}
83
		}
84
		else
85
		{
86
			$this->listTypes = array_combine( $this->listTypes, $this->listTypes );
87
		}
88
89
90
		$manager = \Aimeos\MShop::create( $context, 'price/type' );
91
		$search = $manager->filter()->slice( 0, 0x7fffffff );
92
93
		foreach( $manager->search( $search ) as $item ) {
94
			$this->types[$item->getCode()] = $item->getCode();
95
		}
96
	}
97
98
99
	/**
100
	 * Saves the product related data to the storage
101
	 *
102
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
103
	 * @param array $data List of CSV fields with position as key and data as value
104
	 * @return array List of data which hasn't been imported
105
	 */
106
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array
107
	{
108
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
109
		$refManager = \Aimeos\MShop::create( $this->context(), 'price' );
110
111
		$listMap = [];
112
		$map = $this->getMappedChunk( $data, $this->getMapping() );
113
		$listItems = $product->getListItems( 'price', $this->listTypes, null, false );
114
115
		foreach( $listItems as $listItem )
116
		{
117
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
118
				$listMap[$refItem->getType()][$listItem->getType()][] = $listItem;
119
			}
120
		}
121
122
		foreach( $map as $pos => $list )
123
		{
124
			if( $this->checkEntry( $list ) === false ) {
125
				continue;
126
			}
127
128
			$type = trim( $this->val( $list, 'price.type', 'default' ) );
129
			$listtype = trim( $this->val( $list, 'product.lists.type', 'default' ) );
130
			$listConfig = $this->getListConfig( trim( $this->val( $list, 'product.lists.config', '' ) ) );
131
132
			$this->addType( 'product/lists/type', 'price', $listtype );
133
			$this->addType( 'price/type', 'product', $type );
134
135
			if( isset( $listMap[$type][$listtype] ) && !empty( $listMap[$type][$listtype] ) ) {
136
				$listItem = array_shift( $listMap[$type][$listtype] );
137
			} else {
138
				$listItem = $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

138
				/** @scrutinizer ignore-call */ 
139
    $listItem = $manager->createListItem();

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...
139
			}
140
141
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
142
				$refItem = $refManager->create();
143
			}
144
145
			$listItem = $listItem->setType( $listtype )->setPosition( $pos )->fromArray( $list )->setConfig( $listConfig );
146
147
			$label = $this->val( $list, 'price.currencyid', '' ) . ' ' . $this->val( $list, 'price.value', '' );
148
			$refItem = $refItem->setType( $type )->setLabel( $label )->fromArray( $list );
149
150
			$product->addListItem( 'price', $listItem, $refItem );
151
152
			unset( $listItems[$listItem->getId()] );
153
		}
154
155
		$product->deleteListItems( $listItems->toArray(), true );
156
157
		return $this->object()->process( $product, $data );
158
	}
159
160
161
	/**
162
	 * Checks if an entry can be used for updating a price item
163
	 *
164
	 * @param array $list Associative list of key/value pairs from the mapping
165
	 * @return bool True if valid, false if not
166
	 */
167
	protected function checkEntry( array $list ) : bool
168
	{
169
		if( $this->val( $list, 'price.value' ) === null && $this->val( $list, 'price.currencyid' ) === null ) {
170
			return false;
171
		}
172
173
		if( ( $type = trim( $this->val( $list, 'product.lists.type', 'default' ) ) ) && !isset( $this->listTypes[$type] ) )
174
		{
175
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'product list' );
176
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
177
		}
178
179
		if( ( $type = trim( $this->val( $list, 'price.type', 'default' ) ) ) && !isset( $this->types[$type] ) )
180
		{
181
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'price' );
182
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
183
		}
184
185
		return true;
186
	}
187
}
188