Test Failed
Push — ft/fields-refactor ( a46930...c631a4 )
by Ben
33:55
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\Arr;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Schema;
8
9
class Settings extends Collection
10
{
11
    private $values;
12
13
    /**
14
     *
15
     */
16
    public static function init()
17
    {
18
        $configValues = static::configValues();
0 ignored issues
show
Unused Code introduced by
The assignment to $configValues is dead and can be removed.
Loading history...
19
    }
20
21
    public function get($key, $locale = null, $default = null)
22
    {
23
        $this->fetch();
24
25
        if (! isset($this->values[$key])) {
26
            return $default;
27
        }
28
        
29
        if (is_array($this->values[$key])) {
30
            if ($this->values[$key]['value'] == null) {
31
                return $default;
32
            }
33
34
            return $this->values[$key]['value'];
35
        }
36
37
        return $this->values[$key];
38
    }
39
40
    public function set($key, $value)
41
    {
42
        $this->values[$key] = $value;
43
    }
44
45
    private static function configValues(): array
46
    {
47
        return Arr::dot(config('thinktomorrow.chief-settings'));
48
    }
49
50
    private function fetch()
51
    {
52
        if ($this->values) {
53
            return;
54
        }
55
56
        $config_values = static::configValues();
57
58
        $database_values = Schema::hasTable((new Setting)->getTable())
59
            ? Setting::all()->pluck('value', 'key')->toArray()
60
            : [];
61
62
        $this->values = array_merge($config_values, $database_values);
63
    }
64
65
    public function fresh()
66
    {
67
        $this->values = null;
68
69
        return $this;
70
    }
71
}
72