Completed
Pull Request — develop (#57)
by Tony
08:32
created

Settings::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
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
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
//            Cache::put($key, $v);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
51
            }
52
        }
53
        else {
54
            $this->database->set($key, $value);
55
        }
56
//            Cache::put($key, $v);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
        return $value;
58
    }
59
60
    public function get($key, $default = null)
61
    {
62
        $value = Cache::get($key);
63
        if (is_null($value)) {
64
            $value = $this->database->get($key, $default);
65
            if (is_array($value)) {
66
                $value = self::pathToArray($value, $key);
67
                $config = Config::get('config.' . $key, $default);
68
                if (!is_null($config)) {
69
                    $value = array_replace_recursive($config, $value);
70
                }
71
            }
72
            elseif (is_null($value)) {
73
                $value = Config::get('config.' . $key);
74
            }
75
76
//FIXME: insert cache
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
//            if (!is_null($value)) {
78
//                Cache::put($key, $value);
79
//            }
80
        }
81
        return $value;
82
    }
83
84
    public function has($key)
85
    {
86
        return (Cache::has($key) || Config::has($key) || $this->database->has($key));
87
    }
88
89
    public function forget($key)
90
    {
91
        $this->database->forget($key);
92
        Cache::forget($key);
93
//        Config::forget($key);  // config can't forget?
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
    }
95
96
    public function all()
97
    {
98
        // no caching :(
99
        $config_settings = Config::all()['config'];
100
        $db_settings = self::pathToArray($this->database->all());
101
        return array_replace_recursive($config_settings, $db_settings);
102
    }
103
104
    protected static function pathToArray($data, $prefix="")
105
    {
106
        $tree = array();
107
        foreach ($data as $key => $value) {
108
            if (substr($key, 0, strlen($prefix)) == $prefix) {
109
                $key = substr($key, strlen($prefix));
110
            }
111
            $parts = explode('.', trim($key, '.'));
112
113
            $temp = &$tree;
114
            foreach ($parts as $part) {
115
                $temp = &$temp[$part];
116
            }
117
            $temp = $value;
118
            unset($temp);
119
        }
120
        return $tree;
121
    }
122
123
    protected static function arrayToPath($array, $prefix="")
124
    {
125
        return self::recursive_keys($array, $prefix);
126
    }
127
128
    private static function recursive_keys(array $array, $prefix="", array $path = array())
129
    {
130
        if ($prefix != "") {
131
            $prefix = trim($prefix, '.') . '.';
132
        }
133
        $result = array();
134
        foreach ($array as $key => $val) {
135
            $currentPath = array_merge($path, array($key));
136
            if (is_array($val)) {
137
                $result = array_merge($result, self::recursive_keys($val, $prefix, $currentPath));
138
            }
139
            else {
140
                $result[$prefix . join('.', $currentPath)] = $val;
141
            }
142
        }
143
        return $result;
144
    }
145
146
    /**
147
     * Prepend a value onto an array configuration value.
148
     *
149
     * @param  string $key
150
     * @param  mixed $value
151
     * @return void
152
     */
153
    public function prepend($key, $value)
154
    {
155
        // TODO: Implement prepend() method.S
156
    }
157
158
    /**
159
     * Push a value onto an array configuration value.
160
     *
161
     * @param  string $key
162
     * @param  mixed $value
163
     * @return void
164
     */
165
    public function push($key, $value)
166
    {
167
        // TODO: Implement push() method.
168
    }
169
}