Completed
Push — master ( 2247d8...e3c171 )
by Kevin
02:48
created

ClassConfigurationReader::configure()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 32
rs 6.7272
cc 7
eloc 21
nc 9
nop 1
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__ . '/../');
44
            $count = 0;
45
            while ($count++ < 10) {
46
                $filename = "{$path}/configuration";
47
                $realpath = realpath($filename);
48
                $parts = explode(DIRECTORY_SEPARATOR, $realpath);
49
                $lastPart = array_pop($parts);
50
                // The equality check is due to case-insensitive file systems *ahem* Windows
51
                if ($lastPart == 'configuration' && is_dir($realpath)) {
52
                    $configurationDir = $realpath;
53
                    break;
54
                }
55
                $path .= '/../';
56
                $path = realpath($path); // More for debugging clarity.
57
            }
58
        }
59
60
        if (!is_dir($configurationDir)) return;
61
62
        $configurationFile = get_class($config) . '.php';
63
        $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
64
65
        if (file_exists($configurationFile)) {
66
            include $configurationFile;
67
        }
68
69
    }
70
71
}