Passed
Push — master ( 44dd5c...d886ca )
by Aimeos
02:13
created

Base::finish()   B

Complexity

Conditions 7
Paths 60

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 60
nop 0
dl 0
loc 41
rs 8.6186
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-2018
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 $context;
24
	private $mapping;
25
	private $object;
26
	private $types = [];
27
28
29
	/**
30
	 * Initializes the object
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $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\Context\Item\Iface $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->createSearch()->setSlice( 0, 10000 );
66
					$expr = [
67
						$search->compare( '==', $prefix . '.domain', $domain ),
68
						$search->compare( '==', $prefix . '.code', $codes )
69
					];
70
					$search->setConditions( $search->combine( '&&', $expr ) );
71
72
					$types = $items = [];
73
74
					foreach( $manager->searchItems( $search ) as $item ) {
75
						$types[] = $item->getCode();
1 ignored issue
show
Bug introduced by
The method getCode() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MAdmin\Log\Item\Iface or Aimeos\MShop\Order\Item\Base\Product\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MAdmin\Cache\Item\Iface or Aimeos\MShop\Common\Item\Status\Iface or Aimeos\MShop\Stock\Item\Iface or Aimeos\MShop\Media\Item\Iface or Aimeos\MShop\Order\Item\Iface or Aimeos\MShop\Subscription\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Common\Item\ListRef\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Order\Item\Status\Iface or Aimeos\MShop\Common\Item\Lists\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\PropertyRef\Iface or Aimeos\MShop\Tag\Item\Iface or Aimeos\MShop\Order\Item\Base\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Common\Item\Address\Iface or Aimeos\MShop\Common\Item\Property\Iface or Aimeos\MShop\Common\Item\AddressRef\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Order\Item\Base\Product\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MShop\Media\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Common\Item\Lists\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MShop\Media\Item\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Media\Item\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Order\Item\Base\Base or Aimeos\MShop\Order\Item\Base\Standard or Aimeos\MShop\Customer\Item\Address\Iface or Aimeos\MShop\Order\Item\Base\Address\Iface or Aimeos\MShop\Supplier\Item\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

75
						/** @scrutinizer ignore-call */ 
76
      $types[] = $item->getCode();
Loading history...
76
					}
77
78
					foreach( array_diff( $codes, $types ) as $code ) {
79
						$items[] = $manager->createItem()->setDomain( $domain )->setCode( $code )->setLabel( $code );
1 ignored issue
show
Bug introduced by
The method setDomain() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Product\Item\Iface or Aimeos\MAdmin\Log\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Order\Item\Base\Product\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MAdmin\Cache\Item\Iface or Aimeos\MShop\Common\Item\Status\Iface or Aimeos\MShop\Order\Item\Base\Service\Iface or Aimeos\MShop\Customer\Item\Group\Iface or Aimeos\MShop\Stock\Item\Iface or Aimeos\MShop\Order\Item\Base\Coupon\Iface or Aimeos\MShop\Order\Item\Iface or Aimeos\MShop\Subscription\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Common\Item\ListRef\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Order\Item\Status\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\PropertyRef\Iface or Aimeos\MShop\Order\Item\Base\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Order\Item\...Service\Attribute\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Common\Item\Address\Iface or Aimeos\MShop\Common\Item\Property\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Order\Item\...Product\Attribute\Iface or Aimeos\MShop\Common\Item\AddressRef\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Order\Item\Base\Product\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Common\Item\Tree\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Base\Base or Aimeos\MShop\Order\Item\Base\Standard or Aimeos\MShop\Customer\Item\Address\Iface or Aimeos\MShop\Order\Item\Base\Address\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\Item\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

79
						$items[] = $manager->createItem()->/** @scrutinizer ignore-call */ setDomain( $domain )->setCode( $code )->setLabel( $code );
Loading history...
80
					}
81
82
					$manager->saveItems( $items, false );
83
					$manager->commit();
84
				}
85
				catch( \Exception $e )
86
				{
87
					$manager->rollback();
88
					$this->context->getLogger()->log( 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
89
				}
90
			}
91
		}
92
	}
93
94
95
	/**
96
	 * Registers a used type which is going to be saved if it doesn't exist yet
97
	 *
98
	 * @param string $path Manager path, e.g. "product/lists/type"
99
	 * @param string $domain Domain name the type belongs to, e.g. "attribute"
100
	 * @param string $code Type code
101
	 */
102
	protected function addType( string $path, string $domain, string $code )
103
	{
104
		$this->types[$path][$domain][$code] = $code;
105
	}
106
107
108
	/**
109
	 * Adds the list item default values and returns the resulting array
110
	 *
111
	 * @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1
112
	 * @param integer $pos Computed position of the list item in the associated list of items
113
	 * @return array Given associative list enriched by default values if they were not already set
114
	 */
115
	protected function addListItemDefaults( array $list, $pos )
116
	{
117
		if( !isset( $list['product.lists.position'] ) ) {
118
			$list['product.lists.position'] = $pos;
119
		}
120
121
		if( !isset( $list['product.lists.status'] ) ) {
122
			$list['product.lists.status'] = 1;
123
		}
124
125
		return $list;
126
	}
127
128
129
	/**
130
	 * Returns the context item
131
	 *
132
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
133
	 */
134
	protected function getContext()
135
	{
136
		return $this->context;
137
	}
138
139
140
	/**
141
	 * Returns the mapping list
142
	 *
143
	 * @return array Associative list of field positions in CSV as keys and domain item keys as values
144
	 */
145
	protected function getMapping()
146
	{
147
		return $this->mapping;
148
	}
149
150
151
	/**
152
	 * Returns the decorated processor object
153
	 *
154
	 * @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Processor object
155
	 * @throws \Aimeos\Controller\Jobs\Exception If no processor object is available
156
	 */
157
	protected function getObject()
158
	{
159
		if( $this->object === null ) {
160
			throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' );
161
		}
162
163
		return $this->object;
164
	}
165
}
166