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

Standard::getItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 10
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\Attribute\Import\Xml;
12
13
14
/**
15
 * Job controller for XML attribute 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\Traits;
25
	use \Aimeos\Controller\Common\Common\Import\Xml\Traits;
26
27
28
	/**
29
	 * Returns the localized name of the job.
30
	 *
31
	 * @return string Name of the job
32
	 */
33
	public function getName() : string
34
	{
35
		return $this->context()->translate( 'controller/jobs', 'Attribute import XML' );
36
	}
37
38
39
	/**
40
	 * Returns the localized description of the job.
41
	 *
42
	 * @return string Description of the job
43
	 */
44
	public function getDescription() : string
45
	{
46
		return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing attributes from XML files' );
47
	}
48
49
50
	/**
51
	 * Executes the job.
52
	 *
53
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
54
	 */
55
	public function run()
56
	{
57
		$context = $this->context();
58
		$config = $context->config();
59
		$logger = $context->logger();
60
61
		/** controller/jobs/attribute/import/xml/location
62
		 * File or directory where the content is stored which should be imported
63
		 *
64
		 * You need to configure the XML file or directory with the XML files that
65
		 * should be imported. It should be an absolute path to be sure but can be
66
		 * relative path if you absolutely know from where the job will be executed
67
		 * from.
68
		 *
69
		 * @param string Absolute file or directory path
70
		 * @since 2019.04
71
		 * @category Developer
72
		 * @category User
73
		 * @see controller/jobs/attribute/import/xml/container/type
74
		 * @see controller/jobs/attribute/import/xml/container/content
75
		 * @see controller/jobs/attribute/import/xml/container/options
76
		 */
77
		$location = $config->get( 'controller/jobs/attribute/import/xml/location' );
78
79
		try
80
		{
81
			$logger->info( sprintf( 'Started attribute import from "%1$s"', $location ), 'import/xml/attribute' );
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(), 'attribute', 8 ) === 0 && $entry->getExtension() === 'xml' ) {
96
						$files[] = $entry->getPathname();
97
					}
98
				}
99
			}
100
			else
101
			{
102
				$files[] = $location;
103
			}
104
105
			sort( $files );
106
			$context->__sleep();
107
108
			$fcn = function( $filepath ) {
109
				$this->import( $filepath );
110
			};
111
112
			foreach( $files as $filepath ) {
113
				$context->process()->start( $fcn, [$filepath] );
114
			}
115
116
			$context->process()->wait();
117
118
			$logger->info( sprintf( 'Finished attribute import from "%1$s"', $location ), 'import/xml/attribute' );
119
		}
120
		catch( \Exception $e )
121
		{
122
			$logger->error( 'Attribute import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/xml/attribute' );
123
			$this->mail( 'Attribute XML import error', $e->getMessage() . "\n" . $e->getTraceAsString() );
124
			throw $e;
125
		}
126
	}
127
128
129
	/**
130
	 * Imports the XML file given by its path
131
	 *
132
	 * @param string $filename Absolute or relative path to the XML file
133
	 */
134
	protected function import( string $filename )
