Completed
Pull Request — develop (#57)
by Tony
06:31
created

Settings::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (C) 2016 Tony Murray <[email protected]>
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
/**
18
 * Settings.php
19
 *
20
 * @package    LibreNMS
21
 * @author     Tony Murray <[email protected]>
22
 * @copyright  2016 Tony Murray
23
 * @license    @license http://opensource.org/licenses/GPL-3.0 GNU Public License v3 or later
24
 */
25
namespace App\Settings;
26
27
28
use Cache;
29
use Config;
30
use DB;
31
use Illuminate\Contracts\Config\Repository as ConfigContract;
32
33
class Settings implements ConfigContract
34
{
35
    private $cache_time;
36
    private $database;
37
38
    public function __construct()
39
    {
40
        $this->database = new DatabaseRepository(DB::connection(), 'config');
0 ignored issues
show
Unused Code introduced by
The call to DatabaseRepository::__construct() has too many arguments starting with \DB::connection().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
41
        $this->cache_time = env('CACHE_LIFETIME', 60);
42
    }
43
44
    public function set($key, $value = null)
45
    {
46
        if (is_array($value)) {
47
            $value = self::arrayToPath($value, $key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by parameter $key on line 44 can also be of type array; however, App\Settings\Settings::arrayToPath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
            foreach ($value as $k => $v) {
49
                $this->database->set($k, $v);
50
            }
51
        }
52
        else {
53
            $this->database->set($key, $value);
54
        }
55
        return $value;
56
    }
57
58
    protected static function arrayToPath($array, $prefix = "")
59
    {
60
        return self::recursive_keys($array, $prefix);
61
    }
62
63
    private static function recursive_keys(array $array, $prefix = "", array $path = array())
64
    {
65
        if ($prefix != "") {
66
            $prefix = trim($prefix, '.') . '.';
67
        }
68
        $result = array();
69
        foreach ($array as $key => $val) {
70
            $currentPath = array_merge($path, array($key));
71
            if (is_array($val)) {
72
                $result = array_merge($result, self::recursive_keys($val, $prefix, $currentPath));
73
            }
74
            else {
75
                $result[$prefix . join('.', $currentPath)] = $val;
76
            }
77
        }
78
        return $result;
79
    }
80
81
    public function get($key, $default = null)
82
    {
83
        // return value from cache or fetch it and return it
84
        return Cache::remember($key, $this->cache_time, function () use ($key, $default) {
85
            $value = $this->database->get($key, $default);
86
87
            if (is_array($value)) {
88
                $value = self::pathToArray($value, $key);
89
                $config = Config::get('config.' . $key, $default);
90
                if (!is_null($config)) {
91
                    $value = array_replace_recursive($config, $value);
92
                }
93
            }
94
            elseif (is_null($value)) {
95
                return Config::get('config.' . $key);
96
            }
97
98
            return $value;
99
        });
100
    }
101
102
    protected static function pathToArray($data, $prefix = "")
103
    {
104
        $tree = array();
105
        foreach ($data as $key => $value) {
106
            if (substr($key, 0, strlen($prefix)) == $prefix) {
107
                $key = substr($key, strlen($prefix));
108
            }
109
            $parts = explode('.', trim($key, '.'));
110
111
            $temp = &$tree;
112
            foreach ($parts as $part) {
113
                $temp = &$temp[$part];
114
            }
115
            $temp = $value;
116
            unset($temp);
117
        }
118
        return $tree;
119
    }
120
121
    public function has($key)
122
    {
123
        return (Cache::has($key) || Config::has($key) || $this->database->has($key));
124
    }
125
126
    public function forget($key)
127
    {
128
        $this->database->forget($key);
129
        Cache::forget($key);
130
    }
131
132
    public function all()
133
    {
134
        // no caching :(
135
        $config_settings = Config::all()['config'];
136
        $db_settings = self::pathToArray($this->database->all());
137
        return array_replace_recursive($config_settings, $db_settings);
138
    }
139
140
    /**
141
     * Prepend a value onto an array configuration value.
142
     *
143
     * @param  string $key
144
     * @param  mixed $value
145
     * @return void
146
     */
147
    public function prepend($key, $value)
148
    {
149
        // TODO: Implement prepend() method.S
150
    }
151
152
    /**
153
     * Push a value onto an array configuration value.
154
     *
155
     * @param  string $key
156
     * @param  mixed $value
157
     * @return void
158
     */
159
    public function push($key, $value)
160
    {
161
        // TODO: Implement push() method.
162
    }
163
}