GenericFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 8
c 0
b 0
f 0
dl 0
loc 35
ccs 6
cts 6
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A make() 0 10 3
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Application;
11
12
use Pimple\Container;
13
use WPEmerge\Exceptions\ClassNotFoundException;
14
15
/**
16
 * Generic class instance factory.
17
 */
18
class GenericFactory {
19
	/**
20
	 * Container.
21
	 *
22
	 * @var Container
23
	 */
24
	protected $container = null;
25
26
	/**
27
	 * Constructor.
28
	 *
29
	 * @codeCoverageIgnore
30
	 * @param Container $container
31
	 */
32
	public function __construct( Container $container ) {
33
		$this->container = $container;
34
	}
35
36
	/**
37
	 * Make a class instance.
38
	 *
39
	 * @throws ClassNotFoundException
40
	 * @param  string $class
41
	 * @return object
42
	 */
43 3
	public function make( $class ) {
44 3
		if ( isset( $this->container[ $class ] ) ) {
45 1
			return $this->container[ $class ];
46
		}
47
48 2
		if ( ! class_exists( $class ) ) {
49 1
			throw new ClassNotFoundException( 'Class not found: ' . $class );
50
		}
51
52 1
		return new $class();
53
	}
54
}
55