Completed
Push — master ( e1a749...2c8187 )
by Aimeos
01:48
created

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 154
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 139 6
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\JsonApi\Attribute;
12
13
14
/**
15
 * Factory for attribute JSON API client
16
 *
17
 * @package Client
18
 * @subpackage JsonApi
19
 */
20
class Factory
21
	extends \Aimeos\Client\JsonApi\Common\Factory\Base
22
	implements \Aimeos\Client\JsonApi\Common\Factory\Iface
23
{
24
	/**
25
	 * Creates a attribute client object.
26
	 *
27
	 * @param \Aimeos\MShop\Context\Item\Iface $context Shop context instance with necessary objects
28
	 * @param string $path Name of the client separated by slashes, e.g "product"
29
	 * @param string|null $name Client name (default: "Standard")
30
	 * @return \Aimeos\Client\JsonApi\Iface JSON API client
31
	 * @throws \Aimeos\Client\JsonApi\Exception If requested client implementation couldn't be found or initialisation fails
32
	 */
33
	public static function create( \Aimeos\MShop\Context\Item\Iface $context, $path, $name = null )
34
	{
35
		if( ctype_alnum( $path ) === false )
36
		{
37
			$path = ( is_string( $path ) ? $path : '<not a string>' );
38
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid client "%1$s"', $path ), 400 );
39
		}
40
41
		/** client/jsonapi/attribute/name
42
		 * Class name of the used attribute client implementation
43
		 *
44
		 * Each default JSON API client can be replace by an alternative imlementation.
45
		 * To use this implementation, you have to set the last part of the class
46
		 * name as configuration value so the client factory knows which class it
47
		 * has to instantiate.
48
		 *
49
		 * For example, if the name of the default class is
50
		 *
51
		 *  \Aimeos\Client\JsonApi\Attribute\Standard
52
		 *
53
		 * and you want to replace it with your own version named
54
		 *
55
		 *  \Aimeos\Client\JsonApi\Attribute\Myattribute
56
		 *
57
		 * then you have to set the this configuration option:
58
		 *
59
		 *  client/jsonapi/attribute/name = Myattribute
60
		 *
61
		 * The value is the last part of your own class name and it's case sensitive,
62
		 * so take care that the configuration value is exactly named like the last
63
		 * part of the class name.
64
		 *
65
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
66
		 * characters are possible! You should always start the last part of the class
67
		 * name with an upper case character and continue only with lower case characters
68
		 * or numbers. Avoid chamel case names like "MyAttribute"!
69
		 *
70
		 * @param string Last part of the class name
71
		 * @since 2017.03
72
		 * @category Developer
73
		 */
74
		if( $name === null ) {
75
			$name = $context->getConfig()->get( 'client/jsonapi/attribute/name', 'Standard' );
76
		}
77
78
		if( ctype_alnum( $name ) === false )
79
		{
80
			$classname = is_string( $name ) ? '\\Aimeos\\Client\\JsonApi\\Attribute\\' . $name : '<not a string>';
81
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
82
		}
83
84
		$iface = '\\Aimeos\\Client\\JsonApi\\Iface';
85
		$classname = '\\Aimeos\\Client\\JsonApi\\Attribute\\' . $name;
86
87
		$client = self::createClient( $classname, $iface, $context, $path );
88
89
90
		/** client/jsonapi/attribute/decorators/excludes
91
		 * Excludes decorators added by the "common" option from the JSON API clients
92
		 *
93
		 * Decorators extend the functionality of a class by adding new aspects
94
		 * (e.g. log what is currently done), executing the methods of the underlying
95
		 * class only in certain conditions (e.g. only for logged in users) or
96
		 * modify what is returned to the caller.
97
		 *
98
		 * This option allows you to remove a decorator added via
99
		 * "client/jsonapi/common/decorators/default" before they are wrapped
100
		 * around the JsonApi client.
101
		 *
102
		 *  client/jsonapi/decorators/excludes = array( 'decorator1' )
103
		 *
104
		 * This would remove the decorator named "decorator1" from the list of
105
		 * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via
106
		 * "client/jsonapi/common/decorators/default" for the JSON API client.
107
		 *
108
		 * @param array List of decorator names
109
		 * @since 2017.07
110
		 * @category Developer
111
		 * @see client/jsonapi/common/decorators/default
112
		 * @see client/jsonapi/attribute/decorators/global
113
		 * @see client/jsonapi/attribute/decorators/local
114
		 */
115
116
		/** client/jsonapi/attribute/decorators/global
117
		 * Adds a list of globally available decorators only to the JsonApi client
118
		 *
119
		 * Decorators extend the functionality of a class by adding new aspects
120
		 * (e.g. log what is currently done), executing the methods of the underlying
121
		 * class only in certain conditions (e.g. only for logged in users) or
122
		 * modify what is returned to the caller.
123
		 *
124
		 * This option allows you to wrap global decorators
125
		 * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi
126
		 * client.
127
		 *
128
		 *  client/jsonapi/attribute/decorators/global = array( 'decorator1' )
129
		 *
130
		 * This would add the decorator named "decorator1" defined by
131
		 * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the
132
		 * "attribute" JsonApi client.
133
		 *
134
		 * @param array List of decorator names
135
		 * @since 2017.07
136
		 * @category Developer
137
		 * @see client/jsonapi/common/decorators/default
138
		 * @see client/jsonapi/attribute/decorators/excludes
139
		 * @see client/jsonapi/attribute/decorators/local
140
		 */
141
142
		/** client/jsonapi/attribute/decorators/local
143
		 * Adds a list of local decorators only to the JsonApi client
144
		 *
145
		 * Decorators extend the functionality of a class by adding new aspects
146
		 * (e.g. log what is currently done), executing the methods of the underlying
147
		 * class only in certain conditions (e.g. only for logged in users) or
148
		 * modify what is returned to the caller.
149
		 *
150
		 * This option allows you to wrap local decorators
151
		 * ("\Aimeos\Client\JsonApi\Attribute\Decorator\*") around the JsonApi
152
		 * client.
153
		 *
154
		 *  client/jsonapi/attribute/decorators/local = array( 'decorator2' )
155
		 *
156
		 * This would add the decorator named "decorator2" defined by
157
		 * "\Aimeos\Client\JsonApi\Attribute\Decorator\Decorator2" only to the
158
		 * "attribute" JsonApi client.
159
		 *
160
		 * @param array List of decorator names
161
		 * @since 2017.07
162
		 * @category Developer
163
		 * @see client/jsonapi/common/decorators/default
164
		 * @see client/jsonapi/attribute/decorators/excludes
165
		 * @see client/jsonapi/attribute/decorators/global
166
		 */
167
168
		$client = self::addClientDecorators( $client, $context, $path );
169
170
		return $client->setView( $context->getView() );
171
	}
172
173
}
174
175