Completed
Push — master ( 1e878c...90662d )
by Kevin
07:14 queued 03:43
created

ClassConfigurationReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Magium\Util\Configuration;
4
5
class ClassConfigurationReader
6
{
7
8
    protected $configurationFile;
9
    protected $object;
10
11
    public function __construct(
12
        $configurationFile = null
13
    )
14
    {
15
        $this->configurationFile = $configurationFile;
16
    }
17
18
    /**
19
     * @param null $configurationFile
20
     */
21
    public function setConfigurationFile($configurationFile)
22
    {
23
        $this->configurationFile = $configurationFile;
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
        $configurationFile = $this->configurationFile;
42
        if ($configurationFile === null) {
43
            $configurationFile = get_class($config) . '.php';
44
            $configurationFile = str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
45
46
            $count = 0;
47
            $path = realpath(__DIR__ . '/../');
48
49
            while ($count++ < 10) {
50
                $filename = "{$path}/configuration";
51
                if (is_dir($filename)) {
52
                    $filename .= '/' . $configurationFile;
53
                    if (file_exists($filename)) {
54
                        $configurationFile = realpath($filename);
55
                        break;
56
                    }
57
                }
58
                $path .= '/../';
59
                $path = realpath($path); // More for debugging clarity.
60
            }
61
        }
62
63
        if (file_exists($configurationFile)) {
64
            include $configurationFile;
65
        }
66
67
    }
68
69
}