Completed
Pull Request — master (#25)
by
unknown
02:59
created

Base::getCache()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 16
c 1
b 1
f 0
nc 10
nop 2
dl 0
loc 34
rs 8.8333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Supplier\Import\Csv;
12
13
14
/**
15
 * Common class for CSV supplier import job controllers and processors.
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Base
21
	extends \Aimeos\Controller\Jobs\Base
22
{
23
	/**
24
	 * Converts the CSV field data using the available converter objects
25
	 *
26
	 * @param array $convlist Associative list of CSV field indexes and converter objects
27
	 * @param array $data Associative list of supplier codes and lists of CSV field indexes and their data
28
	 * @return array Associative list of CSV field indexes and their converted data
29
	 */
30
	protected function convertData( array $convlist, array $data ) : array
31
	{
32
		foreach( $convlist as $idx => $converter )
33
		{
34
			foreach( $data as $code => $list )
35
			{
36
				if( isset( $list[$idx] ) )
37
				{
38
					$data[$code][$idx] = $converter->translate( $list[$idx] );
39
				}
40
			}
41
		}
42
43
		return $data;
44
	}
45
46
47
	/**
48
	 * Returns the cache object for the given type
49
	 *
50
	 * @param string $type Type of the cached data
51
	 * @param string|null $name Name of the cache implementation
52
	 * @return \Aimeos\Controller\Common\Supplier\Import\Csv\Cache\Iface Cache object
53
	 */
54
	protected function getCache( string $type, $name = null ) : \Aimeos\Controller\Common\Supplier\Import\Csv\Cache\Iface
55
	{
56
		$context = $this->getContext();
57
		$config = $context->getConfig();
58
59
		if( ctype_alnum( $type ) === false )
60
		{
61
			$classname = is_string( $name ) ? '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Cache\\' . $type : '<not a string>';
62
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
63
		}
64
65
		if( $name === null )
66
		{
67
			$name = $config->get( 'controller/common/supplier/import/csv/cache/' . $type . '/name', 'Standard' );
68
		}
69
70
		if( ctype_alnum( $name ) === false )
71
		{
72
			$classname = is_string( $name ) ? '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Cache\\' . $type . '\\' . $name : '<not a string>';
73
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
74
		}
75
76
		$classname = '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Cache\\' . ucfirst( $type ) . '\\' . $name;
77
78
		if( class_exists( $classname ) === false )
79
		{
80
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
81
		}
82
83
		$object = new $classname( $context );
84
85
		\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Cache\\Iface', $object );
86
87
		return $object;
88
	}
89
90
91
	/**
92
	 * Returns the list of converter objects based on the given converter map
93
	 *
94
	 * @param array $convmap List of converter names for the values at the position in the CSV file
95
	 * @return array Associative list of positions and converter objects
96
	 */
97
	protected function getConverterList( array $convmap ) : array
98
	{
99
		$convlist = [];
100
101
		foreach( $convmap as $idx => $name )
102
		{
103
			$convlist[$idx] = \Aimeos\MW\Convert\Factory::createConverter( $name );
104
		}
105
106
		return $convlist;
107
	}
108
109
110
	/**
111
	 * Returns the rows from the CSV file up to the maximum count
112
	 *
113
	 * @param \Aimeos\MW\Container\Content\Iface $content CSV content object
114
	 * @param int $maxcnt Maximum number of rows that should be retrieved at once
115
	 * @param int $codePos Column position which contains the unique supplier code (starting from 0)
116
	 * @return array List of arrays with supplier codes as keys and list of values from the CSV file
117
	 */
118
	protected function getData( \Aimeos\MW\Container\Content\Iface $content, int $maxcnt, int $codePos ) : array
119
	{
120
		$count = 0;
121
		$data = [];
122
123
		while( $content->valid() && $count++ < $maxcnt )
124
		{
125
			$row = $content->current();
126
			$data[$row[$codePos]] = $row;
127
			$content->next();
128
		}
129
130
		return $data;
131
	}
132
133
134
	/**
135
	 * Returns the default mapping for the CSV fields to the domain item keys
136
	 *
137
	 * Example:
138
	 *  'item' => array(
139
	 *    0 => 'supplier.code', // e.g. unique EAN code
140
	 *    1 => 'supplier.parent', // Code of parent supplier node
141
	 *    2 => 'supplier.label', // UTF-8 encoded text, also used as supplier name
142
	 *    3 => 'supplier.status', // If supplier should be shown in the frontend
143
	 *  ),
144
	 *  'text' => array(
145
	 *    3 => 'text.type', // e.g. "short" for short description
146
	 *    4 => 'text.content', // UTF-8 encoded text
147
	 *  ),
148
	 *  'media' => array(
149
	 *    5 => 'media.url', // relative URL of the supplier image on the server
150
	 *  ),
151
	 *  'address' => array(
152
	 *    6 => supplier.address.countryid', // Country id by ISO 3166-1. e.g. Germany is DE
153
	 *    6 => supplier.address.languageid', // e.g. en for English
154
	 *    6 => supplier.address.city', // e.g. Berlin
155
	 *  ),
156
	 * @return array Associative list of domains as keys ("item" is special for the supplier itself) and a list of
157
	 *    positions and the domain item keys as values.
158
	 */
159
	protected function getDefaultMapping() : array
160
	{
161
		return array(
162
			'item' => array(
163
				0 => 'supplier.code',
164
				1 => 'supplier.label',
165
				2 => 'supplier.status',
166
			),
167
			'text' => array(
168
				3 => 'text.languageid',
169
				4 => 'text.type',
170
				5 => 'text.content',
171
			),
172
			'media' => array(
173
				6 => 'media.type',
174
				7 => 'media.url',
175
			),
176
			'address' => array(
177
				8 => 'supplier.address.languageid',
178
				9 => 'supplier.address.countryid',
179
				10 => 'supplier.address.city',
180
			),
181
		);
182
	}
183
184
185
	/**
186
	 * Returns the mapped data from the CSV line
187
	 *
188
	 * @param array $data List of CSV fields with position as key and domain item key as value (mapped data is removed)
189
	 * @param array $mapping List of domain item keys with the CSV field position as key
190
	 * @return array List of associative arrays containing the chunked properties
191
	 */
192
	protected function getMappedChunk( array &$data, array $mapping ) : array
193
	{
194
		$idx = 0;
195
		$map = [];
196
197
		foreach( $mapping as $pos => $key )
198
		{
199
			if( isset( $map[$idx][$key] ) )
200
			{
201
				$idx++;
202
			}
203
204
			if( isset( $data[$pos] ) )
205
			{
206
				$map[$idx][$key] = $data[$pos];
207
				unset( $data[$pos] );
208
			}
209
		}
210
211
		return $map;
212
	}
213
214
215
	/**
216
	 * Returns the processor object for saving the supplier related information
217
	 *
218
	 * @param array $mappings Associative list of processor types as keys and index/data mappings as values
219
	 * @return \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface Processor object
220
	 */
221
	protected function getProcessors( array $mappings ) : \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface
222
	{
223
		$context = $this->getContext();
224
		$config = $context->getConfig();
225
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Done( $context, [] );
226
227
		foreach( $mappings as $type => $mapping )
228
		{
229
			if( ctype_alnum( $type ) === false )
230
			{
231
				$classname = is_string( $type ) ? '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Processor\\' . $type : '<not a string>';
232
				throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
233
			}
234
235
			$name = $config->get( 'controller/common/supplier/import/csv/processor/' . $type . '/name', 'Standard' );
236
237
			if( ctype_alnum( $name ) === false )
238
			{
239
				$classname = is_string( $name ) ? '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Processor\\' . $type . '\\' . $name : '<not a string>';
240
				throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
241
			}
242
243
			$classname = '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Processor\\' . ucfirst( $type ) . '\\' . $name;
244
245
			if( class_exists( $classname ) === false )
246
			{
247
				throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
248
			}
249
250
			$object = new $classname( $context, $mapping, $object );
251
252
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Controller\\Common\\Supplier\\Import\\Csv\\Processor\\Iface', $object );
253
		}
254
255
		return $object;
256
	}
257
}
258