Test Setup Failed
Push — ft/duplicate-page ( 3cb9e2 )
by Ben
65:03 queued 56:56
created

Settings::get()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 27
rs 8.4444
cc 8
nc 7
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Settings;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\Schema;
9
10
class Settings extends Collection
11
{
12
    public static function configValues(): array
13
    {
14
        return config('thinktomorrow.chief-settings');
15
    }
16
17
    public function get($key, $locale = null, $default = null)
18
    {
19
        $this->fetch();
20
21
        if (!isset($this->items[$key])) {
22
            return $default;
23
        }
24
25
        // non-assoc array of items
26
        if(is_array($this->items[$key]) && key($this->items[$key]) === 0) {
27
            return $this->items[$key];
28
        }
29
30
        // Array of localized items
31
        if (is_array($this->items[$key])) {
32
            if (!$locale) {
33
                $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

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