Completed
Push — master ( 0acc31...830070 )
by Aimeos
02:31
created

JsonApi::createRoot()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 132

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 132
rs 8
c 0
b 0
f 0
cc 4
nc 6
nop 3

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), 2017-2018
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client;
12
13
14
/**
15
 * Factory which can create all JSON API clients
16
 *
17
 * @package Client
18
 * @subpackage JsonApi
19
 */
20
class JsonApi extends \Aimeos\Client\JsonApi\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\Client\JsonApi\Product\Standard or a path of names to
27
	 * retrieve a specific sub-client, e.g. "product/type" for the
28
	 * \Aimeos\Client\JsonApi\Product\Type\Standard client.
29
	 *
30
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
31
	 * @param string $path Name of the client separated by slashes, e.g "order/base"
32
	 * @param string|null $name Name of the client implementation ("Standard" if null)
33
	 * @return \Aimeos\Client\JsonApi\Iface JSON client instance
34
	 * @throws \Aimeos\Client\JsonApi\Exception If the given path is invalid
35
	 */
36
	static public function create( \Aimeos\MShop\Context\Item\Iface $context, $path, $name = null )
37
	{
38
		if( empty( $path ) ) {
39
			return self::createRoot( $context, $path, $name );
40
		}
41
42
		$parts = explode( '/', $path );
43
44
		foreach( $parts as $key => $part )
45
		{
46
			if( ctype_alnum( $part ) === false )
47
			{
48
				$msg = sprintf( 'Invalid client "%1$s"', $path );
49
				throw new \Aimeos\Client\JsonApi\Exception( $msg, 400 );
50
			}
51
52
			$parts[$key] = ucfirst( $part );
53
		}
54
55
		$factory = '\\Aimeos\\Client\\JsonApi\\' . join( '\\', $parts ) . '\\Factory';
56
57
		if( class_exists( $factory ) === true )
58
		{
59
			if( ( $client = @call_user_func_array( [$factory, 'create'], [$context, $path, $name] ) ) === false ) {
60
				throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid factory "%1$s"', $factory ), 400 );
61
			}
62
		}
63
		else
64
		{
65
			$client = self::createRoot( $context, $path, $name );
66
		}
67
68
		return $client;
69
	}
70
71
72
	/**
73
	 * Creates the top level client
74
	 *
75
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
76
	 * @param string $path Name of the client separated by slashes, e.g "order/base"
77
	 * @param string|null $name Name of the JsonApi client (default: "Standard")
78
	 * @return \Aimeos\Client\JsonApi\Iface JSON client instance
79
	 * @throws \Aimeos\Client\JsonApi\Exception If the client couldn't be created
80
	 */
81
	protected static function createRoot( \Aimeos\MShop\Context\Item\Iface $context, $path, $name = null )
