Completed
Push — master ( 00af66...7df475 )
by Aimeos
02:30
created

Factory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 272
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 15 3
A createClient() 0 17 4
A setCache() 0 7 1
B createClientNew() 0 35 5
B createClientRoot() 0 131 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm;
12
13
14
/**
15
 * Factory which can create all JSON API clients
16
 *
17
 * @package Admin
18
 * @subpackage JsonAdm
19
 */
20
class Factory
21
	extends \Aimeos\Admin\JsonAdm\Common\Factory\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Admin\JsonAdm\Common\Factory\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	static private $cache = true;
25
	static private $clients = array();
26
27
28
	/**
29
	 * Removes the client objects from the cache.
30
	 *
31
	 * If neither a context ID nor a path is given, the complete cache will be pruned.
32
	 *
33
	 * @param integer $id Context ID the objects have been created with (string of \Aimeos\MShop\Context\Item\Iface)
34
	 * @param string $path Path describing the client to clear, e.g. "product/lists/type"
35
	 */
36
	static public function clear( $id = null, $path = null )
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
37
	{
38
		if( $id !== null )
39
		{
40
			if( $path !== null ) {
41
				self::$clients[$id][$path] = null;
42
			} else {
43
				self::$clients[$id] = array();
44
			}
45
46
			return;
47
		}
48
49
		self::$clients = array();
50
	}
51
52
53
	/**
54
	 * Creates the required client specified by the given path of client names.
55
	 *
56
	 * Clients are created by providing only the domain name, e.g. "product"
57
	 *  for the \Aimeos\Admin\JsonAdm\Product\Standard or a path of names to
58
	 * retrieve a specific sub-client, e.g. "product/type" for the
59
	 * \Aimeos\Admin\JsonAdm\Product\Type\Standard client.
60
	 *
61
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
62
	 * @param array $templatePaths List of file system paths where the templates are stored
63
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
64
	 * @param string|null $name Name of the client implementation ("Standard" if null)
65
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
66
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the given path is invalid
67
	 */
68
	static public function createClient( \Aimeos\MShop\Context\Item\Iface $context,
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
69
		array $templatePaths, $path, $name = null )
70
	{
71
		$path = strtolower( trim( $path, "/ \n\t\r\0\x0B" ) );
72
73
		if( empty( $path ) ) {
74
			return self::createClientRoot( $context, $context->getView(), $templatePaths, $path, $name );
75
		}
76
77
		$id = (string) $context;
78
79
		if( self::$cache === false || !isset( self::$clients[$id][$path] ) ) {
80
			self::$clients[$id][$path] = self::createClientNew( $context, $templatePaths, $path, $name );
81
		}
82
83
		return self::$clients[$id][$path];
84
	}
85
86
87
	/**
88
	 * Enables or disables caching of class instances.
89
	 *
90
	 * @param boolean $value True to enable caching, false to disable it.
91
	 * @return boolean Previous cache setting
92
	 */
93
	static public function setCache( $value )
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
94
	{
95
		$old = self::$cache;
96
		self::$cache = (boolean) $value;
97
98
		return $old;
99
	}
100
101
102
	/**
103
	 * Creates a new client specified by the given path of client names.
104
	 *
105
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
106
	 * @param array $templatePaths List of file system paths where the templates are stored
107
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
108
	 * @param string|null $name Name of the client implementation ("Standard" if null)
109
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
110
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the given path is invalid
111
	 */
112
	protected static function createClientNew( \Aimeos\MShop\Context\Item\Iface $context,
113
		array $templatePaths, $path, $name )
114
	{
115
		$parts = explode( '/', $path );
116
117
		foreach( $parts as $key => $part )
118
		{
119
			if( ctype_alnum( $part ) === false )
120
			{
121
				$msg = sprintf( 'Invalid client "%1$s" in "%2$s"', $part, $path );
122
				throw new \Aimeos\Admin\JsonAdm\Exception( $msg, 400 );
123
			}
124
125
			$parts[$key] = ucwords( $part );
126
		}
127
128
129
		$view = $context->getView();
130
		$factory = '\\Aimeos\\Admin\\JsonAdm\\' . join( '\\', $parts ) . '\\Factory';
131
132
		if( class_exists( $factory ) === true )
133
		{
134
			$args = array( $context, $view, $templatePaths, $path, $name );
135
136
			if( ( $client = @call_user_func_array( array( $factory, 'createClient' ), $args ) ) === false ) {
137
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid factory "%1$s"', $factory ), 400 );
138
			}
139
		}
140
		else
141
		{
142
			$client = self::createClientRoot( $context, $view, $templatePaths, $path, $name );
143
		}
144
145
		return $client;
146
	}
147
148
149
	/**
150
	 * Creates the top level client
151
	 *
152
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object required by clients
153
	 * @param \Aimeos\MW\View\Iface $view View object
154
	 * @param array $templatePaths List of file system paths where the templates are stored
155
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
156
	 * @param string|null $name Name of the JsonAdm client (default: "Standard")
157
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
158
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the client couldn't be created
159
	 */
160
	protected static function createClientRoot( \Aimeos\MShop\Context\Item\Iface $context,
161
		\Aimeos\MW\View\Iface $view, array $templatePaths, $path, $name = null )
