Passed
Push — master ( 3dda3e...b89f32 )
by Aimeos
03:49
created

Standard::location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2022
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Customer\Import\Xml;
12
13
14
/**
15
 * Job controller for XML customer 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
	/** controller/jobs/customer/import/xml/name
25
	 * Class name of the used customer suggestions scheduler controller implementation
26
	 *
27
	 * Each default job controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Jobs\Customer\Import\Xml\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Jobs\Customer\Import\Xml\Myxml
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/customer/import/xml/name = Myxml
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyXml"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2019.04
55
	 */
56
57
	/** controller/jobs/customer/import/xml/decorators/excludes
58
	 * Excludes decorators added by the "common" option from the customer import CSV job controller
59
	 *
60
	 * Decorators extend the functionality of a class by adding new aspects
61
	 * (e.g. log what is currently done), executing the methods of the underlying
62
	 * class only in certain conditions (e.g. only for logged in users) or
63
	 * modify what is returned to the caller.
64
	 *
65
	 * This option allows you to remove a decorator added via
66
	 * "controller/jobs/common/decorators/default" before they are wrapped
67
	 * around the job controller.
68
	 *
69
	 *  controller/jobs/customer/import/xml/decorators/excludes = array( 'decorator1' )
70
	 *
71
	 * This would remove the decorator named "decorator1" from the list of
72
	 * common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via
73
	 * "controller/jobs/common/decorators/default" to the job controller.
74
	 *
75
	 * @param array List of decorator names
76
	 * @since 2019.04
77
	 * @see controller/jobs/common/decorators/default
78
	 * @see controller/jobs/customer/import/xml/decorators/global
79
	 * @see controller/jobs/customer/import/xml/decorators/local
80
	 */
81
82
	/** controller/jobs/customer/import/xml/decorators/global
83
	 * Adds a list of globally available decorators only to the customer import CSV job controller
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller.
92
	 *
93
	 *  controller/jobs/customer/import/xml/decorators/global = array( 'decorator1' )
94
	 *
95
	 * This would add the decorator named "decorator1" defined by
96
	 * "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller.
97
	 *
98
	 * @param array List of decorator names
99
	 * @since 2019.04
100
	 * @see controller/jobs/common/decorators/default
101
	 * @see controller/jobs/customer/import/xml/decorators/excludes
102
	 * @see controller/jobs/customer/import/xml/decorators/local
103
	 */
104
105
	/** controller/jobs/customer/import/xml/decorators/local
106
	 * Adds a list of local decorators only to the customer import CSV job controller
107
	 *
108
	 * Decorators extend the functionality of a class by adding new aspects
109
	 * (e.g. log what is currently done), executing the methods of the underlying
110
	 * class only in certain conditions (e.g. only for logged in users) or
111
	 * modify what is returned to the caller.
112
	 *
113
	 * This option allows you to wrap local decorators
114
	 * ("\Aimeos\Controller\Jobs\Customer\Import\Xml\Decorator\*") around the job
115
	 * controller.
116
	 *
117
	 *  controller/jobs/customer/import/xml/decorators/local = array( 'decorator2' )
118
	 *
119
	 * This would add the decorator named "decorator2" defined by
120
	 * "\Aimeos\Controller\Jobs\Customer\Import\Xml\Decorator\Decorator2"
121
	 * only to the job controller.
122
	 *
123
	 * @param array List of decorator names
124
	 * @since 2019.04
125
	 * @see controller/jobs/common/decorators/default
126
	 * @see controller/jobs/customer/import/xml/decorators/excludes
127
	 * @see controller/jobs/customer/import/xml/decorators/global
128
	 */
129
130
131
	use \Aimeos\Controller\Common\Common\Import\Traits;
132
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
133
134
135
	/**
136
	 * Returns the localized name of the job.
137
	 *
138
	 * @return string Name of the job
139
	 */
140
	public function getName() : string
141
	{
142
		return $this->context()->translate( 'controller/jobs', 'Customer import XML' );
143
	}
144
145
146
	/**
147
	 * Returns the localized description of the job.
148
	 *
149
	 * @return string Description of the job
150
	 */
151
	public function getDescription() : string
152
	{
153
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing customers from XML files' );
154
	}
155
156
157
	/**
158
	 * Executes the job.
159
	 *
160
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
161
	 */
162
	public function run()
