Completed
Branch master (d2e225)
by Aimeos
03:06
created

JsonAdm::createRoot()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 133
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 4
dl 0
loc 133
rs 9.9
c 0
b 0
f 0

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-2018
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin;
12
13
14
/**
15
 * Factory which can create all JSON API clients
16
 *
17
 * @package Admin
18
 * @subpackage JsonAdm
19
 */
20
class JsonAdm extends \Aimeos\Admin\JsonAdm\Common\Factory\Base
21
{
22
	/**
23
	 * Creates the required client specified by the given path of client names.
24
	 *
25
	 * Clients are created by providing only the domain name, e.g. "product"
26
	 *  for the \Aimeos\Admin\JsonAdm\Product\Standard or a path of names to
27
	 * retrieve a specific sub-client, e.g. "product/type" for the
28
	 * \Aimeos\Admin\JsonAdm\Product\Type\Standard client.
29
	 *
30
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
31
	 * @param \Aimeos\Bootstrap $aimeos Aimeos Bootstrap object
32
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
33
	 * @param string|null $name Name of the client implementation ("Standard" if null)
34
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
35
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the given path is invalid
36
	 */
37
	static public function create( \Aimeos\MShop\Context\Item\Iface $context,
38
		\Aimeos\Bootstrap $aimeos, $path, $name = null )
39
	{
40
		$path = trim( $path, '/' );
41
42
		if( empty( $path ) ) {
43
			return self::createRoot( $context, $aimeos, $path, $name );
44
		} else {
45
			return self::createNew( $context, $aimeos, $path, $name );
46
		}
47
	}
48
49
50
	/**
51
	 * Creates a new client specified by the given path of client names.
52
	 *
53
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
54
	 * @param \Aimeos\Bootstrap $aimeos Aimeos Bootstrap object
55
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
56
	 * @param string|null $name Name of the client implementation ("Standard" if null)
57
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
58
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the given path is invalid
59
	 */
60
	protected static function createNew( \Aimeos\MShop\Context\Item\Iface $context,
61
		\Aimeos\Bootstrap $aimeos, $path, $name )
62
	{
63
		$pname = $name;
64
		$parts = explode( '/', $path );
65
66
		foreach( $parts as $key => $part )
67
		{
68
			if( ctype_alnum( $part ) === false )
69
			{
70
				$msg = sprintf( 'Invalid client "%1$s"', $path );
71
				throw new \Aimeos\Admin\JsonAdm\Exception( $msg, 400 );
72
			}
73
74
			$parts[$key] = ucwords( $part );
75
		}
76
77
		if( $pname === null ) {
78
			$pname = $context->getConfig()->get( 'admin/jsonadm/' . $path . '/name', 'Standard' );
79
		}
80
81
		$view = $context->getView();
82
		$config = $context->getConfig();
83
84
		if( $view->access( $config->get( 'admin/jsonadm/resource/' . $path . '/groups', [] ) ) !== true ) {
85
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Not allowed to access JsonAdm "%1$s" client', $path ) );
0 ignored issues
show
Bug introduced by
The type Aimeos\Admin\JQAdm\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
86
		}
87
88
89
		$view = $context->getView();
90
		$iface = \Aimeos\Admin\JsonAdm\Iface::class;
91
		$classname = '\\Aimeos\\Admin\\JsonAdm\\' . join( '\\', $parts ) . '\\' . $pname;
92
93
		if( ctype_alnum( $pname ) === false )
94
		{
95
			$classname = is_string( $pname ) ? $classname : '<not a string>';
96
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
97
		}
98
99
		if( class_exists( $classname ) === false ) {
100
			return self::createRoot( $context, $aimeos, $path, $name );
101
		}
102
103
		$client = self::createAdmin( $classname, $iface, $context, $path );
104
		$client = self::addClientDecorators( $client, $context, $path );
105
106
		return $client->setAimeos( $aimeos )->setView( $view );
0 ignored issues
show
Bug introduced by
The method setAimeos() does not exist on Aimeos\Admin\JsonAdm\Common\Iface. Since it exists in all sub-types, consider adding an abstract or default implementation to Aimeos\Admin\JsonAdm\Common\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
		return $client->/** @scrutinizer ignore-call */ setAimeos( $aimeos )->setView( $view );
Loading history...
107
	}
108
109
110
	/**
111
	 * Creates the top level client
112
	 *
113
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
114
	 * @param \Aimeos\Bootstrap $aimeos Aimeos Bootstrap object
115
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
116
	 * @param string|null $name Name of the JsonAdm client (default: "Standard")
117
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
118
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the client couldn't be created
119
	 */
120
	protected static function createRoot( \Aimeos\MShop\Context\Item\Iface $context,
121
		\Aimeos\Bootstrap $aimeos, $path, $name = null )
122
	{
123
		/** admin/jsonadm/name
124
		 * Class name of the used JSON API client implementation
125
		 *
126
		 * Each default JSON API client can be replace by an alternative imlementation.
127
		 * To use this implementation, you have to set the last part of the class
128
		 * name as configuration value so the client factory knows which class it
129
		 * has to instantiate.
130
		 *
131
		 * For example, if the name of the default class is
132
		 *
133
		 *  \Aimeos\Admin\JsonAdm\Standard
134
		 *
135
		 * and you want to replace it with your own version named
136
		 *
137
		 *  \Aimeos\Admin\JsonAdm\Mycntl
138
		 *
139
		 * then you have to set the this configuration option:
140
		 *
141
		 *  admin/jsonadm/name = Mycntl
142
		 *
143
		 * The value is the last part of your own class name and it's case sensitive,
144
		 * so take care that the configuration value is exactly named like the last
145
		 * part of the class name.
146
		 *
147
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
148
		 * characters are possible! You should always start the last part of the class
149
		 * name with an upper case character and continue only with lower case characters
150
		 * or numbers. Avoid chamel case names like "MyCntl"!
151
		 *
152
		 * @param string Last part of the class name
153
		 * @since 2015.12
154
		 * @category Developer
155
		 */
156
		if( $name === null ) {
157
			$name = $context->getConfig()->get( 'admin/jsonadm/name', 'Standard' );
158
		}
159
160
		if( ctype_alnum( $name ) === false )
161
		{
162
			$classname = is_string( $name ) ? '\\Aimeos\\Admin\\JsonAdm\\' . $name : '<not a string>';
163
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
164
		}
165
166
		$view = $context->getView();
167
		$iface = '\\Aimeos\\Admin\\JsonAdm\\Iface';
168
		$classname = '\\Aimeos\\Admin\\JsonAdm\\' . $name;
169
170
		$client = self::createAdmin( $classname, $iface, $context, $path );
171
172
		/** admin/jsonadm/decorators/excludes
173
		 * Excludes decorators added by the "common" option from the JSON API clients
174
		 *
175
		 * Decorators extend the functionality of a class by adding new aspects
176
		 * (e.g. log what is currently done), executing the methods of the underlying
177
		 * class only in certain conditions (e.g. only for logged in users) or
178
		 * modify what is returned to the caller.
179
		 *
180
		 * This option allows you to remove a decorator added via
181
		 * "admin/jsonadm/common/decorators/default" before they are wrapped
182
		 * around the Jsonadm client.
183
		 *
184
		 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
185
		 *
186
		 * This would remove the decorator named "decorator1" from the list of
187
		 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
188
		 * "admin/jsonadm/common/decorators/default" for the JSON API client.
189
		 *
190
		 * @param array List of decorator names
191
		 * @since 2016.01
192
		 * @category Developer
193
		 * @see admin/jsonadm/common/decorators/default
194
		 * @see admin/jsonadm/decorators/global
195
		 * @see admin/jsonadm/decorators/local
196
		 */
197
198
		/** admin/jsonadm/decorators/global
199
		 * Adds a list of globally available decorators only to the Jsonadm client
200
		 *
201
		 * Decorators extend the functionality of a class by adding new aspects
202
		 * (e.g. log what is currently done), executing the methods of the underlying
203
		 * class only in certain conditions (e.g. only for logged in users) or
204
		 * modify what is returned to the caller.
205
		 *
206
		 * This option allows you to wrap global decorators
207
		 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
208
		 * client.
209
		 *
210
		 *  admin/jsonadm/product/decorators/global = array( 'decorator1' )
211
		 *
212
		 * This would add the decorator named "decorator1" defined by
213
		 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
214
		 * "product" Jsonadm client.
215
		 *
216
		 * @param array List of decorator names
217
		 * @since 2016.01
218
		 * @category Developer
219
		 * @see admin/jsonadm/common/decorators/default
220
		 * @see admin/jsonadm/decorators/excludes
221
		 * @see admin/jsonadm/decorators/local
222
		 */
223
224
		/** admin/jsonadm/decorators/local
225
		 * Adds a list of local decorators only to the Jsonadm client
226
		 *
227
		 * Decorators extend the functionality of a class by adding new aspects
228
		 * (e.g. log what is currently done), executing the methods of the underlying
229
		 * class only in certain conditions (e.g. only for logged in users) or
230
		 * modify what is returned to the caller.
231
		 *
232
		 * This option allows you to wrap local decorators
233
		 * ("\Aimeos\Admin\Jsonadm\Product\Decorator\*") around the Jsonadm
234
		 * client.
235
		 *
236
		 *  admin/jsonadm/product/decorators/local = array( 'decorator2' )
237
		 *
238
		 * This would add the decorator named "decorator2" defined by
239
		 * "\Aimeos\Admin\Jsonadm\Product\Decorator\Decorator2" only to the
240
		 * "product" Jsonadm client.
241
		 *
242
		 * @param array List of decorator names
243
		 * @since 2016.01
244
		 * @category Developer
245
		 * @see admin/jsonadm/common/decorators/default
246
		 * @see admin/jsonadm/decorators/excludes
247
		 * @see admin/jsonadm/decorators/global
248
		 */
249
250
		$client = self::addClientDecorators( $client, $context, $path );
251
252
		return $client->setAimeos( $aimeos )->setView( $view );
253
	}
254
}
255