Passed
Push — master ( 49504e...c3bcee )
by Aimeos
02:30
created

Factory::create()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 134
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 134
rs 9.9
cc 4
nc 5
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), 2021
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
namespace Aimeos\Client\JsonApi\Site;
12
13
14
/**
15
 * Factory for site 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 site 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, string $path, string $name = null ) : \Aimeos\Client\JsonApi\Iface
34
	{
35
		if( ctype_alnum( $path ) === false ) {
36
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid client "%1$s"', $path ), 400 );
37
		}
38
39
		/** client/jsonapi/site/name
40
		 * Class name of the used site client implementation
41
		 *
42
		 * Each default JSON API client can be replace by an alternative imlementation.
43
		 * To use this implementation, you have to set the last part of the class
44
		 * name as configuration value so the client factory knows which class it
45
		 * has to instantiate.
46
		 *
47
		 * For example, if the name of the default class is
48
		 *
49
		 *  \Aimeos\Client\JsonApi\Site\Standard
50
		 *
51
		 * and you want to replace it with your own version named
52
		 *
53
		 *  \Aimeos\Client\JsonApi\Site\Mysite
54
		 *
55
		 * then you have to set the this configuration option:
56
		 *
57
		 *  client/jsonapi/site/name = Mysite
58
		 *
59
		 * The value is the last part of your own class name and it's case sensitive,
60
		 * so take care that the configuration value is exactly named like the last
61
		 * part of the class name.
62
		 *
63
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
64
		 * characters are possible! You should always start the last part of the class
65
		 * name with an upper case character and continue only with lower case characters
66
		 * or numbers. Avoid chamel case names like "MySite"!
67
		 *
68
		 * @param string Last part of the class name
69
		 * @since 2021.04
70
		 * @category Developer
71
		 */
72
		if( $name === null ) {
73
			$name = $context->getConfig()->get( 'client/jsonapi/site/name', 'Standard' );
74
		}
75
76
		$iface = '\\Aimeos\\Client\\JsonApi\\Iface';
77
		$classname = '\\Aimeos\\Client\\JsonApi\\Site\\' . $name;
78
79
		if( ctype_alnum( $name ) === false ) {
80
			throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
81
		}
82
83
		$client = self::createClient( $classname, $iface, $context, $path );
84
85
86
		/** client/jsonapi/site/decorators/excludes
87
		 * Excludes decorators added by the "common" option from the JSON API clients
88
		 *
89
		 * Decorators extend the functionality of a class by adding new aspects
90
		 * (e.g. log what is currently done), executing the methods of the underlying
91
		 * class only in certain conditions (e.g. only for logged in users) or
92
		 * modify what is returned to the caller.
93
		 *
94
		 * This option allows you to remove a decorator added via
95
		 * "client/jsonapi/common/decorators/default" before they are wrapped
96
		 * around the JsonApi client.
97
		 *
98
		 *  client/jsonapi/decorators/excludes = array( 'decorator1' )
99
		 *
100
		 * This would remove the decorator named "decorator1" from the list of
101
		 * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via
102
		 * "client/jsonapi/common/decorators/default" for the JSON API client.
103
		 *
104
		 * @param array List of decorator names
105
		 * @since 2021.04
106
		 * @category Developer
107
		 * @see client/jsonapi/common/decorators/default
108
		 * @see client/jsonapi/site/decorators/global
109
		 * @see client/jsonapi/site/decorators/local
110
		 */
111
112
		/** client/jsonapi/site/decorators/global
113
		 * Adds a list of globally available decorators only to the JsonApi client
114
		 *
115
		 * Decorators extend the functionality of a class by adding new aspects
116
		 * (e.g. log what is currently done), executing the methods of the underlying
117
		 * class only in certain conditions (e.g. only for logged in users) or
118
		 * modify what is returned to the caller.
119
		 *
120
		 * This option allows you to wrap global decorators
121
		 * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi
122
		 * client.
123
		 *
124
		 *  client/jsonapi/site/decorators/global = array( 'decorator1' )
125
		 *
126
		 * This would add the decorator named "decorator1" defined by
127
		 * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the
128
		 * "site" JsonApi client.
129
		 *
130
		 * @param array List of decorator names
131
		 * @since 2021.04
132
		 * @category Developer
133
		 * @see client/jsonapi/common/decorators/default
134
		 * @see client/jsonapi/site/decorators/excludes
135
		 * @see client/jsonapi/site/decorators/local
136
		 */
137
138
		/** client/jsonapi/site/decorators/local
139
		 * Adds a list of local decorators only to the JsonApi client
140
		 *
141
		 * Decorators extend the functionality of a class by adding new aspects
142
		 * (e.g. log what is currently done), executing the methods of the underlying
143
		 * class only in certain conditions (e.g. only for logged in users) or
144
		 * modify what is returned to the caller.
145
		 *
146
		 * This option allows you to wrap local decorators
147
		 * ("\Aimeos\Client\JsonApi\Site\Decorator\*") around the JsonApi
148
		 * client.
149
		 *
150
		 *  client/jsonapi/site/decorators/local = array( 'decorator2' )
151
		 *
152
		 * This would add the decorator named "decorator2" defined by
153
		 * "\Aimeos\Client\JsonApi\Site\Decorator\Decorator2" only to the
154
		 * "site" JsonApi client.
155
		 *
156
		 * @param array List of decorator names
157
		 * @since 2021.04
158
		 * @category Developer
159
		 * @see client/jsonapi/common/decorators/default
160
		 * @see client/jsonapi/site/decorators/excludes
161
		 * @see client/jsonapi/site/decorators/global
162
		 */
163
164
		$client = self::addClientDecorators( $client, $context, $path );
165
166
		return $client->setView( $context->getView() );
0 ignored issues
show
Bug introduced by
The method setView() does not exist on Aimeos\Client\JsonApi\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Client\JsonApi\Common\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
		return $client->/** @scrutinizer ignore-call */ setView( $context->getView() );
Loading history...
167
	}
168
169
}
170