Cancelled
Push — 0.5 ( d49ebd...32819b )
by Philippe
36s queued 36s
created

Settings::get()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 14
cp 1
rs 8.4444
cc 8
nc 7
nop 3
crap 8
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 52
    public static function configValues(): array
13
    {
14 52
        return config('thinktomorrow.chief-settings');
15
    }
16
17 52
    public function get($key, $locale = null, $default = null)
18
    {
19 52
        $this->fetch();
20
21 52
        if (!isset($this->items[$key])) {
22 40
            return $default;
23
        }
24
25 13
        // non-assoc array of items
26 6
        if (is_array($this->items[$key]) && key($this->items[$key]) === 0) {
27 5
            return $this->items[$key];
28
        }
29
30 6
        // Array of localized items
31 2
        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 6
            }
35
36
            if ($this->items[$key] == null || !isset($this->items[$key][$locale])) {
37 7
                return $default;
38
            }
39
40 1
            return $this->items[$key][$locale] ?? $default;
41
        }
42 1
43 1
        return $this->items[$key];
44
    }
45 52
46
    public function set($key, $value)
47 52
    {
48 13
        $this->items[$key] = $value;
49
    }
50
51 52
    private function fetch()
52
    {
53 52
        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 52
            return;
55 52
        }
56
57 52
        $config_values = static::configValues();
58 52
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 2
    }
65
66
    public function fresh()
67
    {
68
        $this->items = null;
69
70
        return $this;
71
    }
72
}
73