ConfigurationReader::configure()   D
last analyzed

Complexity

Conditions 10
Paths 50

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 22
nc 50
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
class ConfigurationReader extends AbstractConfigurationReader
6
{
7
8
    protected $baseDir;
9
10
    protected $config = null;
11
12
    public function __construct(
13
        $baseDir = null
14
    )
15
    {
16
        $this->baseDir = $baseDir;
17
    }
18
19
    public function configure(ConfigurableObjectInterface $config)
20
    {
21
        if ($this->config === null) {
22
            $file = 'magium.json';
23
            $dir = $this->baseDir;
24
            if ($dir === null) {
25
                $dir = __DIR__;
26
            }
27
            $count = 0;
28
            while ($count++ < 10) {
29
                $filePath = $dir . '/' . $file;
30
                if (file_exists($filePath)) {
31
                    $this->config = json_decode(file_get_contents($filePath), true);
32
                    break;
33
                }
34
                $dir = realpath($dir . '/..');
35
            }
36
        }
37
38
        if ($this->config === null) {
39
            $this->config = false;
40
        }
41
42
        if ($this->config) {
43
            $classes = $this->introspectClass($config);
44
            foreach ($classes as $class) {
45
                $className = strtolower($class);
46
                if (isset($this->config['magium'][$className])) {
47
                    foreach ($this->config['magium'][$className] as $property => $value) {
48
                        $config->set($property, $value);
49
                    }
50
                }
51
            }
52
        }
53
54
    }
55
56
}
57