Completed
Push — master ( 2e10c1...bb3077 )
by Aimeos
02:17
created

Standard::addItemDefaults()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Catalog\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\Catalog\Import\Csv\Processor\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/** controller/common/catalog/import/csv/processor/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Catalog\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 2018.04
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
43
	 * @param \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
			\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/common/catalog/import/csv/processor/media/listtypes
51
		 * Names of the catalog list types for media that are updated or removed
52
		 *
53
		 * If you want to associate media items manually via the administration
54
		 * interface to catalogs and don't want these to be touched during the
55
		 * import, you can specify the catalog list types for these media
56
		 * that shouldn't be updated or removed.
57
		 *
58
		 * @param array|null List of catalog list type names or null for all
59
		 * @since 2018.04
60
		 * @category Developer
61
		 * @category User
62
		 * @see controller/common/catalog/import/csv/domains
63
		 * @see controller/common/catalog/import/csv/processor/attribute/listtypes
64
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
65
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
66
		 * @see controller/common/catalog/import/csv/processor/price/listtypes
67
		 * @see controller/common/catalog/import/csv/processor/text/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/catalog/import/csv/processor/media/listtypes' );
70
	}
71
72
73
	/**
74
	 * Saves the catalog related data to the storage
75
	 *
76
	 * @param \Aimeos\MShop\Catalog\Item\Iface $catalog Catalog item with associated items
77
	 * @param array $data List of CSV fields with position as key and data as value
78
	 * @return array List of data which hasn't been imported
79
	 */
80
	public function process( \Aimeos\MShop\Catalog\Item\Iface $catalog, array $data )
81
	{
82
		$context = $this->getContext();
83
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
84
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'catalog/lists' );
85
		$separator = $context->getConfig()->get( 'controller/common/catalog/import/csv/separator', "\n" );
86
87
		$manager->begin();
88
89
		try
90
		{
91
			$delete = $listMap = [];
92
			$map = $this->getMappedChunk( $data, $this->getMapping() );
93
			$listItems = $catalog->getListItems( 'media', $this->listTypes );
94
95
			foreach( $listItems as $listItem )
96
			{
97
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
98
					$listMap[ $refItem->getUrl() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
99
				}
100
			}
101
102
			foreach( $map as $pos => $list )
103
			{
104
				if( $this->checkEntry( $list ) === false ) {
105
					continue;
106
				}
107
108
				$urls = explode( $separator, $list['media.url'] );
109
				$type = $this->getValue( $list, 'media.type', 'default' );
110
				$typecode = $this->getValue( $list, 'catalog.lists.type', 'default' );
111
112
				foreach( $urls as $url )
113
				{
114
					if( isset( $listMap[$url][$type][$typecode] ) )
115
					{
116
						$listItem = $listMap[$url][$type][$typecode];
117
						$refItem = $listItem->getRefItem();
118
						unset( $listItems[ $listItem->getId() ] );
119
					}
120
					else
121
					{
122
						$listItem = $listManager->createItem();
123
						$refItem = $manager->createItem();
124
					}
125
126
					$list['media.typeid'] = $this->getTypeId( 'media/type', 'catalog', $type );
127
					$list['media.domain'] = 'catalog';
128
					$list['media.url'] = $url;
129
130
					$refItem->fromArray( $this->addItemDefaults( $list ) );
131
					$refItem = $manager->saveItem( $refItem );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $refItem is correct as $manager->saveItem($refItem) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
132
133
					$list['catalog.lists.typeid'] = $this->getTypeId( 'catalog/lists/type', 'media', $typecode );
134
					$list['catalog.lists.parentid'] = $catalog->getId();
135
					$list['catalog.lists.refid'] = $refItem->getId();
0 ignored issues
show
Bug introduced by
The method getId cannot be called on $refItem (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
136
					$list['catalog.lists.domain'] = 'media';
137
138
					$listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
139
					$listManager->saveItem( $listItem, false );
140
				}
141
			}
142
143
			foreach( $listItems as $listItem ) {
144
				$delete[] = $listItem->getRefId();
145
			}
146
147
			$manager->deleteItems( $delete );
148
			$listManager->deleteItems( array_keys( $listItems ) );
149
150
			$data = $this->getObject()->process( $catalog, $data );
151
152
			$manager->commit();
153
		}
154
		catch( \Exception $e )
155
		{
156
			$manager->rollback();
157
			throw $e;
158
		}
159
160
		return $data;
161
	}
162
163
164
	/**
165
	 * Adds the text item default values and returns the resulting array
166
	 *
167
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
168
	 * @return array Given associative list enriched by default values if they were not already set
169
	 */
170
	protected function addItemDefaults( array $list )
171
	{
172
		if( !isset( $list['media.label'] ) ) {
173
			$list['media.label'] = $list['media.url'];
174
		}
175
176
		if( !isset( $list['media.preview'] ) ) {
177
			$list['media.preview'] = $list['media.url'];
178
		}
179
180
		if( !isset( $list['media.status'] ) ) {
181
			$list['media.status'] = 1;
182
		}
183
184
		return $list;
185
	}
186
187
188
	/**
189
	 * Checks if an entry can be used for updating a media item
190
	 *
191
	 * @param array $list Associative list of key/value pairs from the mapping
192
	 * @return boolean True if valid, false if not
193
	 */
194
	protected function checkEntry( array $list )
195
	{
196
		if( !isset( $list['media.url'] ) || $list['media.url'] === '' || isset( $list['catalog.lists.type'] )
197
				&& $this->listTypes !== null && !in_array( $list['catalog.lists.type'], (array) $this->listTypes )
198
		) {
199
			return false;
200
		}
201
202
		return true;
203
	}
204
}
205