DbConfig::setConfigValueAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 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
 * DbConfig.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\Models;
26
27
use Illuminate\Database\Eloquent\Builder;
28
use Illuminate\Database\Eloquent\Model;
29
30
/**
31
 * App\Models\DbConfig
32
 *
33
 * @property integer $config_id
34
 * @property string $config_name
35
 * @property string $config_value
36
 * @property string $config_default
37
 * @property string $config_descr
38
 * @property string $config_group
39
 * @property integer $config_group_order
40
 * @property string $config_sub_group
41
 * @property integer $config_sub_group_order
42
 * @property string $config_hidden
43
 * @property string $config_disabled
44
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigId($value)
45
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigName($value)
46
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigValue($value)
47
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigDefault($value)
48
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigDescr($value)
49
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigGroup($value)
50
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigGroupOrder($value)
51
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigSubGroup($value)
52
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigSubGroupOrder($value)
53
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigHidden($value)
54
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig whereConfigDisabled($value)
55
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig key($key)
56
 * @method static \Illuminate\Database\Query\Builder|\App\Models\DbConfig exactKey($key)
57
 * @mixin \Eloquent
58
 */
59
class DbConfig extends Model
60
{
61
    /**
62
     * Indicates if the model should be timestamped.
63
     *
64
     * @var bool
65
     */
66
    public $timestamps = false;
67
    /**
68
     * The table associated with the model.
69
     *
70
     * @var string
71
     */
72
    protected $table = 'config';
73
    /**
74
     * The primary key column name.
75
     *
76
     * @var string
77
     */
78
    protected $primaryKey = 'config_id';
79
    /**
80
     * Define fillable fields
81
     *
82
     * @var array
83
     */
84
    protected $fillable = ['config_name', 'config_value'];
85
86
    // ---- Helper Functions ----
87
88
    /**
89
     * Unserialize default values.
90
     *
91
     * @param $value
92
     * @return mixed
93
     * @throws \Exception
94
     */
95
    public function getConfigDefault($value)
96
    {
97
        // uses the same code as values
98
        return $this->getConfigValueAttribute($value);
99
    }
100
101
    // ---- Accessors/Mutators ----
102
103
    /**
104
     * Unserialize stored values.
105
     *
106
     * @param $value
107
     * @return mixed
108
     * @throws \Exception
109
     */
110 21
    public function getConfigValueAttribute($value)
111
    {
112 21
        if (!empty($value)) {
113
            try {
114 21
                return unserialize($value);
115
            } catch (\Exception $e) {
116
                if (str_contains($e->getMessage(), 'unserialize():')) {
117
                    return $value;
118
                } else {
119
                    throw $e;
120
                }
121
            }
122
        }
123
        return $value;
124
    }
125
126
    /**
127
     * Serialize values before storing.
128
     *
129
     * @param $value
130
     */
131 35
    public function setConfigValueAttribute($value)
132
    {
133 35
        $this->attributes['config_value'] = serialize($value);
134 35
    }
135
136
    /**
137
     * Serialize defaults before storing.
138
     *
139
     * @param $value
140
     */
141
    public function setConfigDefaultAttribute($value)
142
    {
143
        $this->attributes['config_default'] = serialize($value);
144
    }
145
146
    // ---- Define Scopes ----
147
148
    /**
149
     * Limit the query to config_names that start with $key
150
     *
151
     * @param $query Builder
152
     * @param $key string The key (full or partial) to limit the query to.
153
     * @return Builder
154
     */
155 54
    public function scopeKey(Builder $query, $key)
156
    {
157 54
        return $query->where('config_name', $key)->orWhere('config_name', 'LIKE', $key.'.%');
158
    }
159
160
    /**
161
     * Limit the query to the exact config_name.
162
     *
163
     * @param $query Builder
164
     * @param $key string The full key to limit the query to.
165
     * @return Builder
166
     */
167 7
    public function scopeExactKey(Builder $query, $key)
168
    {
169 7
        return $query->where('config_name', $key);
170
    }
171
}
172