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

YiiAdapter::getConfig()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.732

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 34
ccs 19
cts 24
cp 0.7917
rs 4.909
cc 9
eloc 17
nc 9
nop 3
crap 9.732
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 Exception;
16
use Maslosoft\EmbeDi\Interfaces\AdapterInterface;
17
use Yii;
18
19
/**
20
 * YiiAdapter
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class YiiAdapter implements AdapterInterface
25
{
26
27 7
	public function __construct()
28
	{
29 7
		if (!class_exists('Yii', false))
30 7
		{
31
			throw new Exception(sprintf('Adapter `%s` requires `Yii`', __CLASS__));
32
		}
33 7
	}
34
35 6
	public function getConfig($class, $instanceId, $presetId = null)
36
	{
37 6
		$app = Yii::app();
38 6
		if (empty($app))
39 6
		{
40
			return false;
41
		}
42 6
		$config = $app->getComponents(false);
43 6
		if (!empty($config[$instanceId]))
44 6
		{
45 5
			if (!empty($presetId) && empty($config[$instanceId][$presetId]))
46 5
			{
47
				// Preset is provided, but no configuration for preset found, skip
48
				return false;
49
			}
50 5
			if (!empty($presetId))
51 5
			{
52
				$config = $config[$instanceId][$presetId];
53
			}
54
			else
55
			{
56 5
				$config = $config[$instanceId];
57
			}
58 5
			if (is_object($config))
59 5
			{
60 3
				return (new YiiEmbeDi())->export($config);
61
			}
62 2
			if (isset($config['class']) && $config['class'] == $class)
63 2
			{
64 2
				return $config;
65
			}
66
		}
67 1
		return false;
68
	}
69
70
}
71