Completed
Pull Request — master (#107)
by Kevin
06:11
created

ClassConfigurationReader::setConfigurationDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
                if (is_dir($filename)) {
48
                    $configurationDir = realpath($filename);
49
                    break;
50
                }
51
                $path .= '/../';
52
                $path = realpath($path); // More for debugging clarity.
53
            }
54
        }
55
56
        if (!$configurationDir) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $configurationDir of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
58
        $configurationFile = get_class($config) . '.php';
59
        $configurationFile = $configurationDir . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $configurationFile);
60
61
        if (file_exists($configurationFile)) {
62
            include $configurationFile;
63
        }
64
65
    }
66
67
}