Test Failed
Push — ft/fields-refactor ( 9f047d...e6f9ef )
by Ben
315:03 queued 288:46
created

Settings::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Thinktomorrow\Chief\Settings;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\Schema;
7
8
class Settings extends Collection
9
{
10
    public static function configValues(): array
11
    {
12
        return config('thinktomorrow.chief-settings');
13
    }
14
15
    public function get($key, $locale = null, $default = null)
16
    {
17
        $this->fetch();
18
19
        if (! isset($this->items[$key])) {
20
            return $default;
21
        }
22
        
23
        if (is_array($this->items[$key])) {
24
            if ($this->items[$key]['value'] == null) {
25
                return $default;
26
            }
27
28
            return $this->items[$key]['value'];
29
        }
30
31
        return $this->items[$key];
32
    }
33
34
    public function set($key, $value)
35
    {
36
        $this->items[$key] = $value;
37
    }
38
39
    private function fetch()
40
    {
41
        if ($this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            return;
43
        }
44
45
        $config_values = static::configValues();
46
47
        $database_values = Schema::hasTable((new Setting)->getTable())
48
            ? Setting::all()->pluck('value', 'key')->toArray()
49
            : [];
50
51
        $this->items = array_merge($config_values, $database_values);
52
    }
53
54
    public function fresh()
55
    {
56
        $this->items = null;
57
58
        return $this;
59
    }
60
}
61