Passed
Push — master ( 5e8389...a4f92b )
by Aimeos
23:13 queued 15:41
created

JsonAdm::addComponentDecorators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 48
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
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
21
{
22
	/** admin/jsonadm/name
23
	 * Class name of the used JSON API client implementation
24
	 *
25
	 * Each default JSON API client can be replace by an alternative imlementation.
26
	 * To use this implementation, you have to set the last part of the class
27
	 * name as configuration value so the client factory knows which class it
28
	 * has to instantiate.
29
	 *
30
	 * For example, if the name of the default class is
31
	 *
32
	 *  \Aimeos\Admin\JsonAdm\Standard
33
	 *
34
	 * and you want to replace it with your own version named
35
	 *
36
	 *  \Aimeos\Admin\JsonAdm\Mycntl
37
	 *
38
	 * then you have to set the this configuration option:
39
	 *
40
	 *  admin/jsonadm/name = Mycntl
41
	 *
42
	 * The value is the last part of your own class name and it's case sensitive,
43
	 * so take care that the configuration value is exactly named like the last
44
	 * part of the class name.
45
	 *
46
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
47
	 * characters are possible! You should always start the last part of the class
48
	 * name with an upper case character and continue only with lower case characters
49
	 * or numbers. Avoid chamel case names like "MyCntl"!
50
	 *
51
	 * @param string Last part of the class name
52
	 * @since 2015.12
53
	 * @category Developer
54
	 */
55
56
	/** admin/jsonadm/decorators/excludes
57
	 * Excludes decorators added by the "common" option from the JSON API clients
58
	 *
59
	 * Decorators extend the functionality of a class by adding new aspects
60
	 * (e.g. log what is currently done), executing the methods of the underlying
61
	 * class only in certain conditions (e.g. only for logged in users) or
62
	 * modify what is returned to the caller.
63
	 *
64
	 * This option allows you to remove a decorator added via
65
	 * "admin/jsonadm/common/decorators/default" before they are wrapped
66
	 * around the Jsonadm client.
67
	 *
68
	 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
69
	 *
70
	 * This would remove the decorator named "decorator1" from the list of
71
	 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
72
	 * "admin/jsonadm/common/decorators/default" for the JSON API client.
73
	 *
74
	 * @param array List of decorator names
75
	 * @since 2016.01
76
	 * @category Developer
77
	 * @see admin/jsonadm/common/decorators/default
78
	 * @see admin/jsonadm/decorators/global
79
	 * @see admin/jsonadm/decorators/local
80
	 */
81
82
	/** admin/jsonadm/decorators/global
83
	 * Adds a list of globally available decorators only to the Jsonadm client
84
	 *
85
	 * Decorators extend the functionality of a class by adding new aspects
86
	 * (e.g. log what is currently done), executing the methods of the underlying
87
	 * class only in certain conditions (e.g. only for logged in users) or
88
	 * modify what is returned to the caller.
89
	 *
90
	 * This option allows you to wrap global decorators
91
	 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
92
	 * client.
93
	 *
94
	 *  admin/jsonadm/product/decorators/global = array( 'decorator1' )
95
	 *
96
	 * This would add the decorator named "decorator1" defined by
97
	 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
98
	 * "product" Jsonadm client.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2016.01
102
	 * @category Developer
103
	 * @see admin/jsonadm/common/decorators/default
104
	 * @see admin/jsonadm/decorators/excludes
105
	 * @see admin/jsonadm/decorators/local
106
	 */
107
108
	/** admin/jsonadm/decorators/local
109
	 * Adds a list of local decorators only to the Jsonadm client
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Admin\Jsonadm\Product\Decorator\*") around the Jsonadm
118
	 * client.
119
	 *
120
	 *  admin/jsonadm/product/decorators/local = array( 'decorator2' )
121
	 *
122
	 * This would add the decorator named "decorator2" defined by
123
	 * "\Aimeos\Admin\Jsonadm\Product\Decorator\Decorator2" only to the
124
	 * "product" Jsonadm client.
125
	 *
126
	 * @param array List of decorator names
127
	 * @since 2016.01
128
	 * @category Developer
129
	 * @see admin/jsonadm/common/decorators/default
130
	 * @see admin/jsonadm/decorators/excludes
131
	 * @see admin/jsonadm/decorators/global
132
	 */
133
134
135
	private static $objects = [];
136
137
138
	/**
139
	 * Creates the required client specified by the given path of client names.
140
	 *
141
	 * Clients are created by providing only the domain name, e.g. "product"
142
	 *  for the \Aimeos\Admin\JsonAdm\Product\Standard or a path of names to
143
	 * retrieve a specific sub-client, e.g. "product/type" for the
144
	 * \Aimeos\Admin\JsonAdm\Product\Type\Standard client.
145
	 *
146
	 * @param \Aimeos\MShop\ContextIface $context Context object required by clients
147
	 * @param \Aimeos\Bootstrap $aimeos Aimeos Bootstrap object
148
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
149
	 * @param string|null $name Name of the client implementation ("Standard" if null)
150
	 * @return \Aimeos\Admin\JsonAdm\Iface JSON admin instance
151
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the given path is invalid
152
	 */
153
	public static function create( \Aimeos\MShop\ContextIface $context,
154
		\Aimeos\Bootstrap $aimeos, string $path, string $name = null ) : \Aimeos\Admin\JsonAdm\Iface
155
	{
156
		if( preg_match( '/^[a-z0-9\/]+$/', $path ) !== 1 ) {
157
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid component path "%1$s"', $path, 404 ) );
158
		}
159
160
		empty( $path = trim( $path, '/' ) ) ?: $path .= '/';
161
162
		$view = $context->view();
163
		$config = $context->config();
164
165
		if( $view->access( $config->get( 'admin/jsonadm/resource/' . $path . 'groups', [] ) ) !== true ) {
166
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Not allowed to access JsonAdm "%1$s" client', $path ), 403 );
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...
167
		}
168
169
		$cname = $name ?: $context->config()->get( 'admin/jsonadm/' . $path . 'name', 'Standard' );
170
171
		$classname = '\\Aimeos\\Admin\\JsonAdm\\' . str_replace( '/', '\\', ucwords( $path, '/' ) ) . $cname;
172
		$interface = '\\Aimeos\\Admin\\JsonAdm\\Iface';
173
174
		if( class_exists( $classname ) === false )
175
		{
176
			$cname = $name ?: $context->config()->get( 'admin/jsonadm/name', 'Standard' );
177
			$classname = '\\Aimeos\\Admin\\JsonAdm\\' . $cname;
178
179
			if( class_exists( $classname ) === false ) {
180
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname, 404 ) );
181
			}
182
		}
