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

Standard::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Text;
12
13
14
/**
15
 * Text 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/text/name
25
	 * Name of the text processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Text\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/text/listtypes
51
		 * Names of the catalog list types for texts that are updated or removed
52
		 *
53
		 * If you want to associate text 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 texts
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/media/listtypes
66
		 * @see controller/common/catalog/import/csv/processor/price/listtypes
67
		 * @see controller/common/catalog/import/csv/processor/catalog/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/catalog/import/csv/processor/text/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
		$listManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'catalog/lists' );
83
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'text' );
84
		$manager->begin();
85
86
		try
87
		{
88
			$delete = $listMap = [];
89
			$map = $this->getMappedChunk( $data, $this->getMapping() );
90
			$listItems = $catalog->getListItems( 'text', $this->listTypes );
91
92
			foreach( $listItems as $listItem )
93
			{
94
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
95
					$listMap[ $refItem->getContent() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
96
				}
97
			}
98
99
			foreach( $map as $pos => $list )
100
			{
101
				if( $this->checkEntry( $list ) === false ) {
102
					continue;
103
				}
104
105
				$type = ( isset( $list['text.type'] ) ? $list['text.type'] : 'name' );
106
				$typecode = ( isset( $list['catalog.lists.type'] ) ? $list['catalog.lists.type'] : 'default' );
107
108
				if( isset( $listMap[ $list['text.content'] ][$type][$typecode] ) )
109
				{
110
					$listItem = $listMap[ $list['text.content'] ][$type][$typecode];
111
					$refItem = $listItem->getRefItem();
112
					unset( $listItems[ $listItem->getId() ] );
113
				}
114
				else
115
				{
116
					$listItem = $listManager->createItem();
117
					$refItem = $manager->createItem();
118
				}
119
120
				$list['text.typeid'] = $this->getTypeId( 'text/type', 'catalog', $type );
121
				$list['text.domain'] = 'catalog';
122
123
				$refItem->fromArray( $this->addItemDefaults( $list ) );
124
				$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...
125
126
				$list['catalog.lists.typeid'] = $this->getTypeId( 'catalog/lists/type', 'text', $typecode );
127
				$list['catalog.lists.parentid'] = $catalog->getId();
128
				$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...
129
				$list['catalog.lists.domain'] = 'text';
130
131
				$listItem->fromArray( $this->addListItemDefaults( $list, $pos ) );
132
				$listManager->saveItem( $listItem, false );
133
			}
134
135
			foreach( $listItems as $listItem ) {
136
				$delete[] = $listItem->getRefId();
137
			}
138
139
			$manager->deleteItems( $delete );
140
			$listManager->deleteItems( array_keys( $listItems ) );
141
142
			$data = $this->getObject()->process( $catalog, $data );
143
144
			$manager->commit();
145
		}
146
		catch( \Exception $e )
147
		{
148
			$manager->rollback();
149
			throw $e;
150
		}
151
152
		return $data;
153
	}
154
155
156
	/**
157
	 * Adds the text item default values and returns the resulting array
158
	 *
159
	 * @param array $list Associative list of domain item keys and their values, e.g. "text.status" => 1
160
	 * @return array Given associative list enriched by default values if they were not already set
161
	 */
162
	protected function addItemDefaults( array $list )
163
	{
164
		if( !isset( $list['text.label'] ) ) {
165
			$list['text.label'] = mb_strcut( $list['text.content'], 0, 255 );
166
		}
167
168
		if( !isset( $list['text.languageid'] ) ) {
169
			$list['text.languageid'] = $this->getContext()->getLocale()->getLanguageId();
170
		}
171
172
		if( !isset( $list['text.status'] ) ) {
173
			$list['text.status'] = 1;
174
		}
175
176
		return $list;
177
	}
178
179
180
	/**
181
	 * Checks if an entry can be used for updating a media item
182
	 *
183
	 * @param array $list Associative list of key/value pairs from the mapping
184
	 * @return boolean True if valid, false if not
185
	 */
186
	protected function checkEntry( array $list )
187
	{
188
		if( !isset( $list['text.content'] ) || $list['text.content'] === '' || isset( $list['catalog.lists.type'] )
189
			&& $this->listTypes !== null && !in_array( $list['catalog.lists.type'], (array) $this->listTypes )
190
		) {
191
			return false;
192
		}
193
194
		return true;
195
	}
196
}
197