Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

Settings   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 274
Duplicated Lines 6.93 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 40
c 4
b 1
f 1
lcom 1
cbo 1
dl 19
loc 274
rs 8.2608

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 4 3
A all() 0 7 1
A collectionToArray() 0 19 4
A prepend() 9 19 3
D get() 0 39 9
A forget() 0 14 2
A flush() 0 15 3
D set() 0 32 9
A isReadOnly() 0 5 2
A push() 10 18 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Settings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Settings, and based on these observations, apply Extract Interface, too.

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;
26
27
28
use App\Models\DbConfig;
29
use Cache;
30
use Config;
31
use Illuminate\Contracts\Config\Repository as ConfigContract;
32
33
// adds the possibility to replace the default Config facade
34
35
class Settings implements ConfigContract
36
{
37
    /**
38
     * The tag for the cache.
39
     *
40
     * @var string
41
     */
42
    public static $cache_tag = "Settings";
43
    /**
44
     * The amount of time in minutes to store values in cache
45
     *
46
     * @var int
47
     */
48
    private $cache_time;
49
50
    /**
51
     * Settings constructor.
52
     */
53
    public function __construct()
54
    {
55
        $this->cache_time = env('CACHE_LIFETIME', 60);
56
    }
57
58
    /**
59
     * Check if the key is defined in the Settings store.
60
     *
61
     * @param string $key Only full paths will return true.
62
     * @return bool
63
     */
64
    public function has($key)
65
    {
66
        return (Cache::tags(self::$cache_tag)->has($key) || Config::has('config.'.$key) || DbConfig::key($key)->exists());
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Database\Query\Builder, but not in App\Models\DbConfig.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
    }
68
69
    /**
70
     * Get all settings defined in the Settings store.
71
     *
72
     * @return array A nested array of all settings.
73
     */
74
    public function all()
75
    {
76
        // no caching :(
77
        $config_settings = Config::all()['config'];
78
        $db_settings = self::collectionToArray(DbConfig::all());
79
        return array_replace_recursive($config_settings, $db_settings);
80
    }
81
82
    /**
83
     * Convert an Eloquent Collection into a nested array
84
     *
85
     * @param $data \Illuminate\Database\Eloquent\Collection The Collection.
86
     * @param string $prefix Path to prepend. Do not include trailing .
87
     * @return array The resulting nested array.
88
     */
89
    private static function collectionToArray($data, $prefix = "")
90
    {
91
        $tree = array();
92
        foreach ($data as $item) {
93
            $key = $item->config_name;
94
            if (starts_with($key, $prefix)) {
95
                $key = substr($key, strlen($prefix));
96
            }
97
            $parts = explode('.', trim($key, '.'));
98
99
            $temp = &$tree;
100
            foreach ($parts as $part) {
101
                $temp = &$temp[$part];
102
            }
103
            $temp = $item->config_value;
104
            unset($temp);
105
        }
106
        return $tree;
107
    }
108
109
    /**
110
     * Prepend a value onto an array configuration value.
111
     *
112
     * @param  string $key
113
     * @param  mixed $value
114
     * @return void
115
     */
116
    public function prepend($key, $value)
117
    {
118
        $var = $this->get($key);
119
120
        if (is_array($var)) {
121
            $this->forget($key);
122
            array_unshift($var, $value);
123
            $this->set($key, $var);
124
        }
125 View Code Duplication
        else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
            $arr = [$value];
127
            if (!is_null($var)) {
128
                $arr[] = $var;
129
            }
130
131
            $this->forget($key);
132
            $this->set($key, $arr);
133
        }
134
    }
135
136
    /**
137
     * Get a value from the Settings store.
138
     *
139
     * @param string $key A full or partial . separated key.
140
     * @param null $default If the key isn't found, return this value. By default undefined keys return null.
141
     * @return mixed If the $key is a full path, a bare value will be returned.  If it is a partial path, a nested array will be retuned.
142
     */
143
    public function get($key, $default = null)
144
    {
145
        // return value from cache or fetch it and return it
146
        return Cache::tags(self::$cache_tag)->remember($key, $this->cache_time, function() use ($key, $default) {
147
148
            // fetch the values from storage
149
            if (Config::has('config.'.$key)) {
150
                $config_data = Config::get('config.'.$key, $default);
151
            }
152
            $db_data = DbConfig::key($key)->get(['config_name', 'config_value']);
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\DbConfig.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
153
154
            if (count($db_data) == 1 && $db_data->first()->config_name == $key) {
155
                // return a value if we are getting one item and it is the requested item (not recursing)
156
                return $db_data->first()->config_value;
157
            }
158
            elseif (count($db_data) >= 1) {
159
                // convert the collection to an array
160
                $result = self::collectionToArray($db_data, $key);
161
162
                // if we have config_data, merge them
163
                if (isset($config_data)) {
164
                    return array_replace_recursive($config_data, $result);
165
                }
166
                else {
167
                    return $result;
168
                }
169
            }
170
171
            // fall back to config.php/defaults.php
172
            if (isset($config_data) && (!is_array($config_data) || count($db_data) == 0)) {
173
                // return the value from config.php if it is a value
174
                return $config_data;
175
            }
176
177
178
            // we couldn't find the key, return the default
179
            return $default;
180
        });
181
    }
