Passed
Push — master ( 5b947d...494d83 )
by Aimeos
14:32
created

Base::getListConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
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-2023
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\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\Common\Product\Import\Csv\Base
22
{
23
	private \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object;
24
	private \Aimeos\MShop\ContextIface $context;
25
	private array $types = [];
26
	private array $mapping;
27
28
29
	/**
30
	 * Initializes the object
31
	 *
32
	 * @param \Aimeos\MShop\ContextIface $context Context object
33
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
34
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
35
	 */
36
	public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping,
37
		\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
38
	{
39
		$this->context = $context;
40
		$this->mapping = $mapping;
41
		$this->object = $object;
42
	}
43
44
45
	/**
46
	 * Stores all types for which no type items exist yet
47
	 */
48
	public function finish()
49
	{
50
		if( $this->object ) {
51
			$this->object->finish();
0 ignored issues
show
Bug introduced by
The method finish() does not exist on Aimeos\Controller\Common...ort\Csv\Processor\Iface. Since it exists in all sub-types, consider adding an abstract or default implementation to Aimeos\Controller\Common...ort\Csv\Processor\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
			$this->object->/** @scrutinizer ignore-call */ 
52
                  finish();
Loading history...
52
		}
53
54
		foreach( $this->types as $path => $list )
55
		{
56
			$manager = \Aimeos\MShop::create( $this->context, $path );
57
			$prefix = str_replace( '/', '.', $path );
58
59
			foreach( $list as $domain => $codes )
60
			{
61
				$manager->begin();
62
63
				try
64
				{
65
					$search = $manager->filter()->slice( 0, 10000 );
66
					$expr = [
67
						$search->compare( '==', $prefix . '.domain', $domain ),
68
						$search->compare( '==', $prefix . '.code', $codes )
69
					];
70
					$search->setConditions( $search->and( $expr ) );
71
72
					$types = $items = [];
73
74
					foreach( $manager->search( $search ) as $item ) {
75
						$types[] = $item->getCode();
76
					}
77
78
					foreach( array_diff( $codes, $types ) as $code ) {
79
						$items[] = $manager->create()->setDomain( $domain )->setCode( $code )->setLabel( $code );
80
					}
81
82
					$manager->save( $items, false );
83
					$manager->commit();
84
				}
85
				catch( \Exception $e )
86
				{
87
					$manager->rollback();
88
89
					$msg = 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString();
90
					$this->context->logger()->error( $msg, 'import/csv/product' );
91
				}
92
			}
93
		}
94
	}
95
96
97
	/**
98
	 * Registers a used type which is going to be saved if it doesn't exist yet
99
	 *
100
	 * @param string $path Manager path, e.g. "product/lists/type"
101
	 * @param string $domain Domain name the type belongs to, e.g. "attribute"
102
	 * @param string $code Type code
103
	 * @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Same object for fluent interface
104
	 */
105
	protected function addType( string $path, string $domain, string $code ) : Iface
106
	{
107
		$this->types[$path][$domain][$code] = $code;
108
		return $this;
109
	}
110
111
112
	/**
113
	 * Adds the list item default values and returns the resulting array
114
	 *
115
	 * @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1
116
	 * @param int $pos Computed position of the list item in the associated list of items
117
	 * @return array Given associative list enriched by default values if they were not already set
118
	 */
119
	protected function addListItemDefaults( array $list, int $pos ) : array
120
	{
121
		if( !isset( $list['product.lists.position'] ) ) {
122
			$list['product.lists.position'] = $pos;
123
		}
124
125
		if( !isset( $list['product.lists.status'] ) ) {
126
			$list['product.lists.status'] = 1;
127
		}
128
129
		return $list;
130
	}
131
132
133
	/**
134
	 * Returns the context item
135
	 *
136
	 * @return \Aimeos\MShop\ContextIface Context object
137
	 */
138
	protected function context() : \Aimeos\MShop\ContextIface
139
	{
140
		return $this->context;
141
	}
142
143
144
	/**
145
	 * Returns the configuration for the given string
146
	 *
147
	 * @param string $value Configuration string
148
	 * @return array Configuration settings
149
	 */
150
	protected function getListConfig( string $value ) : array
151
	{
152
		$config = [];
153
154
		foreach( array_filter( explode( "\n", $value ) ) as $line )
155
		{
156
			list( $key, $val ) = explode( ':', $line );
157
			$config[$key] = $val;
158
		}
159
160
		return $config;
161
	}
162
163
164
	/**
165
	 * Returns the mapping list
166
	 *
167
	 * @return array Associative list of field positions in CSV as keys and domain item keys as values
168
	 */
169
	protected function getMapping() : array
170
	{
171
		return $this->mapping;
172
	}
173
174
175
	/**
176
	 * Returns the decorated processor object
177
	 *
178
	 * @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Processor object
179
	 * @throws \Aimeos\Controller\Jobs\Exception If no processor object is available
180
	 */
181
	protected function object() : \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
182
	{
183
		if( $this->object === null ) {
184
			throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' );
185
		}
186
187
		return $this->object;
188
	}
189
}
190