FileConfig   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 18
c 2
b 0
f 1
dl 0
loc 59
ccs 0
cts 24
cp 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A has() 0 3 1
A get() 0 7 2
A save() 0 3 1
A set() 0 9 1
A load() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of PHP DNS Server.
5
 *
6
 * (c) Yif Swery <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace yswery\DNS\Config;
13
14
use yswery\DNS\Exception\ConfigFileNotFoundException;
15
16
class FileConfig
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $configFile;
22
23
    /**
24
     * @var object
25
     */
26
    protected $config;
27
28
    public function __construct(string $configFile)
29
    {
30
        $this->configFile = $configFile;
31
    }
32
33
    /**
34
     * @throws ConfigFileNotFoundException
35
     */
36
    public function load()
37
    {
38
        // make sure the file exists before loading the config
39
        if (file_exists($this->configFile)) {
40
            $data = file_get_contents($this->configFile);
41
            $this->config = json_decode($data);
42
        } else {
43
            throw new ConfigFileNotFoundException('Config file not found.');
44
        }
45
    }
46
47
    public function save()
48
    {
49
        file_put_contents($this->configFile, json_encode($this->config));
50
    }
51
52
    public function get($key, $default = null)
53
    {
54
        if (isset($this->config->{$key})) {
55
            return $this->config->{$key};
56
        }
57
58
        return $default;
59
    }
60
61
    public function set(array $data)
62
    {
63
        $configObject = new RecursiveArrayObject($this->config);
64
        $configArray = $configObject->getArrayCopy();
65
66
        //$origional = json_decode(json_encode($this->config), true);
67
        $new = array_merge($configArray, $data);
68
69
        $this->config = json_decode(json_encode($new), false);
70
    }
71
72
    public function has($key)
73
    {
74
        return isset($this->config->{$key});
75
    }
76
}
77