Completed
Push — develop ( c61736...9994ff )
by Neil
7s
created

DbConfig::setConfigDefaultAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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