Completed
Branch master (c3c77b)
by Aimeos
03:13
created

Standard::checkEntry()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 1
dl 0
loc 19
rs 9.2222
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\Media;
12
13
14
/**
15
 * Media processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
23
{
24
	/** controller/common/product/import/csv/processor/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Media\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2015.10
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
	private $types = [];
37
38
39
	/**
40
	 * Initializes the object
41
	 *
42
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
43
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
44
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
45
	 */
46
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
47
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
48
	{
49
		parent::__construct( $context, $mapping, $object );
50
51
		/** controller/common/product/import/csv/processor/media/listtypes
52
		 * Names of the product list types for media that are updated or removed
53
		 *
54
		 * If you want to associate media items manually via the administration
55
		 * interface to products and don't want these to be touched during the
56
		 * import, you can specify the product list types for these media
57
		 * that shouldn't be updated or removed.
58
		 *
59
		 * @param array|null List of product list type names or null for all
60
		 * @since 2015.05
61
		 * @category Developer
62
		 * @category User
63
		 * @see controller/common/product/import/csv/domains
64
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
65
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/product/import/csv/processor/product/listtypes
67
		 * @see controller/common/product/import/csv/processor/price/listtypes
68
		 * @see controller/common/product/import/csv/processor/text/listtypes
69
		 */
70
		$key = 'controller/common/product/import/csv/processor/media/listtypes';
71
		$this->listTypes = $context->getConfig()->get( $key );
72
73
		if( $this->listTypes === null )
74
		{
75
			$this->listTypes = [];
76
			$manager = \Aimeos\MShop::create( $context, 'product/lists/type' );
77
78
			$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
79
			$search->setConditions( $search->compare( '==', 'product.lists.type.domain', 'media' ) );
80
81
			foreach( $manager->searchItems( $search ) as $item ) {
82
				$this->listTypes[$item->getCode()] = $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 Aimeos\MShop\Common\Item\Iface such as Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Base\Service\Iface or Aimeos\MShop\Customer\Item\Group\Iface or Aimeos\MShop\Order\Item\Base\Coupon\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Order\Item\...Service\Attribute\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Order\Item\...Product\Attribute\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Attribute\Item\Standard or Aimeos\MShop\Catalog\Item\Standard or Aimeos\MShop\Customer\Item\Base or Aimeos\MShop\Customer\Item\Group\Standard or Aimeos\MShop\Common\Item\Type\Standard or Aimeos\MShop\Locale\Item\Site\Standard or Aimeos\MShop\Locale\Item\Currency\Standard or Aimeos\MShop\Locale\Item\Language\Standard or Aimeos\MShop\Order\Item\Base\Service\Base or Aimeos\MShop\Service\Item\Standard or Aimeos\MShop\Order\Item\Base\Coupon\Standard or Aimeos\MShop\Coupon\Item\Code\Standard or Aimeos\MShop\Order\Item\...vice\Attribute\Standard or Aimeos\MShop\Supplier\Item\Standard or Aimeos\MShop\Product\Item\Standard or Aimeos\MShop\Order\Item\...duct\Attribute\Standard or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Common\Item\Tree\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\Customer\Item\Iface or Aimeos\MShop\Attribute\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\Attribute\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\Item\Iface. ( Ignorable by Annotation )

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

82
				$this->listTypes[$item->/** @scrutinizer ignore-call */ getCode()] = $item->getCode();
Loading history...
83
			}
84
		}
85
		else
86
		{
87
			$this->listTypes = array_flip( $this->listTypes );
88
		}
89
90
91
		$manager = \Aimeos\MShop::create( $context, 'media/type' );
92
93
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
94
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'product' ) );
95
96
		foreach( $manager->searchItems( $search ) as $item ) {
97
			$this->types[$item->getCode()] = $item->getCode();
98
		}
99
	}
100
101
102
	/**
103
	 * Saves the product related data to the storage
104
	 *
105
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
106
	 * @param array $data List of CSV fields with position as key and data as value
107
	 * @return array List of data which hasn't been imported
108
	 */
109
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
110
	{
111
		$context = $this->getContext();
112
		$manager = \Aimeos\MShop::create( $context, 'media' );
113
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
114
		$separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" );
115
116
		$listMap = [];
117
		$map = $this->getMappedChunk( $data, $this->getMapping() );
118
		$listItems = $product->getListItems( 'media', $this->listTypes );
119
120
		foreach( $listItems as $listItem )
121
		{
122
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
123
				$listMap[ $refItem->getUrl() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
124
			}
125
		}
126
127
		foreach( $map as $pos => $list )
128
		{
129
			if( $this->checkEntry( $list ) === false ) {
130
				continue;
131
			}
132
133
			$type = $this->getValue( $list, 'media.type', 'default' );
134
			$listtype = $this->getValue( $list, 'product.lists.type', 'default' );
135
			$urls = explode( $separator, $this->getValue( $list, 'media.url', '' ) );
136
137
			foreach( $urls as $url )
138
			{
139
				$url = trim( $url );
140
141
				if( isset( $listMap[$url][$type][$listtype] ) )
142
				{
143
					$listItem = $listMap[$url][$type][$listtype];
144
					$refItem = $listItem->getRefItem();
145
					unset( $listItems[ $listItem->getId() ] );
146
				}
147
				else
148
				{
149
					$listItem = $listManager->createItem()->setType( $listtype );
150
					$refItem = $manager->createItem()->setType( $type );
151
				}
152
153
				$listItem = $listItem->setPosition( $pos++ )->fromArray( $list );
154
				$refItem = $refItem->setLabel( $url )->setPreview( $url )->fromArray( $list )->setUrl( $url );
155
156
				$product->addListItem( 'media', $listItem, $refItem );
157
			}
158
		}
159
160
		$product->deleteListItems( $listItems, true );
161
162
		return $this->getObject()->process( $product, $data );
163
	}
164
165
166
	/**
167
	 * Checks if an entry can be used for updating a media item
168
	 *
169
	 * @param array $list Associative list of key/value pairs from the mapping
170
	 * @return boolean True if valid, false if not
171
	 */
172
	protected function checkEntry( array $list )
173
	{
174
		if( $this->getValue( $list, 'media.url' ) === null ) {
175
			return false;
176
		}
177
178
		if( ( $type = $this->getValue( $list, 'product.lists.type' ) ) && !isset( $this->listTypes[$type] ) )
179
		{
180
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'product list' );
181
			throw new \Aimeos\Controller\Common\Exception( $msg );
182
		}
183
184
		if( ( $type = $this->getValue( $list, 'media.type' ) ) && !isset( $this->types[$type] ) )
185
		{
186
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'media' );
187
			throw new \Aimeos\Controller\Common\Exception( $msg );
188
		}
189
190
		return true;
191
	}
192
}
193