Passed
Push — master ( bd753d...066495 )
by Darko
10:20
created

Settings   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 42
dl 0
loc 146
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A convertValue() 0 24 5
A getValueAttribute() 0 3 1
A settingValue() 0 6 1
A __get() 0 12 3
A toTree() 0 11 2
A settingsUpdate() 0 4 3
1
<?php
2
3
/**
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
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU General Public License for more details.
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program (see LICENSE.txt in the base directory.  If
14
 * not, see:.
15
 *
16
 * @link      <http://www.gnu.org/licenses/>.
17
 *
18
 * @author    niel
19
 * @author    DariusIII
20
 * @copyright 2016 nZEDb, 2017 NNTmux
21
 */
22
23
namespace App\Models;
24
25
use Illuminate\Database\Eloquent\Model;
26
27
/**
28
 * Settings - model for settings table.
29
 *
30
 * @property string $name
31
 * @property string $value
32
 *
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Settings whereName($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Settings whereValue($value)
35
 *
36
 * @mixin \Eloquent
37
 *
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Settings newModelQuery()
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Settings newQuery()
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Settings query()
41
 */
42
class Settings extends Model
43
{
44
    public const REGISTER_STATUS_OPEN = 0;
45
46
    public const REGISTER_STATUS_INVITE = 1;
47
48
    public const REGISTER_STATUS_CLOSED = 2;
49
50
    public const ERR_BADUNRARPATH = -1;
51
52
    public const ERR_BADFFMPEGPATH = -2;
53
54
    public const ERR_BADMEDIAINFOPATH = -3;
55
56
    public const ERR_BADNZBPATH = -4;
57
58
    public const ERR_DEEPNOUNRAR = -5;
59
60
    public const ERR_BADTMPUNRARPATH = -6;
61
62
    public const ERR_BADLAMEPATH = -11;
63
64
    public const ERR_SABCOMPLETEPATH = -12;
65
66
    /**
67
     * @var string
68
     */
69
    protected $primaryKey = 'name';
70
71
    /**
72
     * @var string
73
     */
74
    protected $keyType = 'string';
75
76
    /**
77
     * @var bool
78
     */
79
    protected $dateFormat = false;
80
81
    /**
82
     * @var bool
83
     */
84
    public $timestamps = false;
85
86
    /**
87
     * @var bool
88
     */
89
    public $incrementing = false;
90
91
    /**
92
     * @var array
93
     */
94
    protected $guarded = [];
95
96
    protected static $settingsCollection;
97
98
    /**
99
     * Get the value attribute and convert empty strings to null and numeric strings to numbers.
100
     *
101
     * @param  string  $value
102
     */
103
    public function getValueAttribute($value): mixed
104
    {
105
        return self::convertValue($value);
106
    }
107
108
    /**
109
     * Adapted from https://laravel.io/forum/01-15-2016-overriding-eloquent-attributes.
110
     *
111
     * @return mixed
112
     */
113
    public function __get($key)
114
    {
115
        $override = self::query()->where('name', $key)->first();
116
117
        // If there's an override and no mutator has been explicitly defined on
118
        // the model then use the override value
119
        if ($override && ! $this->hasGetMutator($key)) {
120
            return $override->value;
121
        }
122
123
        // If the attribute is not overridden the use the usual __get() magic method
124
        return parent::__get($key);
125
    }
126
127
    /**
128
     * Return a simple key-value array of all settings.
129
     *
130
     * @throws \RuntimeException
131
     */
132
    public static function toTree(bool $excludeUnsectioned = true): array
0 ignored issues
show
Unused Code introduced by
The parameter $excludeUnsectioned is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

132
    public static function toTree(/** @scrutinizer ignore-unused */ bool $excludeUnsectioned = true): array

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

Loading history...
133
    {
134
        $results = self::query()->pluck('value', 'name')->toArray();
135
136
        if (empty($results)) {
137
            throw new \RuntimeException(
138
                'No results from Settings table! Check your table has been created and populated.'
139
            );
140
        }
141
142
        return $results;
143
    }
144
145
    public static function settingValue($setting): mixed
146
    {
147
        $value = self::query()->where('name', $setting)->value('value');
148
149
        // Apply the same conversion logic as the accessor
150
        return self::convertValue($value);
151
    }
152
153
    /**
154
     * Convert setting value: numeric strings to numbers, preserve empty strings.
155
     *
156
     * @param  string|null  $value
157
     */
158
    public static function convertValue($value): mixed
159
    {
160
        // Handle null
161
        if ($value === null) {
162
            return null;
163
        }
164
165
        // Keep empty strings as empty strings (don't convert to null)
166
        // Many settings expect empty strings, not null
167
        if ($value === '') {
168
            return '';
169
        }
170
171
        // Convert numeric strings to actual numbers
172
        if (is_numeric($value)) {
173
            // Check if it's an integer or float
174
            if (strpos($value, '.') !== false) {
175
                return (float) $value;
176
            }
177
178
            return (int) $value;
179
        }
180
181
        return $value;
182
    }
183
184
    public static function settingsUpdate(array $data = []): void
185
    {
186
        foreach ($data as $key => $value) {
187
            self::query()->where('name', $key)->update(['value' => \is_array($value) ? implode(', ', $value) : $value]);
188
        }
189
    }
190
}
191