Passed
Push — master ( 04e202...9b7189 )
by Aimeos
05:11
created

Factory::create()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2023
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Media;
12
13
14
/**
15
 * Common media controller factory.
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Factory
21
{
22
	private static $objects = [];
23
24
25
	/**
26
	 * Creates a new controller specified by the given name.
27
	 *
28
	 * @param \Aimeos\MShop\ContextIface $context Context object required by controllers
29
	 * @param string|null $name Name of the controller or "Standard" if null
30
	 * @return \Aimeos\Controller\Common\Media\Iface New media controller object
31
	 * @throws \LogicException If class isn't found
32
	 */
33
	public static function create( \Aimeos\MShop\ContextIface $context, string $name = null ) : Iface
34
	{
35
		/** controller/common/media/name
36
		 * Class name of the used media common controller implementation
37
		 *
38
		 * Each default common controller 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 controller factory knows which class it
41
		 * has to instantiate.
42
		 *
43
		 * For example, if the name of the default class is
44
		 *
45
		 *  \Aimeos\Controller\Common\Media\Standard
46
		 *
47
		 * and you want to replace it with your own version named
48
		 *
49
		 *  \Aimeos\Controller\Common\Media\Mymedia
50
		 *
51
		 * then you have to set the this configuration option:
52
		 *
53
		 *  controller/common/media/name = Mymedia
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 "MyMedia"!
63
		 *
64
		 * @param string Last part of the class name
65
		 * @since 2016.01
66
		 * @category Developer
67
		 */
68
		if( $name === null ) {
69
			$name = $context->config()->get( 'controller/common/media/name', 'Standard' );
70
		}
71
72
		if( ctype_alnum( $name ) === false ) {
73
			throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 );
74
		}
75
76
		$classname = 'Aimeos\Controller\Common\Media\\' . $name;
77
		$interface = \Aimeos\Controller\Common\Media\Iface::class;
78
79
		if( isset( self::$objects[$classname] ) ) {
80
			return self::$objects[$classname];
81
		}
82
83
		return \Aimeos\Utils::create( $classname, [$context], $interface );
84
	}
85
86
87
	/**
88
	 * Injects a controller object.
89
	 *
90
	 * The object is returned via createController() if an instance of the class
91
	 * with the name name is requested.
92
	 *
93
	 * @param string $classname Full name of the class for which the object should be returned
94
	 * @param null|\Aimeos\Controller\Common\Media\Iface $controller Frontend controller object
95
	 */
96
	public static function inject( string $classname, \Aimeos\Controller\Common\Media\Iface $controller = null )
97
	{
98
		self::$objects[trim( $classname, '\\' )] = $controller;
99
	}
100
}
101