Passed
Push — master ( 9703dd...7e8f09 )
by Aimeos
15:10
created

Base::addClientDecorators()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 58
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 7
nop 3
dl 0
loc 58
rs 9.2888

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm\Common\Factory;
12
13
14
/**
15
 * Common methods for all JSON API factories
16
 *
17
 * @package Admin
18
 * @subpackage JsonAdm
19
 */
20
class Base
21
{
22
	private static $objects = [];
23
24
25
	/**
26
	 * Injects a client object
27
	 *
28
	 * The object is returned via create() if an instance of the class
29
	 * with the name name is requested.
30
	 *
31
	 * @param string $classname Full name of the class for which the object should be returned
32
	 * @param \Aimeos\Admin\JsonAdm\Iface|null $client JSON API client object
33
	 */
34
	public static function injectClient( string $classname, \Aimeos\Admin\JsonAdm\Iface $client = null )
35
	{
36
		self::$objects[$classname] = $client;
37
	}
38
39
40
	/**
41
	 * Adds the decorators to the JSON API client object
42
	 *
43
	 * @param \Aimeos\Admin\JsonAdm\Common\Iface $client Client object
44
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
45
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
46
	 * @return \Aimeos\Admin\JsonAdm\Iface Client object
47
	 */
48
	protected static function addClientDecorators( \Aimeos\Admin\JsonAdm\Iface $client,
49
		\Aimeos\MShop\Context\Item\Iface $context, string $path ) : \Aimeos\Admin\JsonAdm\Iface
50
	{
51
		$config = $context->getConfig();
52
53
		/** admin/jsonadm/common/decorators/default
54
		 * Configures the list of decorators applied to all JSON API clients
55
		 *
56
		 * Decorators extend the functionality of a class by adding new aspects
57
		 * (e.g. log what is currently done), executing the methods of the underlying
58
		 * class only in certain conditions (e.g. only for logged in users) or
59
		 * modify what is returned to the caller.
60
		 *
61
		 * This option allows you to configure a list of decorator names that should
62
		 * be wrapped around the original instance of all created clients:
63
		 *
64
		 *  admin/jsonadm/common/decorators/default = array( 'decorator1', 'decorator2' )
65
		 *
66
		 * This would wrap the decorators named "decorator1" and "decorator2" around
67
		 * all client instances in that order. The decorator classes would be
68
		 * "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator1" and
69
		 * "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator2".
70
		 *
71
		 * @param array List of decorator names
72
		 * @since 2015.12
73
		 * @category Developer
74
		 */
75
		$decorators = $config->get( 'admin/jsonadm/common/decorators/default', [] );
76
77
		if( empty( $path ) )
78
		{
79
			$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
80
			return self::addDecorators( $client, $decorators, $classprefix, $context, $path );
81
		}
82
83
		$dpath = trim( $path, '/' );
84
		$dpath = ( $dpath !== '' ? $dpath . '/' : $dpath );
85
86
		$excludes = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/excludes', [] );
87
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
88
89
		foreach( $decorators as $key => $name )
90
		{
91
			if( in_array( $name, $excludes ) ) {
92
				unset( $decorators[$key] );
93
			}
94
		}
95
96
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
97
		$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path );
98
99
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
100
		$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/global', [] );
101
		$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path );
102
103
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\' . ucfirst( $localClass ) . '\\Decorator\\';
104
		$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/local', [] );
105
		return self::addDecorators( $client, $decorators, $classprefix, $context, $path );
106
	}
107
108
109
	/**
110
	 * Adds the decorators to the client object
111
	 *
112
	 * @param \Aimeos\Admin\JsonAdm\Iface $client Client object
113
	 * @param array $decorators List of decorator names
114
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JsonAdm\Product\Decorator\"
115
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
116
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
117
	 * @return \Aimeos\Admin\JsonAdm\Iface Client object
118
	 */
119
	protected static function addDecorators( \Aimeos\Admin\JsonAdm\Iface $client, array $decorators, string $classprefix,
120
			\Aimeos\MShop\Context\Item\Iface $context, string $path ) : \Aimeos\Admin\JsonAdm\Iface
121
	{
122
		foreach( $decorators as $name )
123
		{
124
			if( ctype_alnum( $name ) === false )
125
			{
126
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
127
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 404 );
128
			}
129
130
			$classname = $classprefix . $name;
131
132
			if( class_exists( $classname ) === false ) {
133
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
134
			}
135
136
			$client = new $classname( $client, $context, $path );
137
138
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\Iface', $client );
139
		}
140
141
		return $client;
142
	}
143
144
145
	/**
146
	 * Creates a new client object
147
	 *
148
	 * @param string $classname Name of the client class
149
	 * @param string $interface Name of the client interface
150
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
151
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
152
	 * @return \Aimeos\Admin\JsonAdm\Common\Iface Client object
153
	 */
154
	protected static function createAdmin( string $classname, string $interface,
155
		\Aimeos\MShop\Context\Item\Iface $context, string $path ) : \Aimeos\Admin\JsonAdm\Common\Iface
156
	{
157
		if( isset( self::$objects[$classname] ) ) {
158
			return self::$objects[$classname];
159
		}
160
161
		if( class_exists( $classname ) === false ) {
162
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
163
		}
164
165
		$client = new $classname( $context, $path );
166
167
		\Aimeos\MW\Common\Base::checkClass( $interface, $client );
168
169
		return $client;
170
	}
171
}
172