Passed
Push — master ( 44dd5c...d886ca )
by Aimeos
02:13
created

Traits::getProcessors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
	 * Returns the processor objects which have been used up to now
51
	 *
52
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface[] List of processor objects
53
	 */
54
	protected function getProcessors()
55
	{
56
		return $this->processors;
57
	}
58
59
60
	/**
61
	 * Creates a new processor object of the given type
62
	 *
63
	 * @param string $type Type of the processor
64
	 * @return \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface Processor object
65
	 * @throws \Aimeos\Controller\Common\Exception If type is invalid or processor isn't found
66
	 */
67
	protected function createProcessor( $type )
68
	{
69
		$context = $this->getContext();
70
		$config = $context->getConfig();
71
		$parts = explode( '/', $type );
72
73
		foreach( $parts as $part )
74
		{
75
			if( ctype_alnum( $part ) === false )
76
			{
77
				$msg = sprintf( 'Invalid characters in processor type "%1$s"', $type );
78
				throw new \Aimeos\Controller\Common\Exception( $msg );
79
			}
80
		}
81
82
		$name = $config->get( 'controller/common/common/import/xml/processor/' . $type . '/name', 'Standard' );
83
84
		if( ctype_alnum( $name ) === false )
85
		{
86
			$msg = sprintf( 'Invalid characters in processor name "%1$s"', $name );
87
			throw new \Aimeos\Controller\Common\Exception( $msg );
88
		}
89
90
		$segment = str_replace( '/', '\\', ucwords( $type, '/' ) ) . '\\' . $name;
91
		$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $segment;
92
93
		if( class_exists( $classname ) === false )
94
		{
95
			$classname = '\\Aimeos\\Controller\\Common\\Common\\Import\\Xml\\Processor\\' . $segment;
96
97
			if( class_exists( $classname ) === false ) {
98
				throw new \Aimeos\Controller\Common\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
99
			}
100
		}
101
102
		$iface = \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface::class;
103
		return \Aimeos\MW\Common\Base::checkClass( $iface, new $classname( $context ) );
104
	}
105
}
106