Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Html::create()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 8
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client;
12
13
14
/**
15
 * Common factory for HTML clients
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Html
21
{
22
	/**
23
	 * Creates a new client object
24
	 *
25
	 * @param \Aimeos\MShop\Context\Item\Iface $context Shop context instance with necessary objects
26
	 * @param string $path Type of the client, e.g 'account/favorite' for \Aimeos\Client\Html\Account\Favorite\Standard
27
	 * @param string|null $name Client name (default: "Standard")
28
	 * @return \Aimeos\Client\Html\Iface HTML client implementing \Aimeos\Client\Html\Iface
29
	 * @throws \Aimeos\Client\Html\Exception If requested client implementation couldn't be found or initialisation fails
30
	 */
31
	public static function create( \Aimeos\MShop\Context\Item\Iface $context, string $path, string $name = null ) : \Aimeos\Client\Html\Iface
32
	{
33
		if( empty( $path ) ) {
34
			throw new \Aimeos\Client\Html\Exception( sprintf( 'Client path is empty' ) );
35
		}
36
37
		$parts = explode( '/', $path );
38
39
		foreach( $parts as $key => $part )
40
		{
41
			if( ctype_alnum( $part ) === false )
42
			{
43
				$msg = sprintf( 'Invalid characters in client name "%1$s"', $path );
44
				throw new \Aimeos\Client\Html\Exception( $msg, 400 );
45
			}
46
47
			$parts[$key] = ucfirst( $part );
48
		}
49
50
		$factory = '\\Aimeos\\Client\\Html\\' . join( '\\', $parts ) . '\\Factory';
51
52
		if( class_exists( $factory ) === false ) {
53
			throw new \Aimeos\Client\Html\Exception( sprintf( 'Class "%1$s" not available', $factory ) );
54
		}
55
56
		if( ( $client = @call_user_func_array( [$factory, 'create'], [$context, $name] ) ) === false ) {
57
			throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid factory "%1$s"', $factory ) );
58
		}
59
60
		return $client;
61
	}
62
}
63