ArrayAdapter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 79
ccs 20
cts 32
cp 0.625
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getConfig() 0 47 10
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/embedi
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\EmbeDi\Adapters;
14
15
use Maslosoft\EmbeDi\Interfaces\AdapterInterface;
16
17
/**
18
 * ArrayAdapter
19
 *
20
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
21
 */
22
class ArrayAdapter implements AdapterInterface
23
{
24
25
	private $config = [];
26
27
	/**
28
	 * Configuration source for later use
29
	 * Config should have keys of component id and values of config.
30
	 * Example:
31
	 * ```
32
	 * [
33
	 * 		'logger' => [
34
	 * 			'class' => Monolog\Logger\Logger,
35
	 * 		],
36
	 * 		'mangan' => [
37
	 * 			'@logger' => 'logger'
38
	 * 		]
39
	 * ]
40
	 * ```
41
	 * Attributes starting with `@` denotes that link to other
42
	 * config component should be used. In example above, mangan field `logger`
43
	 * will be configured with monolog logger.
44
	 *
45
	 * @param array $config
46
	 */
47 3
	public function __construct($config)
48
	{
49 3
		$this->config = $config;
50 3
	}
51
52 3
	public function getConfig($class, $instanceId, $presetId = null)
53
	{
54 3
		if (isset($this->config[$instanceId]))
55 3
		{
56 3
			if (!empty($presetId) && empty($this->config[$instanceId][$presetId]))
57 3
			{
58
				// Preset is provided, but no configuration for preset found, skip
59
				return false;
60
			}
61 3
			$config = $this->config[$instanceId];
62
63 3
			if (!empty($presetId))
64 3
			{
65
				// Use preset
66 2
				$config = $config[$presetId];
67 2
			}
68
69 3
			if (is_object($config))
70 3
			{
71
				return (new YiiEmbeDi())->export($config);
72
			}
73 3
			if (empty($config['class']))
74 3
			{
75
				return false;
76
			}
77
78
			// Direct class
79 3
			if ($config['class'] == $class)
80 3
			{
81 3
				return $config;
82
			}
83
84
			// Subclass
85
			$info = new \ReflectionClass($class);
86
			if ($info->isSubclassOf($config['class']))
87
			{
88
				return $config;
89
			}
90
91
			// Interface
92
			if ($info->implementsInterface($config['class']))
93
			{
94
				return $config;
95
			}
96
		}
97
		return false;
98
	}
99
100
}
101