Passed
Push — master ( b8f18d...700c02 )
by Aimeos
33:19 queued 19:46
created

Base::getProcessors()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 25
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2023
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Coupon\Import\Csv;
12
13
14
/**
15
 * Common class for CSV coupon 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
	 * Returns the coupon code items for the given codes
25
	 *
26
	 * @param array $codes List of coupon codes
27
	 * @return array Associative list of coupon codes as key and coupon code items as value
28
	 */
29
	protected function getCouponCodeItems( array $codes ) : array
30
	{
31
		$result = [];
32
		$manager = \Aimeos\MShop::create( $this->context(), 'coupon/code' );
33
34
		$search = $manager->filter();
35
		$search->setConditions( $search->compare( '==', 'coupon.code.code', $codes ) );
36
		$search->slice( 0, count( $codes ) );
37
38
		foreach( $manager->search( $search ) as $item ) {
39
			$result[$item->getCode()] = $item;
40
		}
41
42
		return $result;
43
	}
44
45
46
	/**
47
	 * Returns the rows from the CSV file up to the maximum count
48
	 *
49
	 * @param resource $fh File handle to CSV file
50
	 * @param int $maxcnt Maximum number of rows that should be retrieved at once
51
	 * @param int $codePos Column position which contains the unique product code (starting from 0)
52
	 * @return array List of arrays with product codes as keys and list of values from the CSV file
53
	 */
54
	protected function getData( $fh, int $maxcnt, int $codePos ) : array
55
	{
56
		$count = 0;
57
		$data = [];
58
59
		while( ( $row = fgetcsv( $fh ) ) !== false && $count++ < $maxcnt ) {
60
			$data[$row[$codePos]] = $row;
61
		}
62
63
		return $data;
64
	}
65
66
67
	/**
68
	 * Returns the default mapping for the CSV fields to the domain item keys
69
	 *
70
	 * Example:
71
	 *  'item' => array(
72
	 *  	0 => 'coupon.code.code', // e.g. letters and digits
73
	 *  	1 => 'coupon.code.count', // number of time the code is available
74
	 *  ),
75
	 *
76
	 * @return array Associative list of domains as keys and a list of positions and the domain item keys as values
77
	 */
78
	protected function getDefaultMapping()
79
	{
80
		return array(
81
			'code' => array(
82
				0 => 'coupon.code.code',
83
				1 => 'coupon.code.count',
84
				2 => 'coupon.code.datestart',
85
				3 => 'coupon.code.dateend',
86
			),
87
		);
88
	}
89
90
91
	/**
92
	 * Returns the mapped data from the CSV line
93
	 *
94
	 * @param array $data List of CSV fields with position as key and domain item key as value (mapped data is removed)
95
	 * @param array $mapping List of domain item keys with the CSV field position as key
96
	 * @return array List of associative arrays containing the chunked properties
97
	 */
98
	protected function getMappedChunk( array &$data, array $mapping )
99
	{
100
		$idx = 0;
101
		$map = [];
102
103
		foreach( $mapping as $pos => $key )
104
		{
105
			if( isset( $map[$idx][$key] ) ) {
106
				$idx++;
107
			}
108
109
			if( isset( $data[$pos] ) )
110
			{
111
				$map[$idx][$key] = $data[$pos];
112
				unset( $data[$pos] );
113
			}
114
		}
115
116
		return $map;
117
	}
118
119
120
	/**
121
	 * Returns the processor object for saving the coupon related information
122
	 *
123
	 * @param array $mappings Associative list of processor types as keys and index/data mappings as values
124
	 * @return \Aimeos\Controller\Common\Coupon\Import\Csv\Processor\Iface Processor object
125
	 * @throws \LogicException If class can't be instantiated
126
	 */
127
	protected function getProcessors( array $mappings )
128
	{
129
		$context = $this->context();
130
		$config = $context->config();
131
		$object = new \Aimeos\Controller\Common\Coupon\Import\Csv\Processor\Done( $context, [] );
132
133
		foreach( $mappings as $type => $mapping )
134
		{
135
			if( ctype_alnum( $type ) === false ) {
136
				throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $type ), 400 );
137
			}
138
139
			$name = $config->get( 'controller/common/coupon/import/csv/processor/' . $type . '/name', 'Standard' );
140
141
			if( ctype_alnum( $name ) === false ) {
142
				throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 );
143
			}
144
145
			$classname = '\\Aimeos\\Controller\\Common\\Coupon\\Import\\Csv\\Processor\\' . ucfirst( $type ) . '\\' . $name;
146
			$interface = \Aimeos\Controller\Common\Coupon\Import\Csv\Processor\Iface::class;
147
148
			$object = \Aimeos\Utils::create( $classname, [$context, $mapping, $object], $interface );
149
		}
150
151
		return $object;
152
	}
153
}
154