Passed
Push — master ( f2d114...8516f3 )
by Aimeos
06:14 queued 03:02
created

Standard::process()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 21
nop 2
dl 0
loc 67
rs 8.0515
c 0
b 0
f 0

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-2023
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Media;
12
13
14
/**
15
 * Media 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/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Media\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 $listTypes;
35
	private $types = [];
36
	private $mimes = [];
37
38
39
	/**
40
	 * Initializes the object
41
	 *
42
	 * @param \Aimeos\MShop\ContextIface $context Context object
43
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
44
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
45
	 */
46
	public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping,
47
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
48
	{
49
		parent::__construct( $context, $mapping, $object );
50
51
		$this->mimes = array_flip( $context->config()->get( 'controller/common/media/extensions', [] ) );
52
53
		/** controller/common/product/import/csv/processor/media/listtypes
54
		 * Names of the product list types for media that are updated or removed
55
		 *
56
		 * If you want to associate media items manually via the administration
57
		 * interface to products and don't want these to be touched during the
58
		 * import, you can specify the product list types for these media
59
		 * that shouldn't be updated or removed.
60
		 *
61
		 * @param array|null List of product list type names or null for all
62
		 * @since 2015.05
63
		 * @see controller/common/product/import/csv/domains
64
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
65
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/product/import/csv/processor/product/listtypes
67
		 * @see controller/common/product/import/csv/processor/price/listtypes
68
		 * @see controller/common/product/import/csv/processor/text/listtypes
69
		 */
70
		$key = 'controller/common/product/import/csv/processor/media/listtypes';
71
		$this->listTypes = $context->config()->get( $key );
72
73
		if( $this->listTypes === null )
74
		{
75
			$this->listTypes = [];
76
			$manager = \Aimeos\MShop::create( $context, 'product/lists/type' );
77
78
			$search = $manager->filter()->slice( 0, 0x7fffffff );
79
			$search->setConditions( $search->compare( '==', 'product.lists.type.domain', 'media' ) );
80
81
			foreach( $manager->search( $search ) as $item ) {
82
				$this->listTypes[$item->getCode()] = $item->getCode();
83
			}
84
		}
85
		else
86
		{
87
			$this->listTypes = array_flip( $this->listTypes );
88
		}
89
90
91
		$manager = \Aimeos\MShop::create( $context, 'media/type' );
92
93
		$search = $manager->filter()->slice( 0, 0x7fffffff );
94
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'product' ) );
95
96
		foreach( $manager->search( $search ) as $item ) {
97
			$this->types[$item->getCode()] = $item->getCode();
98
		}
99
	}
100
101
102
	/**
103
	 * Saves the product related data to the storage
104
	 *
105
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
106
	 * @param array $data List of CSV fields with position as key and data as value
107
	 * @return array List of data which hasn't been imported
108
	 */
109
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array
110
	{
111
		$context = $this->context();
112
		$manager = \Aimeos\MShop::create( $context, 'product' );
113
		$refManager = \Aimeos\MShop::create( $context, 'media' );
114
		$separator = $context->config()->get( 'controller/common/product/import/csv/separator', "\n" );
115
116
		$listMap = [];
117
		$map = $this->getMappedChunk( $data, $this->getMapping() );
118
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
0 ignored issues
show
Unused Code introduced by
The assignment to $cntl is dead and can be removed.
Loading history...
119
		$listItems = $product->getListItems( 'media', $this->listTypes, null, false );
120
121
		foreach( $listItems as $listItem )
122
		{
123
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
124
				$listMap[$refItem->getUrl()][$refItem->getType()][$refItem->getLanguageId()][$listItem->getType()] = $listItem;
125
			}
126
		}
127
128
		foreach( $map as $pos => $list )
