ClassConfigurationReader::configure()   C
last analyzed

Complexity

Conditions 11
Paths 20

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 32
nc 20
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 ClassConfigurationReader extends AbstractConfigurationReader
6
{
7
8
    protected $configurationDir;
9
    protected $object;
10
11
    public function __construct(
12
        $configurationDir = null
13
    )
14
    {
15
        $this->configurationDir = $configurationDir;
16
    }
17
18
    /**
19
     * @param null $configurationDir
20
     */
21
    public function setConfigurationDir($configurationDir)
22
    {
23
        $this->configurationDir = $configurationDir;
24
    }
25
26
27
    public function __set($name, $value)
28
    {
29
        $this->object->$name = $value;
30
    }
31
32
    public function __call($method, $args)
33
    {
34
        return call_user_func_array([$this->object, $method], $args);
35
    }
36
37
38
    public function configure(ConfigurableObjectInterface $config)
39
    {
40
        $this->object = $config;
41
        $configurationDir = $this->configurationDir;
42
        if ($configurationDir === null) {
43
            $testParts = explode(DIRECTORY_SEPARATOR, realpath(__DIR__.'/../../..'));
44
            $isVendor = array_pop($testParts) == 'vendor';
45
46
            if ($isVendor) {
47
                $path = realpath(__DIR__ . '/../../../../..'); // Get out of the Magium directories
48
            } else {
49
                $path = realpath(__DIR__ . '/../../..');
50
            }
51
52
            $count = 0;
53
            while ($count++ < 10) {
54
                $filename = "{$path}/configuration";
55
                $realpath = realpath($filename);
56
                if ($realpath !== false && is_dir($realpath)) {
57
                    // The equality check is due to case-insensitive file systems *ahem* Windows and OS X HFS+
58
                    $directories = glob(realpath($filename.'/../').'/*', GLOB_ONLYDIR);
59
                    foreach ($directories as $directory) {
60
                        $directory = realpath($directory);
61
                        $parts = explode(DIRECTORY_SEPARATOR, $directory);
62
                        $lastPart = array_pop($parts);
63
                        if ($lastPart == 'configuration') {
64
                            $configurationDir = $realpath;
65
                            break 2;
66
                        }
67
                    }
68
                }
69
                $path .= '/../';
70
                $path = realpath($path); // More for debugging clarity.
71
            }
72
        }
73
74
        if (!is_dir($configurationDir)) return;
75
76
        $classes = $this->introspectClass($config);
77
78
        foreach ($classes as $class) {
79
            $configurationFile = $class . '.php';
80
            $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
81
82
            if (file_exists($configurationFile)) {
83
                include $configurationFile;
84
            }
85
        }
86
    }
87
88
}
89