Passed
Pull Request — master (#73)
by
unknown
02:48
created

Factory::create()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 2
dl 0
loc 51
rs 9.9332
c 0
b 0
f 0

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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Catalog\Product;
13
14
15
/**
16
 * Factory for product part in catalog for HTML clients.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Factory
22
	extends \Aimeos\Client\Html\Common\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Factory\Iface
24
{
25
	/**
26
	 * Creates a product client object.
27
	 *
28
	 * @param \Aimeos\MShop\Context\Item\Iface $context Shop context instance with necessary objects
29
	 * @param string|null $name Client name (default: "Standard")
30
	 * @return \Aimeos\Client\Html\Iface Filter part implementing \Aimeos\Client\Html\Iface
31
	 * @throws \Aimeos\Client\Html\Exception If requested client implementation couldn't be found or initialisation fails
32
	 */
33
	public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name = null )
34
	{
35
		/** client/html/catalog/product/name
36
		 * Class name of the used catalog product client implementation
37
		 *
38
		 * Each default HTML client can be replace by an alternative imlementation.
39
		 * To use this implementation, you have to set the last part of the class
40
		 * name as configuration value so the client factory knows which class it
41
		 * has to instantiate.
42
		 *
43
		 * For example, if the name of the default class is
44
		 *
45
		 *  \Aimeos\Client\Html\Catalog\Product\Standard
46
		 *
47
		 * and you want to replace it with your own version named
48
		 *
49
		 *  \Aimeos\Client\Html\Catalog\Product\Myproduct
50
		 *
51
		 * then you have to set the this configuration option:
52
		 *
53
		 *  client/html/catalog/product/name = Myproduct
54
		 *
55
		 * The value is the last part of your own class name and it's case sensitive,
56
		 * so take care that the configuration value is exactly named like the last
57
		 * part of the class name.
58
		 *
59
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
60
		 * characters are possible! You should always start the last part of the class
61
		 * name with an upper case character and continue only with lower case characters
62
		 * or numbers. Avoid chamel case names like "MyProduct"!
63
		 *
64
		 * @param string Last part of the class name
65
		 * @since 2019.06
66
		 * @category Developer
67
		 */
68
		if( $name === null ) {
69
			$name = $context->getConfig()->get( 'client/html/catalog/product/name', 'Standard' );
70
		}
71
72
		if( ctype_alnum( $name ) === false ) {
73
			$classname = is_string( $name ) ? '\\Aimeos\\Client\\Html\\Catalog\\Product\\' . $name : '<not a string>';
74
			throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) );
75
		}
76
77
		$iface = '\\Aimeos\\Client\\Html\\Iface';
78
		$classname = '\\Aimeos\\Client\\Html\\Catalog\\Product\\' . $name;
79
80
		$client = self::createClient( $context, $classname, $iface );
81
		$client = self::addClientDecorators( $context, $client, 'catalog/product' );
82
83
		return $client->setObject( $client );
84
	}
85
}
86