162
	{
163
		/** admin/jsonadm/name
164
		 * Class name of the used JSON API client implementation
165
		 *
166
		 * Each default JSON API client can be replace by an alternative imlementation.
167
		 * To use this implementation, you have to set the last part of the class
168
		 * name as configuration value so the client factory knows which class it
169
		 * has to instantiate.
170
		 *
171
		 * For example, if the name of the default class is
172
		 *
173
		 *  \Aimeos\Admin\JsonAdm\Standard
174
		 *
175
		 * and you want to replace it with your own version named
176
		 *
177
		 *  \Aimeos\Admin\JsonAdm\Mycntl
178
		 *
179
		 * then you have to set the this configuration option:
180
		 *
181
		 *  admin/jsonadm/name = Mycntl
182
		 *
183
		 * The value is the last part of your own class name and it's case sensitive,
184
		 * so take care that the configuration value is exactly named like the last
185
		 * part of the class name.
186
		 *
187
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
188
		 * characters are possible! You should always start the last part of the class
189
		 * name with an upper case character and continue only with lower case characters
190
		 * or numbers. Avoid chamel case names like "MyCntl"!
191
		 *
192
		 * @param string Last part of the class name
193
		 * @since 2015.12
194
		 * @category Developer
195
		 */
196
		if( $name === null ) {
197
			$name = $context->getConfig()->get( 'admin/jsonadm/name', 'Standard' );
198
		}
199
200
		if( ctype_alnum( $name ) === false )
201
		{
202
			$classname = is_string( $name ) ? '\\Aimeos\\Admin\\JsonAdm\\' . $name : '<not a string>';
203
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
204
		}
205
206
		$iface = '\\Aimeos\\Admin\\JsonAdm\\Iface';
207
		$classname = '\\Aimeos\\Admin\\JsonAdm\\' . $name;
208
209
		$client = self::createClientBase( $classname, $iface, $context, $view, $templatePaths, $path );
210
211
		/** admin/jsonadm/decorators/excludes
212
		 * Excludes decorators added by the "common" option from the JSON API clients
213
		 *
214
		 * Decorators extend the functionality of a class by adding new aspects
215
		 * (e.g. log what is currently done), executing the methods of the underlying
216
		 * class only in certain conditions (e.g. only for logged in users) or
217
		 * modify what is returned to the caller.
218
		 *
219
		 * This option allows you to remove a decorator added via
220
		 * "admin/jsonadm/common/decorators/default" before they are wrapped
221
		 * around the Jsonadm client.
222
		 *
223
		 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
224
		 *
225
		 * This would remove the decorator named "decorator1" from the list of
226
		 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
227
		 * "admin/jsonadm/common/decorators/default" for the JSON API client.
228
		 *
229
		 * @param array List of decorator names
230
		 * @since 2016.01
231
		 * @category Developer
232
		 * @see admin/jsonadm/common/decorators/default
233
		 * @see admin/jsonadm/decorators/global
234
		 * @see admin/jsonadm/decorators/local
235
		 */
236
237
		/** admin/jsonadm/decorators/global
238
		 * Adds a list of globally available decorators only to the Jsonadm client
239
		 *
240
		 * Decorators extend the functionality of a class by adding new aspects
241
		 * (e.g. log what is currently done), executing the methods of the underlying
242
		 * class only in certain conditions (e.g. only for logged in users) or
243
		 * modify what is returned to the caller.
244
		 *
245
		 * This option allows you to wrap global decorators
246
		 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
247
		 * client.
248
		 *
249
		 *  admin/jsonadm/product/decorators/global = array( 'decorator1' )
250
		 *
251
		 * This would add the decorator named "decorator1" defined by
252
		 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
253
		 * "product" Jsonadm client.
254
		 *
255
		 * @param array List of decorator names
256
		 * @since 2016.01
257
		 * @category Developer
258
		 * @see admin/jsonadm/common/decorators/default
259
		 * @see admin/jsonadm/decorators/excludes
260
		 * @see admin/jsonadm/decorators/local
261
		 */
262
263
		/** admin/jsonadm/decorators/local
264
		 * Adds a list of local decorators only to the Jsonadm client
265
		 *
266
		 * Decorators extend the functionality of a class by adding new aspects
267
		 * (e.g. log what is currently done), executing the methods of the underlying
268
		 * class only in certain conditions (e.g. only for logged in users) or
269
		 * modify what is returned to the caller.
270
		 *
271
		 * This option allows you to wrap local decorators
272
		 * ("\Aimeos\Admin\Jsonadm\Product\Decorator\*") around the Jsonadm
273
		 * client.
274
		 *
275
		 *  admin/jsonadm/product/decorators/local = array( 'decorator2' )
276
		 *
277
		 * This would add the decorator named "decorator2" defined by
278
		 * "\Aimeos\Admin\Jsonadm\Product\Decorator\Decorator2" only to the
279
		 * "product" Jsonadm client.
280
		 *
281
		 * @param array List of decorator names
282
		 * @since 2016.01
283
		 * @category Developer
284
		 * @see admin/jsonadm/common/decorators/default
285
		 * @see admin/jsonadm/decorators/excludes
286
		 * @see admin/jsonadm/decorators/global
287
		 */
288
289
		return self::addClientDecorators( $client, $context, $view, $templatePaths, $path );
290
	}
291
}
292