Passed
Push — master ( 96cc40...534161 )
by Aimeos
01:56
created

Traits   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getProcessor() 0 7 2
A createProcessor() 0 27 6
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 Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Common\Import\Xml;
12
13
14
/**
15
 * Shared class for XML importers
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
trait Traits
21
{
22
	private $processors = [];
23
24
25
	/**
26
	 * Returns the processor object for adding the product related information
27
	 *
28
	 * @param string $type Type of the processor
29
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface Processor object
30
	 */
31
	protected function getProcessor( $type )
32
	{
33
		if( !isset( $this->processors[$type] ) ) {
34
			$this->processors[$type] = $this->createProcessor( $type );
35
		}
36
37
		return $this->processors[$type];
38
	}
39
40
41
	/**
42
	 * Creates a new processor object of the given type
43
	 *
44
	 * @param string $type Type of the processor
45
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface Processor object
46
	 * @throws \Aimeos\Controller\Common\Exception If type is invalid or processor isn't found
47
	 */
48
	protected function createProcessor( $type )
49
	{
50
		$context = $this->getContext();
0 ignored issues
show
Bug introduced by
It seems like getContext() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

50
		/** @scrutinizer ignore-call */ 
51
  $context = $this->getContext();
Loading history...
51
		$config = $context->getConfig();
52
53
		if( ctype_alnum( $type ) === false )
54
		{
55
			$classname = is_string($type) ? '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $type : '<not a string>';
0 ignored issues
show
introduced by
The condition is_string($type) is always true.
Loading history...
56
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
57
		}
58
59
		$name = $config->get( 'controller/common/product/import/xml/processor/' . $type . '/name', 'Standard' );
60
61
		if( ctype_alnum( $name ) === false )
62
		{
63
			$classname = is_string($name) ? '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $type . '\\' . $name : '<not a string>';
64
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
65
		}
66
67
		$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . ucfirst( $type ) . '\\' . $name;
68
69
		if( class_exists( $classname ) === false ) {
70
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
71
		}
72
73
		$iface = \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface::class;
74
		return \Aimeos\MW\Common\Base::checkClass( $iface, new $classname( $context ) );
75
	}
76
}
77