Completed
Push — master ( 47a117...268cfa )
by Aimeos
03:32
created

Standard   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
c 3
b 0
f 0
lcom 1
cbo 6
dl 0
loc 176
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 1
C process() 0 73 8
A addItemDefaults() 0 16 4
B checkEntry() 0 10 6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
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
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
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
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $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\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/common/product/import/csv/processor/media/listtypes
51
		 * Names of the product list types for media that are updated or removed
52
		 *
53
		 * If you want to associate media items manually via the administration
54
		 * interface to products and don't want these to be touched during the
55
		 * import, you can specify the product list types for these media
56
		 * that shouldn't be updated or removed.
57
		 *
58
		 * @param array|null List of product list type names or null for all
59
		 * @since 2015.05
60
		 * @category Developer
61
		 * @category User
62
		 * @see controller/common/product/import/csv/domains
63
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
64
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
65
		 * @see controller/common/product/import/csv/processor/product/listtypes
66
		 * @see controller/common/product/import/csv/processor/price/listtypes
67
		 * @see controller/common/product/import/csv/processor/text/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/product/import/csv/processor/media/listtypes' );
70
	}
71
72
73
	/**
74
	 * Saves the product related data to the storage
75
	 *
76
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
77
	 * @param array $data List of CSV fields with position as key and data as value
78
	 * @return array List of data which hasn't been imported
79
	 */
80
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
81
	{
82
		$context = $this->getContext();
83
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
84
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' );
85
		$separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" );
86
87
		$manager->begin();
88
89
		try
90
		{
91
			$map = $this->getMappedChunk( $data );
92
			$listItems = $product->getListItems( 'media' );
93
94
			foreach( $map as $pos => $list )
95
			{
96
				if( $this->checkEntry( $list ) === false ) {
97
					continue;
98
				}
99
100
				$urls = explode( $separator, $list['media.url'] );
101
				$type = $this->getValue( $list, 'media.type', 'default' );
102
				$typecode = $this->getValue( $list, 'product.lists.type', 'default' );
103
104
				if( ( $langid = $this->getValue( $list, 'media.languageid', null ) ) === '' ) {
105
					$langid = null;
106
				}
107
108
				foreach( $urls as $url )
109
				{
110
					if( ( $listItem = array_shift( $listItems ) ) !== null ) {
111
						$refItem = $listItem->getRefItem();
112
					} else {
113
						$listItem = $listManager->createItem();
114
						$refItem = $manager->createItem();
115
					}
116
117
					$list['media.typeid'] = $this->getTypeId( 'media/type', 'product', $type );
118
					$list['media.languageid'] = $langid;
119
					$list['media.domain'] = 'product';
120
					$list['media.url'] = $url;
121
122
					$refItem->fromArray( $this->addItemDefaults( $list ) );
123
					$manager->saveItem( $refItem );
124
125
					$list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'media', $typecode );
126
					$list['product.lists.parentid'] = $product->getId();
127
					$list['product.lists.refid'] = $refItem->getId();
128
					$list['product.lists.domain'] = 'media';
129
130
					$listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
131
					$listManager->saveItem( $listItem );
132
				}
133
			}
134
135
			foreach( $listItems as $listItem )
136
			{
137
				$manager->deleteItem( $listItem->getRefItem()->getId() );
138
				$listManager->deleteItem( $listItem->getId() );
139
			}
140
141
			$remaining = $this->getObject()->process( $product, $data );
142
143
			$manager->commit();
144
		}
145
		catch( \Exception $e )
146
		{
147
			$manager->rollback();
148
			throw $e;
149
		}
150
151
		return $remaining;
152
	}
153
154
155
	/**
156
	 * Adds the text item default values and returns the resulting array
157
	 *
158
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
159
	 * @return array Given associative list enriched by default values if they were not already set
160
	 */
161
	protected function addItemDefaults( array $list )
162
	{
163
		if( !isset( $list['media.label'] ) ) {
164
			$list['media.label'] = $list['media.url'];
165
		}
166
167
		if( !isset( $list['media.preview'] ) ) {
168
			$list['media.preview'] = $list['media.url'];
169
		}
170
171
		if( !isset( $list['media.status'] ) ) {
172
			$list['media.status'] = 1;
173
		}
174
175
		return $list;
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 boolean True if valid, false if not
184
	 */
185
	protected function checkEntry( array $list )
186
	{
187
		if( !isset( $list['media.url'] ) || $list['media.url'] === '' || isset( $list['product.lists.type'] )
188
				&& $this->listTypes !== null && !in_array( $list['product.lists.type'], (array) $this->listTypes )
189
		) {
190
			return false;
191
		}
192
193
		return true;
194
	}
195
}
196