82
	{
83
		/** client/jsonapi/name
84
		 * Class name of the used JSON API client implementation
85
		 *
86
		 * Each default JSON API client can be replace by an alternative imlementation.
87
		 * To use this implementation, you have to set the last part of the class
88
		 * name as configuration value so the client factory knows which class it
89
		 * has to instantiate.
90
		 *
91
		 * For example, if the name of the default class is
92
		 *
93
		 *  \Aimeos\Client\JsonApi\Standard
94
		 *
95
		 * and you want to replace it with your own version named
96
		 *
97
		 *  \Aimeos\Client\JsonApi\Mycntl
98
		 *
99
		 * then you have to set the this configuration option:
100
		 *
101
		 *  client/jsonapi/name = Mycntl
102
		 *
103
		 * The value is the last part of your own class name and it's case sensitive,
104
		 * so take care that the configuration value is exactly named like the last
105
		 * part of the class name.
106
		 *
107
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
108
		 * characters are possible! You should always start the last part of the class
109
		 * name with an upper case character and continue only with lower case characters
110
		 * or numbers. Avoid chamel case names like "MyCntl"!
111
		 *
112
		 * @param string Last part of the class name
113
		 * @since 2015.12
114
		 * @category Developer
115
		 */
116
		if( $name === null ) {
117
			$name = $context->getConfig()->get( 'client/jsonapi/name', 'Standard' );
118
		}
119
120
		if( ctype_alnum( $name ) === false )
121
		{
122
			$classname = is_string( $name ) ? '\\Aimeos\\Client\\JsonApi\\' . $name : '<not a string>';
123
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
124
		}
125
126
		$iface = '\\Aimeos\\Client\\JsonApi\\Iface';
127
		$classname = '\\Aimeos\\Client\\JsonApi\\' . $name;
128
129
		$client = self::createClient( $classname, $iface, $context, $path );
130
131
		/** client/jsonapi/decorators/excludes
132
		 * Excludes decorators added by the "common" option from the JSON API clients
133
		 *
134
		 * Decorators extend the functionality of a class by adding new aspects
135
		 * (e.g. log what is currently done), executing the methods of the underlying
136
		 * class only in certain conditions (e.g. only for logged in users) or
137
		 * modify what is returned to the caller.
138
		 *
139
		 * This option allows you to remove a decorator added via
140
		 * "client/jsonapi/common/decorators/default" before they are wrapped
141
		 * around the Jsonadm client.
142
		 *
143
		 *  client/jsonapi/decorators/excludes = array( 'decorator1' )
144
		 *
145
		 * This would remove the decorator named "decorator1" from the list of
146
		 * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via
147
		 * "client/jsonapi/common/decorators/default" for the JSON API client.
148
		 *
149
		 * @param array List of decorator names
150
		 * @since 2016.01
151
		 * @category Developer
152
		 * @see client/jsonapi/common/decorators/default
153
		 * @see client/jsonapi/decorators/global
154
		 * @see client/jsonapi/decorators/local
155
		 */
156
157
		/** client/jsonapi/decorators/global
158
		 * Adds a list of globally available decorators only to the Jsonadm client
159
		 *
160
		 * Decorators extend the functionality of a class by adding new aspects
161
		 * (e.g. log what is currently done), executing the methods of the underlying
162
		 * class only in certain conditions (e.g. only for logged in users) or
163
		 * modify what is returned to the caller.
164
		 *
165
		 * This option allows you to wrap global decorators
166
		 * ("\Aimeos\Client\Jsonadm\Common\Decorator\*") around the Jsonadm
167
		 * client.
168
		 *
169
		 *  client/jsonapi/product/decorators/global = array( 'decorator1' )
170
		 *
171
		 * This would add the decorator named "decorator1" defined by
172
		 * "\Aimeos\Client\Jsonadm\Common\Decorator\Decorator1" only to the
173
		 * "product" Jsonadm client.
174
		 *
175
		 * @param array List of decorator names
176
		 * @since 2016.01
177
		 * @category Developer
178
		 * @see client/jsonapi/common/decorators/default
179
		 * @see client/jsonapi/decorators/excludes
180
		 * @see client/jsonapi/decorators/local
181
		 */
182
183
		/** client/jsonapi/decorators/local
184
		 * Adds a list of local decorators only to the Jsonadm client
185
		 *
186
		 * Decorators extend the functionality of a class by adding new aspects
187
		 * (e.g. log what is currently done), executing the methods of the underlying
188
		 * class only in certain conditions (e.g. only for logged in users) or
189
		 * modify what is returned to the caller.
190
		 *
191
		 * This option allows you to wrap local decorators
192
		 * ("\Aimeos\Client\Jsonadm\Product\Decorator\*") around the Jsonadm
193
		 * client.
194
		 *
195
		 *  client/jsonapi/product/decorators/local = array( 'decorator2' )
196
		 *
197
		 * This would add the decorator named "decorator2" defined by
198
		 * "\Aimeos\Client\Jsonadm\Product\Decorator\Decorator2" only to the
199
		 * "product" Jsonadm client.
200
		 *
201
		 * @param array List of decorator names
202
		 * @since 2016.01
203
		 * @category Developer
204
		 * @see client/jsonapi/common/decorators/default
205
		 * @see client/jsonapi/decorators/excludes
206
		 * @see client/jsonapi/decorators/global
207
		 */
208
209
		$client = self::addClientDecorators( $client, $context, $path );
210
211
		return $client->setView( $context->getView() );
212
	}
213
}
214