183
184
		$client = self::createComponent( $context, $classname, $interface, $path );
185
		$client = self::addComponentDecorators( $context, $client, $path );
186
187
		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

187
		return $client->/** @scrutinizer ignore-call */ setAimeos( $aimeos )->setView( $view );
Loading history...
188
	}
189
190
191
	/**
192
	 * Injects a client object
193
	 *
194
	 * The object is returned via create() if an instance of the class
195
	 * with the name name is requested.
196
	 *
197
	 * @param string $classname Full name of the class for which the object should be returned
198
	 * @param \Aimeos\Admin\JsonAdm\Iface|null $client JSON API client object
199
	 */
200
	public static function inject( string $classname, \Aimeos\Admin\JsonAdm\Iface $client = null )
201
	{
202
		self::$objects['\\' . ltrim( $classname, '\\' )] = $client;
203
	}
204
205
206
	/**
207
	 * Adds the decorators to the JSON API client object
208
	 *
209
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
210
	 * @param \Aimeos\Admin\JsonAdm\Common\Iface $client Client object
211
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
212
	 * @return \Aimeos\Admin\JsonAdm\Iface Client object
213
	 */
214
	protected static function addComponentDecorators( \Aimeos\MShop\ContextIface $context,
215
		\Aimeos\Admin\JsonAdm\Iface $client, string $path ) : \Aimeos\Admin\JsonAdm\Iface
