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

Utils   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
dl 0
loc 25
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A implements() 0 7 2
A create() 0 13 4
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 */
7
8
9
namespace Aimeos;
10
11
12
/**
13
 * Utility methods
14
 */
15
class Utils
16
{
17
	public static function create( string $class, array $args, string $iface = null ) : object
18
	{
19
		if( class_exists( $class ) === false ) {
20
			throw new \LogicException( sprintf( 'Class "%1$s" not found', $class ), 400 );
21
		}
22
23
		$object = new $class( ...$args );
24
25
		if( $iface && !( $object instanceof $iface ) ) {
26
			throw new \LogicException( sprintf( 'Class "%1$s" does not implement "%2$s"', $class, $iface ), 400 );
27
		}
28
29
		return $object;
30
	}
31
32
33
	public static function implements( object $object, string $iface ) : object
34
	{
35
		if( !( $object instanceof $iface ) ) {
36
			throw new \LogicException( sprintf( 'Class "%1$s" does not implement "%2$s"', get_class( $object ), $iface ), 400 );
37
		}
38
39
		return $object;
40
	}
41
}
42