Completed
Pull Request — develop (#57)
by Tony
07:24
created

DbConfig::getConfigValueAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
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
 * 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
 */
35
class DbConfig extends Model
36
{
37
    /**
38
     * The table associated with the model.
39
     *
40
     * @var string
41
     */
42
    protected $table = 'config';
43
44
    /**
45
     * The primary key column name.
46
     *
47
     * @var string
48
     */
49
    protected $primaryKey = 'config_id';
50
51
    /**
52
     * Define fillable fields
53
     *
54
     * @var array
55
     */
56
    protected $fillable = ['config_name', 'config_value'];
57
58
    /**
59
     * Limit the query to config_names that start with $key
60
     *
61
     * @param $query Builder
62
     * @param $key string The key (full or partial) to limit the query to.
63
     * @return Builder
64
     */
65
    public function scopeKey(Builder $query, $key)
66
    {
67
        return $query->where('config_name', 'LIKE', $key . '%');
68
    }
69
70
    /**
71
     * Limit the query to the exact config_name.
72
     *
73
     * @param $query Builder
74
     * @param $key string The full key to limit the query to.
75
     * @return Builder
76
     */
77
    public function scopeExactKey(Builder $query, $key)
78
    {
79
        return $query->where('config_name', $key);
80
    }
81
82
    /**
83
     * Indicates if the model should be timestamped.
84
     *
85
     * @var bool
86
     */
87
    public $timestamps = false;
88
89
    // ---- Accessors/Mutators ----
90
91
    /**
92
     * Unserialize stored values.
93
     *
94
     * @param $value
95
     * @return mixed
96
     */
97
    public function getConfigValueAttribute($value)
98
    {
99
        if (!empty($value)) {
100
            return unserialize($value);
101
        }
102
        return $value;
103
    }
104
105
    /**
106
     * Serialize values before storing.
107
     *
108
     * @param $value
109
     */
110
    public function setConfigValueAttribute($value)
111
    {
112
        $this->attributes['config_value'] = serialize($value);
113
    }
114
}
115