182
183
    /**
184
     * Forget a key and all children
185
     * This cannot forget variables set in config.php
186
     *
187
     * @param string $key Explicit key to forget
188
     */
189
    public function forget($key)
190
    {
191
        // Cannot remove from config
192
193
        $count = DbConfig::key($key)->count();
0 ignored issues
show
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in App\Models\DbConfig.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
194
        if ($count == 1) {
195
            $this->flush($key);
196
        }
197
        else {
198
            $this->flush(); // possible optimization: selective flush
199
        }
200
201
        DbConfig::key($key)->delete();
202
    }
203
204
    /**
205
     * Clear the settings cache.
206
     * If path is set, only clear the path and it's parents.
207
     * This will not clear children.
208
     *
209
     * @param string $key The path to clear.
210
     */
211
    public function flush($key = null)
212
    {
213
        if (is_null($key)) {
214
            // Clear all cache
215
            Cache::tags(self::$cache_tag)->flush();
216
        }
217
        else {
218
            // Clear specific path
219
            $path = [];
220
            foreach (explode('.', $key) as $element) {
221
                $path[] = $element;
222
                Cache::tags(self::$cache_tag)->forget(join('.', $path));
223
            }
224
        }
225
    }
226
227
    // ---- Local Utility functions ----
228
229
    /**
230
     * Set a key value pair into the Settings store.
231
     *
232
     * @param string $key A . separated path to this setting
233
     * @param array|null $value A value or an array. If value is an array it will be converted to a . separate path(s) concatinated onto the given key
234
     * @throws \Exception
235
     */
236
    public function set($key, $value = null)
237
    {
238
        if (is_array($value)) {
239
            // repeat until value contains no arrays
240
            foreach ($value as $k => $v) {
241
                if (!empty($key)) {
242
                    if (is_string($k) && !str_contains($k, '.') && DbConfig::exactKey($key)->exists() && DbConfig::key($key)->count() == 1) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Database\Query\Builder, but not in App\Models\DbConfig.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Bug introduced by
The method count does only exist in Illuminate\Database\Query\Builder, but not in App\Models\DbConfig.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
243
                        // check that we aren't trying to set an array onto an existing value only setting
244
                        throw new \Exception("Attempting to set array value to existing non-array value at the key '".$key."'");
245
                    } else {
246
                        // we are not at the leaf yet, add this chunk to the key and recurse
247
                        $this->set($key.'.'.$k, $v);
248
                    }
249
                } else {
250
                    // a leaf, recurse one last time
251
                    $this->set($k, $v);
252
                }
253
            }
254
        }
255
        else {
256
            // make sure we can save this
257
            if ($this->isReadOnly($key)) {
258
                throw new \Exception("The setting '".$key."' is read only");
259
            }
260
261
            // flush the cache and save the value in db and cache
262
            $this->flush($key);
263
            DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $value]);
0 ignored issues
show
Bug introduced by
The method updateOrCreate() does not exist on App\Models\DbConfig. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
264
            Cache::tags(self::$cache_tag)->put($key, $value, $this->cache_time);
265
        }
266
267
    }
268
269
    /**
270
     * Check if the key is read only.
271
     * When the user is not a global admin.
272
     * Currently, $key is unused
273
     *
274
     * @param string $key The path to check
275
     * @return boolean false or the source: config | auth
276
     */
277
    public function isReadOnly($key)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
278
    {
279
        $user = \Auth::user();
280
        return (is_null($user) || !$user->isAdmin());
281
    }
282
283
    /**
284
     * Push a value onto an array configuration value.
285
     *
286
     * @param  string $key
287
     * @param  mixed $value
288
     * @return void
289
     */
290
    public function push($key, $value)
291
    {
292
        $var = $this->get($key);
293
        if (is_array($var)) {
294
            $var[] = $value;
295
            $this->set($key, $var);
296
        }
297 View Code Duplication
        else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
            $arr = array();
299
            if (!is_null($var)) {
300
                $arr[] = $var;
301
            }
302
            $arr[] = $value;
303
304
            $this->forget($key);
305
            $this->set($key, $arr);
306
        }
307
    }
308
}
309