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

Base::getCache()   B

Complexity

Conditions 7
Paths 10

Size

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