163
	{
164
		$context = $this->context();
165
		$logger = $context->logger();
166
		$location = $this->location();
167
168
		try
169
		{
170
			$logger->info( sprintf( 'Started customer import from "%1$s"', $location ), 'import/xml/customer' );
171
172
			if( !file_exists( $location ) )
173
			{
174
				$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location );
175
				throw new \Aimeos\Controller\Jobs\Exception( $msg );
176
			}
177
178
			$files = [];
179
180
			if( is_dir( $location ) )
181
			{
182
				foreach( new \DirectoryIterator( $location ) as $entry )
183
				{
184
					if( strncmp( $entry->getFilename(), 'customer', 8 ) === 0 && $entry->getExtension() === 'xml' ) {
185
						$files[] = $entry->getPathname();
186
					}
187
				}
188
			}
189
			else
190
			{
191
				$files[] = $location;
192
			}
193
194
			sort( $files );
195
			$context->__sleep();
196
197
			$fcn = function( $filepath ) {
198
				$this->import( $filepath );
199
			};
200
201
			foreach( $files as $filepath ) {
202
				$context->process()->start( $fcn, [$filepath] );
203
			}
204
205
			$context->process()->wait();
206
207
			$logger->info( sprintf( 'Finished customer import from "%1$s"', $location ), 'import/xml/customer' );
208
		}
209
		catch( \Exception $e )
210
		{
211
			$logger->error( 'Customer import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/customer' );
212
			$this->mail( 'Customer XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
213
			throw $e;
214
		}
215
	}
216
217
218
	/**
219
	 * Returns the directory for storing imported files
220
	 *
221
	 * @return string Directory for storing imported files
222
	 */
223
	protected function backup() : string
224
	{
225
		/** controller/jobs/customer/import/xml/backup
226
		 * Name of the backup for sucessfully imported files
227
		 *
228
		 * After a XML file was imported successfully, you can move it to another
229
		 * location, so it won't be imported again and isn't overwritten by the
230
		 * next file that is stored at the same location in the file system.
231
		 *
232
		 * You should use an absolute path to be sure but can be relative path
233
		 * if you absolutely know from where the job will be executed from. The
234
		 * name of the new backup location can contain placeholders understood
235
		 * by the PHP DateTime::format() method (with percent signs prefix) to
236
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
237
		 * "backup/2000-01-01". For more information about the date() placeholders,
238
		 * please have a look  into the PHP documentation of the
239
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
240
		 *
241
		 * **Note:** If no backup name is configured, the file or directory
242
		 * won't be moved away. Please make also sure that the parent directory
243
		 * and the new directory are writable so the file or directory could be
244
		 * moved.
245
		 *
246
		 * @param integer Name of the backup file, optionally with date/time placeholders
247
		 * @since 2019.04
248
		 * @see controller/jobs/customer/import/xml/domains
249
		 * @see controller/jobs/customer/import/xml/location
250
		 * @see controller/jobs/customer/import/xml/max-query
251
		 */
252
		return (string) $this->context()->config()->get( 'controller/jobs/customer/import/xml/backup' );
253
	}
254
255
256
	/**
257
	 * Returns the list of domain names that should be retrieved along with the attribute items
258
	 *
259
	 * @return array List of domain names
260
	 */
261
	protected function domains() : array
262
	{
263
		/** controller/jobs/customer/import/xml/domains
264
		 * List of item domain names that should be retrieved along with the attribute items
265
		 *
266
		 * For efficient processing, the items associated to the customers can be
267
		 * fetched to, minimizing the number of database queries required. To be
268
		 * most effective, the list of item domain names should be used in the
269
		 * mapping configuration too, so the retrieved items will be used during
270
		 * the import.
271
		 *
272
		 * @param array Associative list of MShop item domain names
273
		 * @since 2019.04
274
		 * @see controller/jobs/customer/import/xml/backup
275
		 * @see controller/jobs/customer/import/xml/location
276
		 * @see controller/jobs/customer/import/xml/max-query
277
		 */
278
		$domains = ['customer/address', 'customer/property', 'customer/group', 'media', 'product', 'text'];
279
		return $this->context()->config()->get( 'controller/jobs/customer/import/xml/domains', $domains );
280
	}
281
282
283
	/**
284
	 * Imports the XML file given by its path
285
	 *
286
	 * @param string $filename Absolute or relative path to the XML file
287
	 */
288
	protected function import( string $filename )
289
	{
290
		$slice = 0;
291
		$nodes = [];
292
293
		$xml = new \XMLReader();
294
		$maxquery = $this->max();
295
		$logger = $this->context()->logger();
296
297
		if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) {
298
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) );
299
		}
300
301
		$logger->info( sprintf( 'Started customer import from file "%1$s"', $filename ), 'import/xml/customer' );
302
303
		while( $xml->read() === true )
304
		{
305
			if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'customeritem' )
306
			{
307
				if( ( $dom = $xml->expand() ) === false )
308
				{
309
					$msg = sprintf( 'Expanding "%1$s" node failed', 'customeritem' );
310
					throw new \Aimeos\Controller\Jobs\Exception( $msg );
311
				}
312
313
				$nodes[] = $dom;
314
315
				if( $slice++ >= $maxquery )
316
				{
317
					$this->importNodes( $nodes );
318
					unset( $nodes );
319
					$nodes = [];
320
					$slice = 0;
321
				}
322
			}
323
		}
324
325
		$this->importNodes( $nodes );
326
		unset( $nodes );
327
328
		$this->saveTypes();
329
330
		foreach( $this->getProcessors() as $proc ) {
331
			$proc->finish();
332
		}
