Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

Settings::get()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.2222
cc 6
nc 6
nop 3
crap 6
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 13
    public static function configValues(): array
11
    {
12 13
        return config('thinktomorrow.chief-settings');
13
    }
14
15 13
    public function get($key, $locale = null, $default = null)
16
    {
17 13
        $this->fetch();
18
19 13
        if (!isset($this->items[$key])) {
20 4
            return $default;
21
        }
22
23 9
        if (is_array($this->items[$key])) {
24 5
            if (!$locale) {
25 4
                $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 5
            if ($this->items[$key] == null || !isset($this->items[$key][$locale])) {
29 1
                return $default;
30
            }
31
32 5
            return $this->items[$key][$locale] ?? $default;
33
        }
34
35 4
        return $this->items[$key];
36
    }
37
38 1
    public function set($key, $value)
39
    {
40 1
        $this->items[$key] = $value;
41 1
    }
42
43 13
    private function fetch()
44
    {
45 13
        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 6
            return;
47
        }
48
49 13
        $config_values = static::configValues();
50
51 13
        $database_values = Schema::hasTable((new Setting)->getTable())
52 13
            ? Setting::all()->pluck('value', 'key')->toArray()
53 13
            : [];
54
55 13
        $this->items = array_merge($config_values, $database_values);
56 13
    }
57
58 2
    public function fresh()
59
    {
60 2
        $this->items = null;
61
62 2
        return $this;
63
    }
64
}
65