Contenedor   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A obtener() 0 18 3
A buildDependencies() 0 19 3
1
<?php
2
namespace src\contenedorId;
3
4
use ReflectionClass;
5
use RuntimeException;
6
use src\contenedorId\ContenedorInterface;
7
8
/**
9
 * 
10
 */
11
class Contenedor implements ContenedorInterface
12
{	
13
	private $_bindings = [];
14
15
	//Rutas de Factory
16
	private $_rutas = [
17
		'FactoryInterface' => 'src\\Factory'
18
	];
19
	
20
	public function obtener(string $abstract)
21
	{
22
		if(interface_exists($abstract))
23
		{
24
			$interface = explode('\\', $abstract);
25
			$abstract = $this->_rutas[$interface[1]];
26
		}
27
28
		if(isset($this->_bindings[$abstract]))
29
		{
30
			return $this->_bindings[$abstract]($this);
31
		}
32
		
33
		$reflection = new ReflectionClass($abstract);
34
35
		$dependencies = $this->buildDependencies($reflection);
36
37
		return $reflection->newInstanceArgs($dependencies);
38
	}
39
40
	private function buildDependencies(ReflectionClass $reflection): array
41
	{
42
		if(!$constructor = $reflection->getConstructor())
43
		{
44
			return [];
45
		}
46
47
		$params = $constructor->getParameters();
48
49
		return array_map(function ($param)
50
		{
51
			if(!$type = $param->getType())
52
			{
53
				throw new RuntimeException();
54
			}
55
56
			return $this->obtener($type->getName());
57
58
		}, $params);
59
	}
60
}