Passed
Push — main ( f9f23c...f7616c )
by Aimeos
05:03
created

Factory::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 50
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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