Total Complexity | 7 |
Total Lines | 65 |
Duplicated Lines | 0 % |
Coverage | 93.75% |
Changes | 0 |
1 | <?php |
||
14 | class Setting extends Model |
||
15 | { |
||
16 | protected $primaryKey = 'key'; |
||
17 | |||
18 | public $timestamps = false; |
||
19 | |||
20 | protected $guarded = []; |
||
21 | |||
22 | /** |
||
23 | * Get a setting value. |
||
24 | * |
||
25 | * @param string $key |
||
26 | * |
||
27 | * @return mixed|string |
||
28 | */ |
||
29 | 3 | public static function get(string $key) |
|
30 | { |
||
31 | 3 | if ($record = self::find($key)) { |
|
32 | 3 | return $record->value; |
|
33 | } |
||
34 | |||
35 | return ''; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Set a setting (no pun) value. |
||
40 | * |
||
41 | * @param string|array $key The key of the setting, or an associative array of settings, |
||
42 | * in which case $value will be discarded. |
||
43 | * @param mixed $value |
||
44 | */ |
||
45 | 132 | public static function set($key, $value = null): void |
|
46 | { |
||
47 | 132 | if (is_array($key)) { |
|
48 | 1 | foreach ($key as $k => $v) { |
|
49 | 1 | self::set($k, $v); |
|
50 | } |
||
51 | |||
52 | 1 | return; |
|
53 | } |
||
54 | |||
55 | 132 | self::updateOrCreate(compact('key'), compact('value')); |
|
56 | 132 | } |
|
57 | |||
58 | /** |
||
59 | * Serialize the setting value before saving into the database. |
||
60 | * This makes settings more flexible. |
||
61 | * |
||
62 | * @param mixed $value |
||
63 | */ |
||
64 | 132 | public function setValueAttribute($value): void |
|
65 | { |
||
66 | 132 | $this->attributes['value'] = serialize($value); |
|
67 | 132 | } |
|
68 | |||
69 | /** |
||
70 | * Get the unserialized setting value. |
||
71 | * |
||
72 | * @param mixed $value |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | 3 | public function getValueAttribute($value) |
|
79 | } |
||
80 | } |
||
81 |