Completed
Push — master ( 0ed5fa...41d654 )
by Aimeos
02:01
created

Standard::process()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 7.8901
c 0
b 0
f 0
cc 8
nc 12
nop 2
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
22
	implements \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Iface
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
85
		$delete = $listMap = [];
0 ignored issues
show
Unused Code introduced by
$delete is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
		$map = $this->getMappedChunk( $data, $this->getMapping() );
87
		$listItems = $catalog->getListItems( 'text', $this->listTypes );
88
89
		foreach( $listItems as $listItem )
90
		{
91
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
92
				$listMap[ $refItem->getContent() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
93
			}
94
		}
95
96
		foreach( $map as $pos => $list )
97
		{
98
			if( $this->checkEntry( $list ) === false ) {
99
				continue;
100
			}
101
102
			$content = trim( $list['text.content'] );
103
			$type = trim( isset( $list['text.type'] ) ? $list['text.type'] : 'name' );
104
			$typecode = trim( isset( $list['catalog.lists.type'] ) ? $list['catalog.lists.type'] : 'default' );
105
106
			if( isset( $listMap[$content][$type][$typecode] ) )
107
			{
108
				$listItem = $listMap[$content][$type][$typecode];
109
				$refItem = $listItem->getRefItem();
110
				unset( $listItems[ $listItem->getId() ] );
111
			}
112
			else
113
			{
114
				$listItem = $listManager->createItem( $typecode, 'text' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $typecode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
				$refItem = $manager->createItem( $type, 'catalog' );
0 ignored issues
show
Unused Code introduced by
The call to Iface::createItem() has too many arguments starting with $type.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
			}
117
118
			$refItem->fromArray( $this->addItemDefaults( $list ) );
119
			$listItem->fromArray( $this->addListItemDefaults( $list, $pos ) );
120
121
			$catalog->addListItem( 'text', $listItem, $refItem );
122
		}
123
124
		$catalog->deleteListItems( $listItems, true );
125
126
		return $this->getObject()->process( $catalog, $data );
127
	}
128
129
130
	/**
131
	 * Adds the text item default values and returns the resulting array
132
	 *
133
	 * @param array $list Associative list of domain item keys and their values, e.g. "text.status" => 1
134
	 * @return array Given associative list enriched by default values if they were not already set
135
	 */
136
	protected function addItemDefaults( array $list )
137
	{
138
		if( !isset( $list['text.label'] ) ) {
139
			$list['text.label'] = mb_strcut( trim( $list['text.content'] ), 0, 255 );
140
		}
141
142
		if( !isset( $list['text.status'] ) ) {
143
			$list['text.status'] = 1;
144
		}
145
146
		return $list;
147
	}
148
149
150
	/**
151
	 * Checks if an entry can be used for updating a media item
152
	 *
153
	 * @param array $list Associative list of key/value pairs from the mapping
154
	 * @return boolean True if valid, false if not
155
	 */
156
	protected function checkEntry( array $list )
157
	{
158
		if( !isset( $list['text.content'] ) || trim( $list['text.content'] ) === '' || isset( $list['catalog.lists.type'] )
159
			&& $this->listTypes !== null && !in_array( trim( $list['catalog.lists.type'] ), (array) $this->listTypes )
160
		) {
161
			return false;
162
		}
163
164
		return true;
165
	}
166
}
167