Passed
Push — master ( f11089...634c17 )
by Aimeos
04:11
created

Base::finish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
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), 2015-2024
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor;
12
13
14
/**
15
 * Abstract class with common methods for all CSV import processors
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
abstract class Base
21
	extends \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Base
22
{
23
	use \Aimeos\Controller\Jobs\Common\Types;
24
25
26
	private \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object;
27
	private \Aimeos\MShop\ContextIface $context;
28
	private array $mapping;
29
30
31
	/**
32
	 * Initializes the object
33
	 *
34
	 * @param \Aimeos\MShop\ContextIface $context Context object
35
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
36
	 * @param \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
37
	 */
38
	public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping,
39
		\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object = null )
40
	{
41
		$this->context = $context;
42
		$this->mapping = $mapping;
43
		$this->object = $object;
44
	}
45
46
47
	/**
48
	 * Stores all types for which no type items exist yet
49
	 */
50
	public function finish()
51
	{
52
		if( $this->object ) {
53
			$this->object->finish();
54
		}
55
56
		$this->saveTypes();
57
	}
58
59
60
	/**
61
	 * Adds the list item default values and returns the resulting array
62
	 *
63
	 * @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1
64
	 * @param int $pos Computed position of the list item in the associated list of items
65
	 * @return array Given associative list enriched by default values if they were not already set
66
	 */
67
	protected function addListItemDefaults( array $list, int $pos ) : array
68
	{
69
		if( !isset( $list['product.lists.position'] ) ) {
70
			$list['product.lists.position'] = $pos;
71
		}
72
73
		if( !isset( $list['product.lists.status'] ) ) {
74
			$list['product.lists.status'] = 1;
75
		}
76
77
		return $list;
78
	}
79
80
81
	/**
82
	 * Returns the context item
83
	 *
84
	 * @return \Aimeos\MShop\ContextIface Context object
85
	 */
86
	protected function context() : \Aimeos\MShop\ContextIface
87
	{
88
		return $this->context;
89
	}
90
91
92
	/**
93
	 * Returns the configuration for the given string
94
	 *
95
	 * @param string $value Configuration string
96
	 * @return array Configuration settings
97
	 */
98
	protected function getListConfig( string $value ) : array
99
	{
100
		$config = [];
101
102
		foreach( array_filter( explode( "\n", $value ) ) as $line )
103
		{
104
			$parts = explode( ':', $line );
105
106
			if( count( $parts ) !== 2 ) {
107
				throw new \Aimeos\Controller\Jobs\Exception( 'Invalid list configuration: ' . $value );
108
			}
109
110
			list( $key, $val ) = $parts;
111
			$config[$key] = $val;
112
		}
113
114
		return $config;
115
	}
116
117
118
	/**
119
	 * Returns the mapping list
120
	 *
121
	 * @return array Associative list of field positions in CSV as keys and domain item keys as values
122
	 */
123
	protected function getMapping() : array
124
	{
125
		return $this->mapping;
126
	}
127
128
129
	/**
130
	 * Returns the decorated processor object
131
	 *
132
	 * @return \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface Processor object
133
	 * @throws \Aimeos\Controller\Jobs\Exception If no processor object is available
134
	 */
135
	protected function object() : \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface
136
	{
137
		if( $this->object === null ) {
138
			throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' );
139
		}
140
141
		return $this->object;
142
	}
143
}
144