216
	{
217
		$localClass = str_replace( '/', '\\', ucwords( $path, '/' ) );
218
		$config = $context->config();
219
220
		/** admin/jsonadm/common/decorators/default
221
		 * Configures the list of decorators applied to all JSON API clients
222
		 *
223
		 * Decorators extend the functionality of a class by adding new aspects
224
		 * (e.g. log what is currently done), executing the methods of the underlying
225
		 * class only in certain conditions (e.g. only for logged in users) or
226
		 * modify what is returned to the caller.
227
		 *
228
		 * This option allows you to configure a list of decorator names that should
229
		 * be wrapped around the original instance of all created clients:
230
		 *
231
		 *  admin/jsonadm/common/decorators/default = array( 'decorator1', 'decorator2' )
232
		 *
233
		 * This would wrap the decorators named "decorator1" and "decorator2" around
234
		 * all client instances in that order. The decorator classes would be
235
		 * "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator1" and
236
		 * "\Aimeos\Admin\JsonAdm\Common\Decorator\Decorator2".
237
		 *
238
		 * @param array List of decorator names
239
		 * @since 2015.12
240
		 * @category Developer
241
		 */
242
		$decorators = $config->get( 'admin/jsonadm/common/decorators/default', [] );
243
		$excludes = $config->get( 'admin/jsonadm/' . $path . 'decorators/excludes', [] );
244
245
		foreach( $decorators as $key => $name )
246
		{
247
			if( in_array( $name, $excludes ) ) {
248
				unset( $decorators[$key] );
249
			}
250
		}
251
252
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
253
		$client = self::addDecorators( $context, $client, $path, $decorators, $classprefix );
254
255
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
256
		$decorators = $config->get( 'admin/jsonadm/' . $path . 'decorators/global', [] );
257
		$client = self::addDecorators( $context, $client, $path, $decorators, $classprefix );
258
259
		$classprefix = '\\Aimeos\\Admin\\JsonAdm\\' . ucfirst( $localClass ) . 'Decorator\\';
260
		$decorators = $config->get( 'admin/jsonadm/' . $path . 'decorators/local', [] );
261
		return self::addDecorators( $context, $client, $path, $decorators, $classprefix );
262
	}
263
264
265
	/**
266
	 * Adds the decorators to the client object
267
	 *
268
	 * @param \Aimeos\MShop\ContextIface $context Context instance with necessary objects
269
	 * @param \Aimeos\Admin\JsonAdm\Iface $client Client object
270
	 * @param array $decorators List of decorator names
271
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JsonAdm\Product\Decorator\"
272
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
273
	 * @return \Aimeos\Admin\JsonAdm\Iface Client object
274
	 */
275
	protected static function addDecorators( \Aimeos\MShop\ContextIface $context, \Aimeos\Admin\JsonAdm\Iface $client,
276
		string $path, array $decorators, string $classprefix ) : \Aimeos\Admin\JsonAdm\Iface
277
	{
278
		foreach( $decorators as $name )
279
		{
280
			if( ctype_alnum( $name ) === false )
281
			{
282
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
283
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 404 );
284
			}
285
286
			$classname = $classprefix . $name;
287
288
			if( class_exists( $classname ) === false ) {
289
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
290
			}
291
292
			$interface = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\Iface';
293
			$client = new $classname( $client, $context, $path );
294
295
			if( !( $client instanceof $interface ) )
296
			{
297
				$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
298
				throw new \Aimeos\Admin\JsonAdm\Exception( $msg, 400 );
299
			}
300
		}
301
302
		return $client;
303
	}
304
305
306
	/**
307
	 * Creates a new client object
308
	 *
309
	 * @param \Aimeos\MShop\ContextIface $context Context object
310
	 * @param string $classname Name of the client class
311
	 * @param string $interface Name of the client interface
312
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
313
	 * @return \Aimeos\Admin\JsonAdm\Common\Iface Client object
314
	 */
315
	protected static function createComponent( \Aimeos\MShop\ContextIface $context,
316
		string $classname, string $interface, string $path ) : \Aimeos\Admin\JsonAdm\Common\Iface
317
	{
318
		if( isset( self::$objects[$classname] ) ) {
319
			return self::$objects[$classname];
320
		}
321
322
		if( class_exists( $classname ) === false ) {
323
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
324
		}
325
326
		$client = new $classname( $context, trim( $path, '/' ) );
327
328
		if( !( $client instanceof $interface ) )
329
		{
330
			$msg = sprintf( 'Class "%1$s" does not implement "%2$s"', $classname, $interface );
331
			throw new \Aimeos\Admin\JsonAdm\Exception( $msg, 400 );
332
		}
333
334
		return $client;
335
	}
336
}
337