Passed
Push — master ( dda91a...58f742 )
by Aimeos
03:00
created

Traits::createProcessor()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 9
nop 1
dl 0
loc 37
rs 9.0111
c 0
b 0
f 0
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 context item
27
	 *
28
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
29
	 */
30
	abstract protected function getContext();
31
32
33
	/**
34
	 * Returns the processor object for adding the product related information
35
	 *
36
	 * @param string $type Type of the processor
37
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface Processor object
38
	 */
39
	protected function getProcessor( $type )
40
	{
41
		if( !isset( $this->processors[$type] ) ) {
42
			$this->processors[$type] = $this->createProcessor( $type );
43
		}
44
45
		return $this->processors[$type];
46
	}
47
48
49
	/**
50
	 * Creates a new processor object of the given type
51
	 *
52
	 * @param string $type Type of the processor
53
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface Processor object
54
	 * @throws \Aimeos\Controller\Common\Exception If type is invalid or processor isn't found
55
	 */
56
	protected function createProcessor( $type )
57
	{
58
		$context = $this->getContext();
59
		$config = $context->getConfig();
60
		$parts = explode( '/', $type );
61
62
		foreach( $parts as $part )
63
		{
64
			if( ctype_alnum( $part ) === false )
65
			{
66
				$msg = sprintf( 'Invalid characters in processor type "%1$s"', $type );
67
				throw new \Aimeos\Controller\Common\Exception( $msg );
68
			}
69
		}
70
71
		$name = $config->get( 'controller/common/common/import/xml/processor/' . $type . '/name', 'Standard' );
72
73
		if( ctype_alnum( $name ) === false )
74
		{
75
			$msg = sprintf( 'Invalid characters in processor name "%1$s"', $name );
76
			throw new \Aimeos\Controller\Common\Exception( $msg );
77
		}
78
79
		$segment = str_replace( '/', '\\', ucwords( $type, '/' ) ) . '\\' . $name;
80
		$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $segment;
81
82
		if( class_exists( $classname ) === false )
83
		{
84
			$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $segment;
85
86
			if( class_exists( $classname ) === false ){
87
				throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
88
			}
89
		}
90
91
		$iface = \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface::class;
92
		return \Aimeos\MW\Common\Base::checkClass( $iface, new $classname( $context ) );
93
	}
94
}
95