Passed
Push — master ( e3443a...9a4066 )
by Aimeos
01:47
created

Traits::getProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
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
				$classname = is_string( $part ) ? '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $part : '<not a string>';
67
				throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
68
			}
69
		}
70
71
		$name = $config->get( 'controller/common/product/import/xml/processor/' . $type . '/name', 'Standard' );
72
73
		if( ctype_alnum( $name ) === false )
74
		{
75
			$classname = is_string( $name ) ? '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . implode( '\\', $parts ) . '\\' . $name : '<not a string>';
76
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
77
		}
78
79
		$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . str_replace( '/', '\\', ucwords( $type, '/' ) ) . '\\' . $name;
80
81
		if( class_exists( $classname ) === false ) {
82
			throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
83
		}
84
85
		$iface = \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface::class;
86
		return \Aimeos\MW\Common\Base::checkClass( $iface, new $classname( $context ) );
87
	}
88
}
89