Passed
Push — master ( 659531...28cbe9 )
by Aimeos
03:02
created

Standard::importNodes()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 24
rs 9.2222
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2021
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
	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() : string
33
	{
34
		return $this->context()->translate( 'controller/jobs', 'Customer 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() : string
44
	{
45
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing customers 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->context();
57
		$config = $context->config();
58
		$logger = $context->logger();
59
60
		/** controller/jobs/customer/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/customer/import/xml/container/type
73
		 * @see controller/jobs/customer/import/xml/container/content
74
		 * @see controller/jobs/customer/import/xml/container/options
75
		 */
76
		$location = $config->get( 'controller/jobs/customer/import/xml/location' );
77
78
		try
79
		{
80
			$logger->info( sprintf( 'Started customer import from "%1$s"', $location ), 'import/xml/customer' );
81
82
			if( !file_exists( $location ) )
83
			{
84
				$msg = sprintf( 'File or directory "%1$s" doesn\'t exist', $location );
85
				throw new \Aimeos\Controller\Jobs\Exception( $msg );
86
			}
87
88
			$files = [];
89
90
			if( is_dir( $location ) )
91
			{
92
				foreach( new \DirectoryIterator( $location ) as $entry )
93
				{
94
					if( strncmp( $entry->getFilename(), 'customer', 8 ) === 0 && $entry->getExtension() === 'xml' ) {
95
						$files[] = $entry->getPathname();
96
					}
97
				}
98
			}
99
			else
100
			{
101
				$files[] = $location;
102
			}
103
104
			sort( $files );
105
			$context->__sleep();
106
107
			$fcn = function( $filepath ) {
108
				$this->import( $filepath );
109
			};
110
111
			foreach( $files as $filepath ) {
112
				$context->process()->start( $fcn, [$filepath] );
113
			}
114
115
			$context->process()->wait();
116
117
			$logger->info( sprintf( 'Finished customer import from "%1$s"', $location ), 'import/xml/customer' );
118
		}
119
		catch( \Exception $e )
120
		{
121
			$logger->error( 'Customer import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/customer' );
122
			$this->mail( 'Customer XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
123
			throw $e;
124
		}
125
	}
126
127
128
	/**
129
	 * Imports the XML file given by its path
130
	 *
131
	 * @param string $filename Absolute or relative path to the XML file
132
	 */
133
	protected function import( string $filename )
134
	{
135
		$context = $this->context();
136
		$config = $context->config();
137
		$logger = $context->logger();
138
139
140
		$domains = ['customer', 'customer/address', 'customer/property', 'customer/group', 'media', 'product', 'text'];
141
142
		/** controller/jobs/customer/import/xml/domains
143
		 * List of item domain names that should be retrieved along with the customer items
144
		 *
145
		 * This configuration setting overwrites the shared option
146
		 * "controller/common/customer/import/xml/domains" if you need a
147
		 * specific setting for the job controller. Otherwise, you should
148
		 * use the shared option for consistency.
149
		 *
150
		 * @param array Associative list of MShop item domain names
151
		 * @since 2019.04
152
		 * @category Developer
153
		 * @see controller/jobs/customer/import/xml/backup
154
		 * @see controller/jobs/customer/import/xml/max-query
155
		 */
156
		$domains = $config->get( 'controller/jobs/customer/import/xml/domains', $domains );
157
158
		/** controller/jobs/customer/import/xml/backup
159
		 * Name of the backup for sucessfully imported files
160
		 *
161
		 * After a XML file was imported successfully, you can move it to another
162
		 * location, so it won't be imported again and isn't overwritten by the
163
		 * next file that is stored at the same location in the file system.
164
		 *
165
		 * You should use an absolute path to be sure but can be relative path
166
		 * if you absolutely know from where the job will be executed from. The
167
		 * name of the new backup location can contain placeholders understood
168
		 * by the PHP DateTime::format() method (with percent signs prefix) to
169
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
170
		 * "backup/2000-01-01". For more information about the date() placeholders,
171
		 * please have a look  into the PHP documentation of the
172
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
173
		 *
174
		 * **Note:** If no backup name is configured, the file or directory
175
		 * won't be moved away. Please make also sure that the parent directory
176
		 * and the new directory are writable so the file or directory could be
177
		 * moved.
178
		 *
179
		 * @param integer Name of the backup file, optionally with date/time placeholders
180
		 * @since 2019.04
181
		 * @category Developer
182
		 * @see controller/jobs/customer/import/xml/domains
183
		 * @see controller/jobs/customer/import/xml/max-query
184
		 */
185
		$backup = $config->get( 'controller/jobs/customer/import/xml/backup' );
186
187
		/** controller/jobs/customer/import/xml/max-query
188
		 * Maximum number of XML nodes processed at once
189
		 *
190
		 * Processing and fetching several customer items at once speeds up importing
191
		 * the XML files. The more items can be processed at once, the faster the
192
		 * import. More items also increases the memory usage of the importer and
193
		 * thus, this parameter should be low enough to avoid reaching the memory
194
		 * limit of the PHP process.
195
		 *
196
		 * @param integer Number of XML nodes
197
		 * @since 2019.04
198
		 * @category Developer
199
		 * @category User
200
		 * @see controller/jobs/customer/import/xml/domains
201
		 * @see controller/jobs/customer/import/xml/backup
202
		 */
203
		$maxquery = $config->get( 'controller/jobs/customer/import/xml/max-query', 100 );
204
205
206
		$slice = 0;
207
		$nodes = [];
208
		$xml = new \XMLReader();
209
210
		if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) {
211
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) );
212
		}
213
214
		$logger->info( sprintf( 'Started customer import from file "%1$s"', $filename ), 'import/xml/customer' );
215
216
		while( $xml->read() === true )
217
		{
218
			if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'customeritem' )
219
			{
220
				if( ( $dom = $xml->expand() ) === false )
221
				{
222
					$msg = sprintf( 'Expanding "%1$s" node failed', 'customeritem' );
223
					throw new \Aimeos\Controller\Jobs\Exception( $msg );
224
				}
225
226
				$nodes[] = $dom;
227
228
				if( $slice++ >= $maxquery )
229
				{
230
					$this->importNodes( $nodes, $domains );
231
					unset( $nodes );
232
					$nodes = [];
233
					$slice = 0;
234
				}
235
			}
236
		}
