Passed
Push — master ( c40152...87de23 )
by Aimeos
06:15
created

Standard::import()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 111
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 29
nc 10
nop 1
dl 0
loc 111
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Product\Import\Xml;
12
13
14
/**
15
 * Job controller for XML product imports
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
25
26
27
	/**
28
	 * Returns the localized name of the job.
29
	 *
30
	 * @return string Name of the job
31
	 */
32
	public function getName()
33
	{
34
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Product import XML' );
35
	}
36
37
38
	/**
39
	 * Returns the localized description of the job.
40
	 *
41
	 * @return string Description of the job
42
	 */
43
	public function getDescription()
44
	{
45
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Imports new and updates existing products from XML files' );
46
	}
47
48
49
	/**
50
	 * Executes the job.
51
	 *
52
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
53
	 */
54
	public function run()
55
	{
56
		$context = $this->getContext();
57
		$config = $context->getConfig();
58
		$logger = $context->getLogger();
59
60
		/** controller/jobs/product/import/xml/location
61
		 * File or directory where the content is stored which should be imported
62
		 *
63
		 * You need to configure the XML file or directory with the XML files that
64
		 * should be imported. It should be an absolute path to be sure but can be
65
		 * relative path if you absolutely know from where the job will be executed
66
		 * from.
67
		 *
68
		 * @param string Absolute file or directory path
69
		 * @since 2019.04
70
		 * @category Developer
71
		 * @category User
72
		 * @see controller/jobs/product/import/xml/container/type
73
		 * @see controller/jobs/product/import/xml/container/content
74
		 * @see controller/jobs/product/import/xml/container/options
75
		 */
76
		$location = $config->get( 'controller/jobs/product/import/xml/location' );
77
78
		try
79
		{
80
			$msg = sprintf( 'Started product import from "%1$s" (%2$s)', $location, __CLASS__ );
81
			$logger->log( $msg, \Aimeos\MW\Logger\Base::INFO );
82
83
			if( !file_exists( $location ) )
84
			{
85
				$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location );
86
				throw new \Aimeos\Controller\Jobs\Exception( $msg );
87
			}
88
89
			$files = [];
90
91
			if( is_dir( $location ) )
92
			{
93
				foreach( new \DirectoryIterator( $location ) as $entry )
94
				{
95
					if( strncmp( $entry->getFilename(), 'product', 7 ) === 0 && $entry->getExtension() === 'xml' ) {
96
						$files[] = $entry->getPathname();
97
					}
98
				}
99
			}
100
			else
101
			{
102
				$files[] = $location;
103
			}
104
105
			sort( $files );
106
			$total = 0;
107
108
			foreach( $files as $filepath ) {
109
				$total += $this->import( $filepath );
110
			}
111
112
			$msg = 'Finished product import from "%1$s": %2$s total (%3$s MB)';
113
			$mem = number_format( memory_get_peak_usage() / 1024 / 1024, 2 );
114
115
			$logger->log( sprintf( $msg, $location, $total, $mem ), \Aimeos\MW\Logger\Base::INFO );
116
		}
117
		catch( \Exception $e )
118
		{
119
			$logger->log( 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString() );
120
			throw $e;
121
		}
122
	}
123
124
125
	/**
126
	 * Imports the XML file given by its path
127
	 *
128
	 * @param string $filename Absolute or relative path to the XML file
129
	 * @return integer Total number of imported products
130
	 */
131
	protected function import( $filename )
132
	{
133
		$context = $this->getContext();
134
		$config = $context->getConfig();
135
		$domains = ['attribute', 'media', 'price', 'product', 'product/property', 'text'];
136
137
		/** controller/jobs/product/import/xml/domains
138
		 * List of item domain names that should be retrieved along with the product items
139
		 *
140
		 * This configuration setting overwrites the shared option
141
		 * "controller/common/product/import/xml/domains" if you need a
142
		 * specific setting for the job controller. Otherwise, you should
143
		 * use the shared option for consistency.
144
		 *
145
		 * @param array Associative list of MShop item domain names
146
		 * @since 2019.04
147
		 * @category Developer
148
		 * @see controller/jobs/product/import/xml/backup
149
		 * @see controller/jobs/product/import/xml/max-query
150
		 */
151
		$domains = $config->get( 'controller/jobs/product/import/xml/domains', $domains );
152
153
		/** controller/jobs/product/import/xml/backup
154
		 * Name of the backup for sucessfully imported files
155
		 *
156
		 * After a XML file was imported successfully, you can move it to another
157
		 * location, so it won't be imported again and isn't overwritten by the
158
		 * next file that is stored at the same location in the file system.
159
		 *
160
		 * You should use an absolute path to be sure but can be relative path
161
		 * if you absolutely know from where the job will be executed from. The
162
		 * name of the new backup location can contain placeholders understood
163
		 * by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d"
164
		 * which would create "backup/2000-01-01". For more information about the
165
		 * strftime() placeholders, please have a look into the PHP documentation of
166
		 * the {@link http://php.net/manual/en/function.strftime.php strftime() function}.
167
		 *
168
		 * '''Note:''' If no backup name is configured, the file or directory
169
		 * won't be moved away. Please make also sure that the parent directory
170
		 * and the new directory are writable so the file or directory could be
171
		 * moved.
172
		 *
173
		 * @param integer Name of the backup file, optionally with date/time placeholders
174
		 * @since 2019.04
175
		 * @category Developer
176
		 * @see controller/jobs/product/import/xml/domains
177
		 * @see controller/jobs/product/import/xml/max-query
178
		 */
179
		$backup = $config->get( 'controller/jobs/product/import/xml/backup' );
180
181
		/** controller/jobs/product/import/xml/max-query
182
		 * Maximum number of XML nodes processed at once
183
		 *
184
		 * Processing and fetching several product items at once speeds up importing
185
		 * the XML files. The more items can be processed at once, the faster the
186
		 * import. More items also increases the memory usage of the importer and
187
		 * thus, this parameter should be low enough to avoid reaching the memory
188
		 * limit of the PHP process.
189
		 *
190
		 * @param integer Number of XML nodes
191
		 * @since 2019.04
192
		 * @category Developer
193
		 * @category User
194
		 * @see controller/jobs/product/import/xml/domains
195
		 * @see controller/jobs/product/import/xml/backup
196
		 */
197
		$maxquery = $config->get( 'controller/jobs/product/import/xml/max-query', 1000 );
198
199
200
		$nodes = [];
201
		$total = $slice = 0;
202
		$xml = new \XMLReader();
203
204
		if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) {
205
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) );
206
		}