129
		{
130
			if( $this->checkEntry( $list ) === false ) {
131
				continue;
132
			}
133
134
			$type = $this->val( $list, 'media.type', 'default' );
135
			$langId = $this->val( $list, 'media.languageid', '' );
136
			$listtype = $this->val( $list, 'product.lists.type', 'default' );
137
			$urls = explode( $separator, $this->val( $list, 'media.url', '' ) );
138
139
			unset( $list['media.url'] );
140
141
			$this->addType( 'product/lists/type', 'media', $listtype );
142
			$this->addType( 'media/type', 'product', $type );
143
144
			foreach( $urls as $idx => $url )
145
			{
146
				$url = trim( $url );
147
148
				if( isset( $listMap[$url][$type][$langId][$listtype] ) )
149
				{
150
					$listItem = $listMap[$url][$type][$langId][$listtype];
151
					$refItem = $listItem->getRefItem();
152
					unset( $listItems[$listItem->getId()] );
153
				}
154
				else
155
				{
156
					$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

156
					$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...
157
					$refItem = $refManager->create()->setType( $type );
158
				}
159
160
				$ext = strtolower( pathinfo( $url, PATHINFO_EXTENSION ) );
0 ignored issues
show
Bug introduced by
It seems like pathinfo($url, Aimeos\Co...dia\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

160
				$ext = strtolower( /** @scrutinizer ignore-type */ pathinfo( $url, PATHINFO_EXTENSION ) );
Loading history...
161
				if( isset( $this->mimes[$ext] ) ) {
162
					$refItem->setMimeType( $this->mimes[$ext] );
163
				}
164
165
				$refItem->setDomain( 'product' );
166
				$refItem = $this->update( $refItem, $list, $url );
167
				$listItem = $listItem->setPosition( $pos++ )->fromArray( $list );
168
169
				$product->addListItem( 'media', $listItem, $refItem );
170
			}
171
		}
172
173
		$product->deleteListItems( $listItems->toArray(), true );
174
175
		return $this->object()->process( $product, $data );
176
	}
177
178
179
	/**
180
	 * Checks if an entry can be used for updating a media item
181
	 *
182
	 * @param array $list Associative list of key/value pairs from the mapping
183
	 * @return bool True if valid, false if not
184
	 */
185
	protected function checkEntry( array $list ) : bool
186
	{
187
		if( $this->val( $list, 'media.url' ) === null ) {
188
			return false;
189
		}
190
191
		if( ( $type = $this->val( $list, 'product.lists.type' ) ) && !isset( $this->listTypes[$type] ) )
192
		{
193
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'product list' );
194
			throw new \Aimeos\Controller\Common\Exception( $msg );
195
		}
196
197
		if( ( $type = $this->val( $list, 'media.type' ) ) && !isset( $this->types[$type] ) )
198
		{
199
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'media' );
200
			throw new \Aimeos\Controller\Common\Exception( $msg );
201
		}
202
203
		return true;
204
	}
205
206
207
	/**
208
	 * Updates the media item with the given key/value pairs
209
	 *
210
	 * @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update
211
	 * @param array &$list Associative list of key/value pairs, matching pairs are removed
212
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item
213
	 */
214
	protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list, string $url ) : \Aimeos\MShop\Media\Item\Iface
215
	{
216
		try
217
		{
218
			if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) {
219
				$refItem->setPreviews( $map )->setUrl( $url );
220
			} elseif( isset( $list['media.preview'] ) ) {
221
				$refItem->setPreview( $list['media.preview'] )->setUrl( $url );
222
			} elseif( $refItem->getUrl() !== $url ) {
223
				$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ), true );
224
			} else {
225
				$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ) );
226
			}
227
228
			unset( $list['media.previews'], $list['media.preview'] );
229
		}
230
		catch( \Aimeos\Controller\Common\Exception $e )
231
		{
232
			$msg = sprintf( 'Scaling image "%1$s" failed: %2$s', $url, $e->getMessage() );
233
			$this->context()->logger()->error( $msg, 'import/csv/product' );
234
		}
235
236
		return $refItem->fromArray( $list );
237
	}
238
}
239