Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Base::getSubManagerBase()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 9
nop 3
dl 0
loc 37
rs 8.0835
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MAdmin
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\MAdmin\Common\Manager;
13
14
15
/**
16
 * Provides common methods required by most of the manager classes.
17
 *
18
 * @package MAdmin
19
 * @subpackage Common
20
 */
21
abstract class Base extends \Aimeos\MShop\Common\Manager\Base
22
{
23
	/**
24
	 * Adds the configured decorators to the given manager object.
25
	 *
26
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager object
27
	 * @param string $managerpath Manager sub-names separated by slashes, e.g. "list/type"
28
	 * @param string $domain Domain name in lower case, e.g. "product"
29
	 */
30
	protected function addManagerDecorators( \Aimeos\MShop\Common\Manager\Iface $manager, $managerpath, $domain )
31
	{
32
		$context = $this->getContext();
33
		$config = $context->getConfig();
34
35
		/** madmin/common/manager/decorators/default
36
		 * Configures the list of decorators applied to all admin managers
37
		 *
38
		 * Decorators extend the functionality of a class by adding new aspects
39
		 * (e.g. log what is currently done), executing the methods of the underlying
40
		 * class only in certain conditions (e.g. only for logged in users) or
41
		 * modify what is returned to the caller.
42
		 *
43
		 * This option allows you to configure a list of decorator names that should
44
		 * be wrapped around the original instances of all created managers:
45
		 *
46
		 *  madmin/common/manager/decorators/default = array( 'decorator1', 'decorator2' )
47
		 *
48
		 * This would wrap the decorators named "decorator1" and "decorator2" around
49
		 * all controller instances in that order. The decorator classes would be
50
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" and
51
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator2".
52
		 *
53
		 * @param array List of decorator names
54
		 * @since 2014.03
55
		 * @category Developer
56
		 */
57
		$decorators = $config->get( 'madmin/common/manager/decorators/default', [] );
58
		$excludes = $config->get( 'madmin/' . $domain . '/manager/' . $managerpath . '/decorators/excludes', [] );
59
60
		foreach( $decorators as $key => $name )
61
		{
62
			if( in_array( $name, $excludes ) ) {
63
				unset( $decorators[$key] );
64
			}
65
		}
66
67
		$classprefix = '\\Aimeos\\MShop\\Common\\Manager\\Decorator\\';
68
		$manager = $this->addDecorators( $context, $manager, $decorators, $classprefix );
69
70
		$classprefix = '\\Aimeos\\MShop\\Common\\Manager\\Decorator\\';
71
		$decorators = $config->get( 'madmin/' . $domain . '/manager/' . $managerpath . '/decorators/global', [] );
72
		$manager = $this->addDecorators( $context, $manager, $decorators, $classprefix );
73
74
		$subpath = $this->createSubNames( $managerpath );
75
		$classprefix = 'MShop_' . ucfirst( $domain ) . '_Manager_' . $subpath . '_Decorator_';
76
		$decorators = $config->get( 'madmin/' . $domain . '/manager/' . $managerpath . '/decorators/local', [] );
77
78
		return $this->addDecorators( $context, $manager, $decorators, $classprefix );
79
	}
80
81
82
	/**
83
	 * Returns a new manager the given extension name
84
	 *
85
	 * @param string $domain Name of the domain (product, text, media, etc.)
86
	 * @param string $manager Name of the sub manager type in lower case (can contain a path like base/product)
87
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
88
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions
89
	 */
90
	protected function getSubManagerBase( $domain, $manager, $name )
91
	{
92
		$domain = strtolower( $domain );
93
		$manager = strtolower( $manager );
94
		$config = $this->getContext()->getConfig();
95
96
97
		if( empty( $domain ) || ctype_alnum( $domain ) === false ) {
98
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Invalid characters in domain name "%1$s"', $domain ) );
99
		}
100
101
		if( $name === null ) {
102
			$name = $config->get( 'mshop/' . $domain . '/manager/' . $manager . '/name', 'Standard' );
103
		}
104
105
		if( empty( $name ) || ctype_alnum( $name ) === false ) {
106
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Invalid characters in manager name "%1$s"', $name ) );
107
		}
108
109
		$domainname = ucfirst( $domain );
110
		$subnames = $this->createSubNames( $manager );
111
112
		$classname = '\\Aimeos\\MAdmin\\' . $domainname . '\\Manager\\' . $subnames . '\\' . $name;
113
		$interface = '\\Aimeos\\MAdmin\\' . $domainname . '\\Manager\\' . $subnames . '\\Iface';
114
115
		if( class_exists( $classname ) === false ) {
116
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
117
		}
118
119
		$subManager = new $classname( $this->getContext() );
120
121
		if( ( $subManager instanceof $interface ) === false ) {
122
			throw new \Aimeos\MAdmin\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $interface ) );
123
		}
124
125
		return $subManager;
126
	}
127
}
128