Passed
Push — master ( 5d05b9...5af5d7 )
by Aimeos
05:15
created

Base   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addRuleDecorators() 0 23 4
A getProvider() 0 31 5
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package MShop
7
 * @subpackage Rule
8
 */
9
10
11
namespace Aimeos\MShop\Rule\Manager;
12
13
14
/**
15
 * Abstract class for rule managers.
16
 *
17
 * @package MShop
18
 * @subpackage Service
19
 */
20
abstract class Base
21
	extends \Aimeos\MShop\Common\Manager\Base
22
{
23
	/**
24
	 * Returns the rule provider which is responsible for the rule item.
25
	 *
26
	 * @param \Aimeos\MShop\Rule\Item\Iface $item Rule item object
27
	 * @param string $type Rule type code
28
	 * @return \Aimeos\MShop\Rule\Provider\Iface Returns the decoratad rule provider object
29
	 * @throws \Aimeos\MShop\Rule\Exception If provider couldn't be found
30
	 */
31
	public function getProvider( \Aimeos\MShop\Rule\Item\Iface $item, string $type ) : \Aimeos\MShop\Rule\Provider\Iface
32
	{
33
		$type = ucwords( $type );
34
		$names = explode( ',', $item->getProvider() );
35
36
		if( ctype_alnum( $type ) === false ) {
37
			throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Invalid characters in type name "%1$s"', $type ) );
38
		}
39
40
		if( ( $provider = array_shift( $names ) ) === null ) {
41
			throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Provider in "%1$s" not available', $item->getProvider() ) );
42
		}
43
44
		if( ctype_alnum( $provider ) === false ) {
45
			throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Invalid characters in provider name "%1$s"', $provider ) );
46
		}
47
48
		$classname = '\Aimeos\MShop\Rule\Provider\\' . $type . '\\' . $provider;
49
50
		if( class_exists( $classname ) === false ) {
51
			throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
52
		}
53
54
		$context = $this->getContext();
55
		$config = $context->getConfig();
0 ignored issues
show
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
56
		$provider = new $classname( $context, $item );
57
58
		self::checkClass( \Aimeos\MShop\Rule\Provider\Factory\Iface::class, $provider );
59
		$provider = $this->addRuleDecorators( $item, $provider, $names, $type );
60
61
		return $provider->setObject( $provider );
62
	}
63
64
65
	/**
66
	 *
67
	 * @param \Aimeos\MShop\Rule\Item\Iface $ruleItem Rule item object
68
	 * @param \Aimeos\MShop\Rule\Provider\Iface $provider Rule provider object
69
	 * @param array $names List of decorator names that should be wrapped around the rule provider object
70
	 * @param string $type Rule type code
71
	 * @return \Aimeos\MShop\Rule\Provider\Iface Rule provider object
72
	 */
73
	protected function addRuleDecorators( \Aimeos\MShop\Rule\Item\Iface $ruleItem,
74
		\Aimeos\MShop\Rule\Provider\Iface $provider, array $names, string $type ) : \Aimeos\MShop\Rule\Provider\Iface
75
	{
76
		$classprefix = '\Aimeos\MShop\Rule\Provider\\' . $type . '\Decorator\\';
77
78
		foreach( $names as $name )
79
		{
80
			if( ctype_alnum( $name ) === false ) {
81
				throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Invalid characters in class name "%1$s"', $name ) );
82
			}
83
84
			$classname = $classprefix . $name;
85
86
			if( class_exists( $classname ) === false ) {
87
				throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
88
			}
89
90
			$provider = new $classname( $this->getContext(), $ruleItem, $provider );
91
92
			self::checkClass( $classprefix . 'Iface', $provider );
93
		}
94
95
		return $provider;
96
	}
97
}
98