Completed
Push — master ( 88148a...751b72 )
by Kevin
05:53 queued 02:43
created

ClassConfigurationReader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 13
c 5
b 1
f 1
lcom 1
cbo 0
dl 0
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __call() 0 4 1
A __construct() 0 6 1
A setConfigurationDir() 0 4 1
D configure() 0 37 9
1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
class ClassConfigurationReader
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
        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
            $path = realpath(__DIR__ . '/../../../../..'); // Get out of the Magium directories
44
            $count = 0;
45
            while ($count++ < 10) {
46
                $filename = "{$path}/configuration";
47
                $realpath = realpath($filename);
48
                if ($realpath !== false && is_dir($realpath)) {
49
                    // The equality check is due to case-insensitive file systems *ahem* Windows and OS X HFS+
50
                    $directories = glob(realpath($filename.'/../').'/*', GLOB_ONLYDIR);
51
                    foreach ($directories as $directory) {
52
                        $parts = explode(DIRECTORY_SEPARATOR, $directory);
53
                        $lastPart = array_pop($parts);
54
                        if ($lastPart == 'configuration') {
55
                            $configurationDir = $realpath;
56
                            break;
57
                        }
58
                    }
59
                }
60
                $path .= '/../';
61
                $path = realpath($path); // More for debugging clarity.
62
            }
63
        }
64
65
        if (!is_dir($configurationDir)) return;
66
67
        $configurationFile = get_class($config) . '.php';
68
        $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
69
70
        if (file_exists($configurationFile)) {
71
            include $configurationFile;
72
        }
73
74
    }
75
76
}