333
334
		$logger->info( sprintf( 'Finished customer import from file "%1$s"', $filename ), 'import/xml/customer' );
335
336
		if( !empty( $backup = $this->backup() ) && @rename( $filename, $backup = \Aimeos\Base\Str::strtime( $backup ) ) === false )
337
		{
338
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
339
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
340
		}
341
	}
342
343
344
	/**
345
	 * Imports the given DOM nodes
346
	 *
347
	 * @param string[] $ref List of domain names whose referenced items will be updated in the customer items
348
	 */
349
	protected function importNodes( array $nodes )
350
	{
351
		$codes = [];
352
353
		foreach( $nodes as $node )
354
		{
355
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
356
				$codes[$attr->nodeValue] = null;
357
			}
358
		}
359
360
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
361
		$search = $manager->filter()->slice( 0, count( $codes ) )->add( ['customer.code'=> array_keys( $codes )] );
362
		$map = $manager->search( $search, $this->domains() )->col( null, 'customer.code' );
363
364
		foreach( $nodes as $node )
365
		{
366
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[$attr->nodeValue] ) ) {
367
				$item = $this->process( $map[$attr->nodeValue], $node );
0 ignored issues
show
Bug introduced by
It seems like $map[$attr->nodeValue] can also be of type null; however, parameter $item of Aimeos\Controller\Jobs\C...Xml\Standard::process() does only seem to accept Aimeos\MShop\Customer\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

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

367
				$item = $this->process( /** @scrutinizer ignore-type */ $map[$attr->nodeValue], $node );
Loading history...
368
			} else {
369
				$item = $this->process( $manager->create(), $node );
370
			}
371
372
			$manager->save( $item );
373
		}
374
	}
375
376
377
	/**
378
	 * Returns the path to the directory with the XML file
379
	 *
380
	 * @return string Path to the directory with the XML file
381
	 */
382
	protected function location() : string
383
	{
384
		/** controller/jobs/customer/import/xml/location
385
		 * File or directory where the content is stored which should be imported
386
		 *
387
		 * You need to configure the XML file or directory with the XML files that
388
		 * should be imported. It should be an absolute path to be sure but can be
389
		 * relative path if you absolutely know from where the job will be executed
390
		 * from.
391
		 *
392
		 * @param string Relative path to the XML files
393
		 * @since 2019.04
394
		 * @see controller/jobs/customer/import/xml/backup
395
		 * @see controller/jobs/customer/import/xml/domains
396
		 * @see controller/jobs/customer/import/xml/max-query
397
		 */
398
		return (string) $this->context()->config()->get( 'controller/jobs/customer/import/xml/location', 'customer' );
399
	}
400
401
402
	/**
403
	 * Returns the maximum number of XML nodes processed at once
404
	 *
405
	 * @return int Maximum number of XML nodes
406
	 */
407
	protected function max() : int
408
	{
409
		/** controller/jobs/customer/import/xml/max-query
410
		 * Maximum number of XML nodes processed at once
411
		 *
412
		 * Processing and fetching several attribute items at once speeds up importing
413
		 * the XML files. The more items can be processed at once, the faster the
414
		 * import. More items also increases the memory usage of the importer and
415
		 * thus, this parameter should be low enough to avoid reaching the memory
416
		 * limit of the PHP process.
417
		 *
418
		 * @param integer Number of XML nodes
419
		 * @since 2019.04
420
		 * @see controller/jobs/customer/import/xml/domains
421
		 * @see controller/jobs/customer/import/xml/location
422
		 * @see controller/jobs/customer/import/xml/backup
423
		 */
424
		return $this->context()->config()->get( 'controller/jobs/customer/import/xml/max-query', 100 );
425
	}
426
427
428
	/**
429
	 * Updates the customer item and its referenced items using the given DOM node
430
	 *
431
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object to update
432
	 * @param \DomElement $node DOM node used for updateding the customer item
433
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated customer item object
434
	 */
435
	protected function process( \Aimeos\MShop\Customer\Item\Iface $item, \DomElement $node ) : \Aimeos\MShop\Customer\Item\Iface
436
	{
437
		try
438
		{
439
			$list = [];
440
441
			foreach( $node->attributes as $attr ) {
442
				$list[$attr->nodeName] = $attr->nodeValue;
443
			}
444
445
			foreach( $node->childNodes as $tag )
446
			{
447
				if( in_array( $tag->nodeName, ['address', 'lists', 'property', 'group'] ) ) {
448
					$item = $this->getProcessor( $tag->nodeName )->process( $item, $tag );
449
				} elseif( $tag->nodeName[0] !== '#' ) {
450
					$list[$tag->nodeName] = $tag->nodeValue;
451
				}
452
			}
453
454
			$item->fromArray( $list, true );
455
		}
456
		catch( \Exception $e )
457
		{
458
			$msg = 'Customer import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString();
459
			$this->context()->logger()->error( $msg, 'import/xml/customer' );
460
		}
461
462
		return $item;
463
	}
464
}
465