Standard::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 46
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 46
rs 9.7666
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\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\Jobs\Common\Catalog\Import\Csv\Processor\Base
22
	implements \Aimeos\Controller\Jobs\Common\Catalog\Import\Csv\Processor\Iface
23
{
24
	/** controller/jobs/catalog/import/csv/processor/text/name
25
	 * Name of the text processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Jobs\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
	 */
33
34
	private ?array $listTypes = null;
35
	private array $types = [];
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\ContextIface $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\Jobs\Common\Catalog\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping,
46
			?\Aimeos\Controller\Jobs\Common\Catalog\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/jobs/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
		 * @see controller/jobs/catalog/import/csv/domains
61
		 * @see controller/jobs/catalog/import/csv/processor/attribute/listtypes
62
		 * @see controller/jobs/catalog/import/csv/processor/catalog/listtypes
63
		 * @see controller/jobs/catalog/import/csv/processor/media/listtypes
64
		 * @see controller/jobs/catalog/import/csv/processor/price/listtypes
65
		 * @see controller/jobs/catalog/import/csv/processor/catalog/listtypes
66
		 */
67
		$key = 'controller/jobs/catalog/import/csv/processor/text/listtypes';
68
		$this->listTypes = $context->config()->get( $key );
69
70
		if( $this->listTypes === null )
71
		{
72
			$this->listTypes = [];
73
			$manager = \Aimeos\MShop::create( $context, 'catalog/lists/type' );
74
			$search = $manager->filter()->slice( 0, 0x7fffffff );
75
76
			foreach( $manager->search( $search ) as $item ) {
77
				$this->listTypes[$item->getCode()] = $item->getCode();
78
			}
79
		}
80
		else
81
		{
82
			$this->listTypes = array_combine( $this->listTypes, $this->listTypes );
83
		}
84
85
86
		$manager = \Aimeos\MShop::create( $context, 'text/type' );
87
		$search = $manager->filter()->slice( 0, 0x7fffffff );
88
89
		foreach( $manager->search( $search ) as $item ) {
90
			$this->types[$item->getCode()] = $item->getCode();
91
		}
92
	}
93
94
95
	/**
96
	 * Saves the catalog related data to the storage
97
	 *
98
	 * @param \Aimeos\MShop\Catalog\Item\Iface $catalog Catalog item with associated items
99
	 * @param array $data List of CSV fields with position as key and data as value
100
	 * @return array List of data which hasn't been imported
101
	 */
102
	public function process( \Aimeos\MShop\Catalog\Item\Iface $catalog, array $data ) : array
103
	{
104
		$context = $this->context();
105
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
106
		$refManager = \Aimeos\MShop::create( $context, 'text' );
107
108
		$listMap = [];
109
		$map = $this->getMappedChunk( $data, $this->getMapping() );
110
		$listItems = $catalog->getListItems( 'text', $this->listTypes );
111
112
		foreach( $listItems as $listItem )
113
		{
114
			if( ( $refItem = $listItem->getRefItem() ) !== null ) {
115
				$listMap[$refItem->getContent()][$refItem->getType()][$listItem->getType()] = $listItem;
116
			}
117
		}
118
119
		foreach( $map as $pos => $list )
120
		{
121
			if( $this->checkEntry( $list ) === false ) {
122
				continue;
123
			}
124
125
			$type = trim( $this->val( $list, 'text.type', 'name' ) );
126
			$content = trim( $this->val( $list, 'text.content', '' ) );
127
128
			$listtype = trim( $this->val( $list, 'catalog.lists.type', 'default' ) );
129
			$listConfig = $this->getListConfig( trim( $this->val( $list, 'catalog.lists.config', '' ) ) );
130
131
			$this->addType( 'catalog/lists/type', 'text', $listtype );
132
			$this->addType( 'text/type', 'product', $type );
133
134
			if( isset( $listMap[$content][$type][$listtype] ) )
135
			{
136
				$listItem = $listMap[$content][$type][$listtype];
137
				$refItem = $listItem->getRefItem();
138
				unset( $listItems[$listItem->getId()] );
139
			}
140
			else
141
			{
142
				$listItem = $manager->createListItem()->setType( $listtype );
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

142
				$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->setType( $listtype );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
				$refItem = $refManager->create()->setType( $type );
144
			}
145
146
			$listItem = $listItem->setPosition( $pos )->fromArray( $list )->setConfig( $listConfig );
147
148
			$label = mb_strcut( strip_tags( $this->val( $list, 'text.content', '' ) ), 0, 255 );
149
			$refItem = $refItem->setLabel( $label )->fromArray( $list );
150
151
			$catalog->addListItem( 'text', $listItem, $refItem );
152
		}
153
154
		$catalog->deleteListItems( $listItems->toArray(), true );
155
156
		return $this->object()->process( $catalog, $data );
157
	}
158
159
160
	/**
161
	 * Checks if an entry can be used for updating a media item
162
	 *
163
	 * @param array $list Associative list of key/value pairs from the mapping
164
	 * @return bool True if valid, false if not
165
	 */
166
	protected function checkEntry( array $list ) : bool
167
	{
168
		if( $this->val( $list, 'text.content' ) === null ) {
169
			return false;
170
		}
171
172
		if( ( $type = trim( $this->val( $list, 'catalog.lists.type', 'default' ) ) ) && !isset( $this->listTypes[$type] ) )
173
		{
174
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'catalog list' );
175
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
176
		}
177
178
		if( ( $type = trim( $this->val( $list, 'text.type', 'name' ) ) ) && !isset( $this->types[$type] ) )
179
		{
180
			$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'text' );
181
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
182
		}
183
184
		return true;
185
	}
186
}
187