207
208
		while( $xml->read() === true )
209
		{
210
			if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'productitem' )
211
			{
212
				if( ( $dom = $xml->expand() ) === false )
213
				{
214
					$msg = sprintf( 'Expanding "%1$s" node failed', 'productitem' );
215
					throw new \Aimeos\Controller\Jobs\Exception( $msg );
216
				}
217
218
				$nodes[] = $dom;
219
220
				if( $slice++ >= $maxquery )
221
				{
222
					$this->importNodes( $nodes, $domains );
223
					unset( $nodes );
224
					$nodes = [];
225
					$slice = 0;
226
				}
227
228
				$total++;
229
			}
230
		}
231
232
		$this->importNodes( $nodes, $domains );
233
		unset( $nodes );
234
235
		if( !empty( $backup ) && @rename( $filename, strftime( $backup ) ) === false )
236
		{
237
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
238
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
239
		}
240
241
		return $total;
242
	}
243
244
245
	/**
246
	 * Imports the given DOM nodes
247
	 *
248
	 * @param \DomElement[] $nodes List of nodes to import
249
	 * @param string[] $ref List of domain names whose referenced items will be updated in the product items
250
	 */
251
	protected function importNodes( array $nodes, array $ref )
252
	{
253
		$codes = $map = [];
254
255
		foreach( $nodes as $node )
256
		{
257
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
258
				$codes[$attr->nodeValue] = null;
259
			}
260
		}
261
262
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
263
		$search = $manager->createSearch()->setSlice( 0, count( $codes ) );
264
		$search->setConditions( $search->compare( '==', 'product.code', array_keys( $codes ) ) );
265
266
		foreach( $manager->searchItems( $search, $ref ) as $item ) {
267
			$map[$item->getCode()] = $item;
268
		}
269
270
		foreach( $nodes as $node )
271
		{
272
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[$attr->nodeValue] ) ) {
273
				$item = $this->process( $map[$attr->nodeValue], $node );
274
			} else {
275
				$item = $this->process( $manager->createItem(), $node );
0 ignored issues
show
Bug introduced by
$manager->createItem() of type Aimeos\MShop\Attribute\Item\Iface is incompatible with the type Aimeos\MShop\Product\Item\Iface expected by parameter $item of Aimeos\Controller\Jobs\P...Xml\Standard::process(). ( Ignorable by Annotation )

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

275
				$item = $this->process( /** @scrutinizer ignore-type */ $manager->createItem(), $node );
Loading history...
276
			}
277
278
			$manager->saveItem( $item );
279
		}
280
	}
281
282
283
	/**
284
	 * Updates the product item and its referenced items using the given DOM node
285
	 *
286
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object to update
287
	 * @param \DomElement $node DOM node used for updateding the product item
288
	 * @return \Aimeos\MShop\Product\Item\Iface $item Updated product item object
289
	 */
290
	protected function process( \Aimeos\MShop\Product\Item\Iface $item, \DomElement $node )
291
	{
292
		$list = [];
293
294
		foreach( $node->attributes as $attr ) {
295
			$list[$attr->nodeName] = $attr->nodeValue;
296
		}
297
298
		foreach( $node->childNodes as $tag )
299
		{
300
			if( in_array( $tag->nodeName, ['lists', 'property'] ) ) {
301
				$item = $this->getProcessor( $tag->nodeName )->process( $item, $tag );
302
			} else {
303
				$list[$tag->nodeName] = $tag->nodeValue;
304
			}
305
		}
306
307
		return $item->fromArray( $list, true );
308
	}
309
}
310