Completed
Push — master ( 4ea525...0da2a9 )
by Peter
06:28
created

ArrayAdapter::getConfig()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.5969

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 15
cts 19
cp 0.7895
rs 5.3846
cc 8
eloc 12
nc 8
nop 3
crap 8.5969
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 2
	public function __construct($config)
48
	{
49 2
		$this->config = $config;
50 2
	}
51
52 2
	public function getConfig($class, $instanceId, $presetId = null)
53
	{
54 2
		if (isset($this->config[$instanceId]))
55 2
		{
56 2
			if (!empty($presetId) && empty($this->config[$instanceId][$presetId]))
57 2
			{
58
				// Preset is provided, but no configuration for preset found, skip
59
				return false;
60
			}
61 2
			$config = $this->config[$instanceId];
62
63 2
			if (!empty($presetId))
64 2
			{
65
				// Use preset
66 1
				$config = $config[$presetId];
67 1
			}
68
69 2
			if (is_object($config))
70 2
			{
71
				return (new YiiEmbeDi())->export($config);
72
			}
73 2
			if (isset($config['class']) && $config['class'] == $class)
74 2
			{
75 2
				return $config;
76
			}
77
		}
78
		return false;
79
	}
80
81
}
82