Standard::checkEntry()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
nc 4
nop 1
dl 0
loc 19
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text;
12
13
14
/**
15
 * Text 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/text/name
25
	 * Name of the text processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\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/text/listtypes
53
		 * Names of the product list types for texts that are updated or removed
54
		 *
55
		 * If you want to associate text 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 texts
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/price/listtypes
68
		 * @see controller/jobs/product/import/csv/product/listtypes
69
		 * @see controller/jobs/product/import/csv/supplier/listtypes
70
		 */
71
		$default = $config->get( 'controller/jobs/product/import/csv/processor/text/listtypes' );
72
		$this->listTypes = $config->get( 'controller/jobs/product/import/csv/text/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, 'text/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
		$context = $this->context();
109
		$manager = \Aimeos\MShop::create( $context, 'product' );
110
		$refManager = \Aimeos\MShop::create( $context, 'text' );
111
112
		$pos = 0;
113
		$listMap = [];
114
		$map = $this->getMappedChunk( $data, $this->getMapping() );
115
		$listItems = $product->getListItems( 'text', $this->listTypes, null, false );
116
117
		foreach( $listItems as $listItem )
118
		{
119
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
120
				$listMap[$refItem->getContent()][$refItem->getLanguageId()][$refItem->getType()][$listItem->getType()] = $listItem;
121
			}
122
		}
123
124
		foreach( $map as $list )
125
		{
126
			if( $this->checkEntry( $list ) === false ) {
127
				continue;
128
			}
129
130
			$listtype = trim( $this->val( $list, 'product.lists.type', 'default' ) );
131
			$listConfig = $this->getListConfig( trim( $this->val( $list, 'product.lists.config', '' ) ) );
132
133
			unset( $list['product.lists.config'] );
134
135
			$language = trim( $this->val( $list, 'text.languageid', '' ) );
136
			$content = trim( $this->val( $list, 'text.content', '' ) );
137
			$type = trim( $this->val( $list, 'text.type', 'name' ) );
138
139
			$this->addType( 'product/lists/type', 'text', $listtype );
140
			$this->addType( 'text/type', 'product', $type );
141
142
			if( isset( $listMap[$content][$language][$type][$listtype] ) )
143
			{
144
				$listItem = $listMap[$content][$language][$type][$listtype];
145
				$refItem = $listItem->getRefItem();
146
				unset( $listItems[$listItem->getId()] );
147
			}
148
			else
149
			{
150
				$listItem = $manager->createListItem()->setType( $listtype );
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

150
				$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->setType( $listtype );

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