Completed
Push — master ( e8462d...7f5a45 )
by Aimeos
01:35
created

Base   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 155
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A injectClient() 0 4 1
B addClientDecorators() 0 63 7
A addDecorators() 0 24 5
A createAdmin() 0 16 3
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\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( $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\Common\Iface Client object
47
	 */
48
	protected static function addClientDecorators( \Aimeos\Admin\JsonAdm\Iface $client,
49
		\Aimeos\MShop\Context\Item\Iface $context, $path )
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( $path !== null && is_string( $path ) )
78
		{
79
			$dpath = trim( $path, '/' );
80
			$dpath = ( $dpath !== '' ? $dpath . '/' : $dpath );
81
82
			$excludes = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/excludes', [] );
83
			$localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) );
84
85
			foreach( $decorators as $key => $name )
86
			{
87
				if( in_array( $name, $excludes ) ) {
88
					unset( $decorators[$key] );
89
				}
90
			}
91
92
			$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
93
			$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/global', [] );
94
			$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path );
95
96
			if( !empty( $path ) )
97
			{
98
				$classprefix = '\\Aimeos\\Admin\\JsonAdm\\' . ucfirst( $localClass ) . '\\Decorator\\';
99
				$decorators = $config->get( 'admin/jsonadm/' . $dpath . 'decorators/local', [] );
100
				$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path );
101
			}
102
		}
103
		else
104
		{
105
			$classprefix = '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\';
106
			$client = self::addDecorators( $client, $decorators, $classprefix, $context, $path );
107
		}
108
109
		return $client;
110
	}
111
112
113
	/**
114
	 * Adds the decorators to the client object
115
	 *
116
	 * @param \Aimeos\Admin\JsonAdm\Iface $client Client object
117
	 * @param array $decorators List of decorator names
118
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JsonAdm\Product\Decorator\"
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context instance with necessary objects
120
	 * @param string $path Name of the client separated by slashes, e.g "product/stock"
121
	 * @return \Aimeos\Admin\JsonAdm\Common\Iface Client object
122
	 */
123
	protected static function addDecorators( \Aimeos\Admin\JsonAdm\Iface $client, array $decorators, $classprefix,
124
			\Aimeos\MShop\Context\Item\Iface $context, $path )
125
	{
126
		foreach( $decorators as $name )
127
		{
128
			if( ctype_alnum( $name ) === false )
129
			{
130
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
131
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ), 404 );
132
			}
133
134
			$classname = $classprefix . $name;
135
136
			if( class_exists( $classname ) === false ) {
137
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
138
			}
139
140
			$client = new $classname( $client, $context, $path );
141
142
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JsonAdm\\Common\\Decorator\\Iface', $client );
143
		}
144
145
		return $client;
146
	}
147
148
149
	/**
150
	 * Creates a new client object
151
	 *
152
	 * @param string $classname Name of the client class
153
	 * @param string $interface Name of the client interface
154
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
155
	 * @param string $path Name of the client separated by slashes, e.g "product/property"
156
	 * @return \Aimeos\Admin\JsonAdm\Common\Iface Client object
157
	 */
158
	protected static function createAdmin( $classname, $interface, \Aimeos\MShop\Context\Item\Iface $context, $path )
159
	{
160
		if( isset( self::$objects[$classname] ) ) {
161
			return self::$objects[$classname];
162
		}
163
164
		if( class_exists( $classname ) === false ) {
165
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ), 404 );
166
		}
167
168
		$client = new $classname( $context, $path );
169
170
		\Aimeos\MW\Common\Base::checkClass( $interface, $client );
171
172
		return $client;
173
	}
174
}
175