Passed
Push — master ( 108d2e...42466b )
by Aimeos
24:07 queued 21:25
created

Base   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 148
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMapping() 0 3 1
A __construct() 0 6 1
A addListItemDefaults() 0 11 3
B finish() 0 43 7
A addType() 0 4 1
A object() 0 7 2
A context() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor;
12
13
use \Aimeos\MW\Logger\Base as Log;
14
15
16
/**
17
 * Abstract class with common methods for all CSV import processors
18
 *
19
 * @package Controller
20
 * @subpackage Common
21
 */
22
abstract class Base
23
	extends \Aimeos\Controller\Common\Product\Import\Csv\Base
24
{
25
	private $context;
26
	private $mapping;
27
	private $object;
28
	private $types = [];
29
30
31
	/**
32
	 * Initializes the object
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $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\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
37
	 */
38
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
39
		\Aimeos\Controller\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();
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

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