Passed
Push — dependabot/npm_and_yarn/@vue/t... ( 1a0b23...9776fc )
by
unknown
84:45 queued 64:29
created

Settings::set()   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 2
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 (!$locale) {
25
                $locale = app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

25
                $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
26
            }
27
28
            if ($this->items[$key] == null || !isset($this->items[$key][$locale])) {
29
                return $default;
30
            }
31
32
            return $this->items[$key][$locale] ?? $default;
33
        }
34
35
        return $this->items[$key];
36
    }
37
38
    public function set($key, $value)
39
    {
40
        $this->items[$key] = $value;
41
    }
42
43
    private function fetch()
44
    {
45
        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...
46
            return;
47
        }
48
49
        $config_values = static::configValues();
50
51
        $database_values = Schema::hasTable((new Setting)->getTable())
52
            ? Setting::all()->pluck('value', 'key')->toArray()
53
            : [];
54
55
        $this->items = array_merge($config_values, $database_values);
56
    }
57
58
    public function fresh()
59
    {
60
        $this->items = null;
61
62
        return $this;
63
    }
64
}
65