135
	{
136
		$context = $this->context();
137
		$config = $context->config();
138
		$logger = $context->logger();
139
140
141
		/** controller/jobs/attribute/import/xml/domains
142
		 * List of item domain names that should be retrieved along with the attribute items
143
		 *
144
		 * This configuration setting overwrites the shared option
145
		 * "controller/common/attribute/import/xml/domains" if you need a
146
		 * specific setting for the job controller. Otherwise, you should
147
		 * use the shared option for consistency.
148
		 *
149
		 * @param array Associative list of MShop item domain names
150
		 * @since 2019.04
151
		 * @category Developer
152
		 * @see controller/jobs/attribute/import/xml/backup
153
		 * @see controller/jobs/attribute/import/xml/max-query
154
		 */
155
		$domains = $config->get( 'controller/jobs/attribute/import/xml/domains', [] );
156
157
		/** controller/jobs/attribute/import/xml/backup
158
		 * Name of the backup for sucessfully imported files
159
		 *
160
		 * After a XML file was imported successfully, you can move it to another
161
		 * location, so it won't be imported again and isn't overwritten by the
162
		 * next file that is stored at the same location in the file system.
163
		 *
164
		 * You should use an absolute path to be sure but can be relative path
165
		 * if you absolutely know from where the job will be executed from. The
166
		 * name of the new backup location can contain placeholders understood
167
		 * by the PHP DateTime::format() method (with percent signs prefix) to
168
		 * create dynamic paths, e.g. "backup/%Y-%m-%d" which would create
169
		 * "backup/2000-01-01". For more information about the date() placeholders,
170
		 * please have a look  into the PHP documentation of the
171
		 * {@link https://www.php.net/manual/en/datetime.format.php format() method}.
172
		 *
173
		 * **Note:** If no backup name is configured, the file or directory
174
		 * won't be moved away. Please make also sure that the parent directory
175
		 * and the new directory are writable so the file or directory could be
176
		 * moved.
177
		 *
178
		 * @param integer Name of the backup file, optionally with date/time placeholders
179
		 * @since 2019.04
180
		 * @category Developer
181
		 * @see controller/jobs/attribute/import/xml/domains
182
		 * @see controller/jobs/attribute/import/xml/max-query
183
		 */
184
		$backup = $config->get( 'controller/jobs/attribute/import/xml/backup' );
185
186
		/** controller/jobs/attribute/import/xml/max-query
187
		 * Maximum number of XML nodes processed at once
188
		 *
189
		 * Processing and fetching several attribute items at once speeds up importing
190
		 * the XML files. The more items can be processed at once, the faster the
191
		 * import. More items also increases the memory usage of the importer and
192
		 * thus, this parameter should be low enough to avoid reaching the memory
193
		 * limit of the PHP process.
194
		 *
195
		 * @param integer Number of XML nodes
196
		 * @since 2019.04
197
		 * @category Developer
198
		 * @category User
199
		 * @see controller/jobs/attribute/import/xml/domains
200
		 * @see controller/jobs/attribute/import/xml/backup
201
		 */
202
		$maxquery = $config->get( 'controller/jobs/attribute/import/xml/max-query', 100 );
203
204
205
		$slice = 0;
206
		$nodes = [];
207
		$xml = new \XMLReader();
208
209
		if( $xml->open( $filename, LIBXML_COMPACT | LIBXML_PARSEHUGE ) === false ) {
210
			throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No XML file "%1$s" found', $filename ) );
211
		}
212
213
		$logger->info( sprintf( 'Started attribute import from file "%1$s"', $filename ), 'import/xml/attribute' );
214
215
		while( $xml->read() === true )
216
		{
217
			if( $xml->depth === 1 && $xml->nodeType === \XMLReader::ELEMENT && $xml->name === 'attributeitem' )
218
			{
219
				if( ( $dom = $xml->expand() ) === false )
220
				{
221
					$msg = sprintf( 'Expanding "%1$s" node failed', 'attributeitem' );
222
					throw new \Aimeos\Controller\Jobs\Exception( $msg );
223
				}
224
225
				$nodes[] = $dom;
226
227
				if( $slice++ >= $maxquery )
228
				{
229
					$this->importNodes( $nodes, $domains );
230
					unset( $nodes );
231
					$nodes = [];
232
					$slice = 0;
233
				}
234
			}
235
		}
236
237
		$this->importNodes( $nodes, $domains );
238
		unset( $nodes );
239
240
		$this->saveTypes();
241
242
		foreach( $this->getProcessors() as $proc ) {
243
			$proc->finish();
244
		}
245
246
		$logger->info( sprintf( 'Finished attribute import from file "%1$s"', $filename ), 'import/xml/attribute' );
247
248
		if( !empty( $backup ) && @rename( $filename, $backup = \Aimeos\MW\Str::strtime( $backup ) ) === false )
249
		{
250
			$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $filename, $backup );
251
			throw new \Aimeos\Controller\Jobs\Exception( $msg );
252
		}
253
	}
254
255
256
	/**
257
	 * Imports the given DOM nodes
258
	 *
259
	 * @param \DomElement[] $nodes List of nodes to import
260
	 * @param string[] $ref List of domain names whose referenced items will be updated in the attribute items
261
	 */
262
	protected function importNodes( array $nodes, array $ref )
263
	{
264
		$keys = [];
265
266
		foreach( $nodes as $node )
267
		{
268
			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

268
			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...
269
				$keys[] = $attr->nodeValue;
270
			}
271
		}
272
273
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
274
		$search = $manager->filter()->slice( 0, count( $keys ) )->add( ['attribute.key' => $keys] );
275
		$items = $manager->search( $search, $ref );
276
		$map = $items->getKey()->combine( $items );
277
278
		foreach( $nodes as $node )
279
		{
280
			if( ( $attr = $node->attributes->getNamedItem( 'ref' ) ) !== null && isset( $map[md5( $attr->nodeValue )] ) ) {
281
				$item = $this->process( $map[md5( $attr->nodeValue )], $node );
0 ignored issues
show
Bug introduced by
It seems like $map[md5($attr->nodeValue)] can also be of type null; however, parameter $item of Aimeos\Controller\Jobs\A...Xml\Standard::process() does only seem to accept Aimeos\MShop\Attribute\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

281
				$item = $this->process( /** @scrutinizer ignore-type */ $map[md5( $attr->nodeValue )], $node );
Loading history...
282
			} else {
283
				$item = $this->process( $manager->create(), $node );
284
			}
285
286
			$manager->save( $item );
287
			$this->addType( 'attribute/type', $item->getDomain(), $item->getType() );
0 ignored issues
show
Bug introduced by
It seems like $item->getType() can also be of type null; however, parameter $code of Aimeos\Controller\Jobs\A...Xml\Standard::addType() does only seem to accept string, 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

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