237
238
		$this->importNodes( $nodes, $domains );
239
		unset( $nodes );
240
241
		foreach( $this->getProcessors() as $proc ) {
242
			$proc->finish();
243
		}
244
245
		$logger->info( sprintf( 'Finished customer import from file "%1$s"', $filename ), 'import/xml/customer' );
246
247
		if( !empty( $backup ) && @rename( $filename, $backup = \Aimeos\MW\Str::strtime( $backup ) ) === false )
248
		{
249
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
250
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
251
		}
252
	}
253
254
255
	/**
256
	 * Imports the given DOM nodes
257
	 *
258
	 * @param \DomElement[] $nodes List of nodes to import
259
	 * @param string[] $ref List of domain names whose referenced items will be updated in the customer items
260
	 */
261
	protected function importNodes( array $nodes, array $ref )
262
	{
263
		$codes = [];
264
265
		foreach( $nodes as $node )
266
		{
267
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null ) {
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

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

267
			if( ( $attr = $node->attributes->/** @scrutinizer ignore-call */ getNamedItem( 'ref' ) ) !== null ) {

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...
268
				$codes[$attr->nodeValue] = null;
269
			}
270
		}
271
272
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
273
		$search = $manager->filter()->slice( 0, count( $codes ) )->add( ['customer.code'=> array_keys( $codes )] );
274
		$map = $manager->search( $search, $ref )->col( null, 'customer.code' );
275
276
		foreach( $nodes as $node )
277
		{
278
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[$attr->nodeValue] ) ) {
279
				$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

279
				$item = $this->process( /** @scrutinizer ignore-type */ $map[$attr->nodeValue], $node );
Loading history...
280
			} else {
281
				$item = $this->process( $manager->create(), $node );
282
			}
283
284
			$manager->save( $item );
285
		}
286
	}
287
288
289
	/**
290
	 * Updates the customer item and its referenced items using the given DOM node
291
	 *
292
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object to update
293
	 * @param \DomElement $node DOM node used for updateding the customer item
294
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated customer item object
295
	 */
296
	protected function process( \Aimeos\MShop\Customer\Item\Iface $item, \DomElement $node ) : \Aimeos\MShop\Customer\Item\Iface
297
	{
298
		try
299
		{
300
			$list = [];
301
302
			foreach( $node->attributes as $attr ) {
303
				$list[$attr->nodeName] = $attr->nodeValue;
304
			}
305
306
			foreach( $node->childNodes as $tag )
307
			{
308
				if( in_array( $tag->nodeName, ['address', 'lists', 'property', 'group'] ) ) {
309
					$item = $this->getProcessor( $tag->nodeName )->process( $item, $tag );
310
				} elseif( $tag->nodeName[0] !== '#' ) {
311
					$list[$tag->nodeName] = $tag->nodeValue;
312
				}
313
			}
314
315
			$item->fromArray( $list, true );
316
		}
317
		catch( \Exception $e )
318
		{
319
			$msg = 'Customer import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString();
320
			$this->context()->logger()->error( $msg, 'import/xml/customer' );
321
		}
322
323
		return $item;
324
	}
325
}
326