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

ClassConfigurationReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setConfigurationFile() 0 4 1
A __set() 0 4 1
A __call() 0 4 1
B configure() 0 30 6
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
}