ConfigProxy::searchForConfigClass()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
namespace Api\Config;
3
4
class ConfigProxy
5
{
6
7
    private $config = null;
8
    private $formats =
9
    [
10
        [
11
            'extension' => 'json',
12
            'configName' => 'Api\Config\ConfigJson'
13
        ],
14
    ];
15
16
    public function __construct($filename)
17
    {
18
        $extension = $this->getFileExtension($filename);
19
        $configName = $this->searchForConfigClass($extension);
20
        $this->config = new $configName;
21
        $this->config->loadFromFile($filename);
22
    }
23
24
    private function getFileExtension($filename)
25
    {
26
        $extension = explode('.', $filename);
27
        $extension = strtolower(end($extension));
28
        return $extension;
29
    }
30
31
    private function searchForConfigClass($extension)
32
    {
33
        foreach ($this->formats as $format) {
34
            if ($format['extension'] == $extension) {
35
                return $format['configName'];
36
            }
37
        }
38
        throw new \Exception('Config format can\'t be handled');
39
    }
40
41
    public function __get($name)
42
    {
43
        return $this->config->$name;
44
    }
45
46
    public function __set($name, $value)
47
    {
48
        $this->config->$name = $value;
49
    }
50
}
51