Passed
Push — master ( 9305f0...d2c20b )
by Aimeos
11:48
created

Standard::process()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 8
eloc 38
c 5
b 0
f 0
nc 21
nop 2
dl 0
loc 66
rs 8.0675

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-2021
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
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
	private $types = [];
37
	private $mimes = [];
38
39
40
	/**
41
	 * Initializes the object
42
	 *
43
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
44
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
45
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
46
	 */
47
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
48
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
49
	{
50
		parent::__construct( $context, $mapping, $object );
51
52
		$this->mimes = array_flip( $context->getConfig()->get( 'controller/common/media/extensions', [] ) );
53
54
		/** controller/common/product/import/csv/processor/media/listtypes
55
		 * Names of the product list types for media that are updated or removed
56
		 *
57
		 * If you want to associate media items manually via the administration
58
		 * interface to products and don't want these to be touched during the
59
		 * import, you can specify the product list types for these media
60
		 * that shouldn't be updated or removed.
61
		 *
62
		 * @param array|null List of product list type names or null for all
63
		 * @since 2015.05
64
		 * @category Developer
65
		 * @category User
66
		 * @see controller/common/product/import/csv/domains
67
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
68
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
69
		 * @see controller/common/product/import/csv/processor/product/listtypes
70
		 * @see controller/common/product/import/csv/processor/price/listtypes
71
		 * @see controller/common/product/import/csv/processor/text/listtypes
72
		 */
73
		$key = 'controller/common/product/import/csv/processor/media/listtypes';
74
		$this->listTypes = $context->getConfig()->get( $key );
75
76
		if( $this->listTypes === null )
77
		{
78
			$this->listTypes = [];
79
			$manager = \Aimeos\MShop::create( $context, 'product/lists/type' );
80
81
			$search = $manager->filter()->slice( 0, 0x7fffffff );
82
			$search->setConditions( $search->compare( '==', 'product.lists.type.domain', 'media' ) );
83
84
			foreach( $manager->search( $search ) as $item ) {
85
				$this->listTypes[$item->getCode()] = $item->getCode();
86
			}
87
		}
88
		else
89
		{
90
			$this->listTypes = array_flip( $this->listTypes );
91
		}
92
93
94
		$manager = \Aimeos\MShop::create( $context, 'media/type' );
95
96
		$search = $manager->filter()->slice( 0, 0x7fffffff );
97
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'product' ) );
98
99
		foreach( $manager->search( $search ) as $item ) {
100
			$this->types[$item->getCode()] = $item->getCode();
101
		}
102
	}
103
104
105
	/**
106
	 * Saves the product related data to the storage
107
	 *
108
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
109
	 * @param array $data List of CSV fields with position as key and data as value
110
	 * @return array List of data which hasn't been imported
111
	 */
112
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array
113
	{
114
		$context = $this->getContext();
115
		$manager = \Aimeos\MShop::create( $context, 'media' );
116
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
117
		$separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" );
118
119
		$listMap = [];
120
		$map = $this->getMappedChunk( $data, $this->getMapping() );
121
		$listItems = $product->getListItems( 'media', $this->listTypes );
122
		$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...
123
124
		foreach( $listItems as $listItem )
125
		{
126
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
127
				$listMap[$refItem->getUrl()][$refItem->getType()][$refItem->getLanguageId()][$listItem->getType()] = $listItem;
128
			}
129
		}
130
131
		foreach( $map as $pos => $list )
132
		{
133
			if( $this->checkEntry( $list ) === false ) {
134
				continue;
135
			}
136
137
			$type = $this->getValue( $list, 'media.type', 'default' );
138
			$langId = $this->getValue( $list, 'media.languageid', '' );
139
			$listtype = $this->getValue( $list, 'product.lists.type', 'default' );
140
			$urls = explode( $separator, $this->getValue( $list, 'media.url', '' ) );
141
142
			unset( $list['media.url'] );
143
144
			$this->addType( 'product/lists/type', 'media', $listtype );
145
			$this->addType( 'media/type', 'product', $type );
146
147
			foreach( $urls as $idx => $url )
148
			{
149
				$url = trim( $url );
150
151
				if( isset( $listMap[$url][$type][$langId][$listtype] ) )
152
				{
153
					$listItem = $listMap[$url][$type][$langId][$listtype];
154
					$refItem = $listItem->getRefItem();
155
					unset( $listItems[$listItem->getId()] );
156
				}
157
				else
158
				{
159
					$listItem = $listManager->create()->setType( $listtype );
160
					$refItem = $manager->create()->setType( $type );
161
				}
162
163
				$ext = pathinfo( $url, PATHINFO_EXTENSION );
164
				if( isset( $this->mimes[$ext] ) ) {
165
					$refItem->setMimeType( $this->mimes[$ext] );
166
				}
167
168
				$refItem = $this->update( $refItem, $list, $url );
169
				$listItem = $listItem->setPosition( $pos++ )->fromArray( $list );
170
171
				$product->addListItem( 'media', $listItem, $refItem );
172
			}
173
		}
174
175
		$product->deleteListItems( $listItems->toArray(), true );
176
177
		return $this->getObject()->process( $product, $data );
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 bool True if valid, false if not
186
	 */
187
	protected function checkEntry( array $list ) : bool
188
	{
189
		if( $this->getValue( $list, 'media.url' ) === null ) {
190
			return false;
191
		}
192
193
		if( ( $type = $this->getValue( $list, 'product.lists.type' ) ) && !isset( $this->listTypes[$type] ) )
194
		{
195
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'product list' );
196
			throw new \Aimeos\Controller\Common\Exception( $msg );
197
		}
198
199
		if( ( $type = $this->getValue( $list, 'media.type' ) ) && !isset( $this->types[$type] ) )
200
		{
201
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'media' );
202
			throw new \Aimeos\Controller\Common\Exception( $msg );
203
		}
204
205
		return true;
206
	}
207
208
209
	/**
210
	 * Updates the media item with the given key/value pairs
211
	 *
212
	 * @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update
213
	 * @param array &$list Associative list of key/value pairs, matching pairs are removed
214
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item
215
	 */
216
	protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list, string $url ) : \Aimeos\MShop\Media\Item\Iface
217
	{
218
		$context = $this->getContext();
219
		$fs = $context->fs( 'fs-media' );
220
221
		try
222
		{
223
			if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) {
224
				$refItem->setPreviews( $map )->setUrl( $url );
225
			} elseif( isset( $list['media.preview'] ) ) {
226
				$refItem->setPreview( $list['media.preview'] )->setUrl( $url );
227
			} elseif( $refItem->getPreviews() === [] || $refItem->getUrl() !== $url
228
				|| $fs->has( $url ) && (
229
					!( $fs instanceof \Aimeos\MW\Filesystem\MetaIface )
230
					|| date( 'Y-m-d H:i:s', $fs->time( $url ) ) > $refItem->getTimeModified()
231
				)
232
			) {
233
				$refItem = \Aimeos\Controller\Common\Media\Factory::create( $context )->scale( $refItem->setUrl( $url ) );
234
			}
235
236
			unset( $list['media.previews'], $list['media.preview'] );
237
		}
238
		catch( \Aimeos\Controller\Common\Exception $e )
239
		{
240
			$context->getLogger()->log( sprintf( 'Scaling image "%1$s" failed: %2$s', $url, $e->getMessage() ) );
241
		}
242
243
		return $refItem->fromArray( $list );
244
	}
245
}
246