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

DatabaseRepository::collectionToArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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
 * DatabaseRepository.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 App\Models\DbConfig;
29
use App\Models\Notification;
30
31
class DatabaseRepository
32
{
33
    // 'config_name', 'config_value'
34
35
    public function has($key)
36
    {
37
        return DbConfig::exactKey($key)->exists();
0 ignored issues
show
Bug introduced by
The method exactKey() does not exist on App\Models\DbConfig. Did you maybe mean scopeExactKey()?

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...
38
    }
39
40
    public function get($key, $default = null)
41
    {
42
        $results = DbConfig::key($key)->get(['config_name', 'config_value']);
43
        if (count($results) > 1) {
44
            return $this->collectionToArray($results);
45
        }
46
        elseif (count($results) == 1) {
47
            $entry = $results->first();
48
            if ($entry->config_name != $key) { //FIXME: better test
49
                // trim the prefix
50
                $local_key = substr($entry->config_name, strlen($key) + 1);
51
                return [$local_key => $entry->config_value];
52
            }
53
54
            $value = $entry->config_value;
55
            return is_null($value) ? $default : $value;
56
        }
57
        else {
58
            return $default;
59
        }
60
    }
61
62
    private function collectionToArray($collection)
63
    {
64
        $ret = array();
65
        foreach ($collection as $item) {
66
            $ret[$item->config_name] = $item->config_value;
67
        }
68
        return $ret;
69
    }
70
71
    public function set($key, $value = null)
72
    {
73
        DbConfig::updateOrCreate(['config_name' => "$key"], ['config_value' => $value]);
1 ignored issue
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...
74
    }
75
76
    public function reset($key)
77
    {
78
        DbConfig::key($key)->delete();
79
    }
80
81
    public function forget($key)
82
    {
83
        // set to null to prevent falling back to Config
84
        DbConfig::key($key)->update(['config_value' => null]);
85
    }
86
87
    public function all()
88
    {
89
        $settings = DbConfig::all();
90
        return $this->objsToArray($settings);
91
    }
92
93
    private function objsToArray($objs)
94
    {
95
        $ret = array();
96
        foreach ($objs as $obj) {
97
            $ret[$obj->key] = $obj->value;
98
        }
99
        return $ret;
100
    }
101
}