Passed
Push — master ( 00089d...cdf2c0 )
by Philippe
08:10 queued 12s
created

Settings::fetch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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