Passed
Push — 0.3 ( 5a9264...9cb195 )
by Ben
115:24 queued 63:34
created

Setting::refreshFieldsFromConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Thinktomorrow\Chief\Settings;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Thinktomorrow\Chief\Fields\Types\InputField;
7
8
class Setting extends Model
9
{
10
    public $table = 'settings';
11
    public $timestamps = false;
12
    public $guarded = [];
13
14
    private static $fieldsFromConfig;
15
16 2
    public function getFieldAttribute()
17
    {
18 2
        $fields = $this->fieldsFromConfig();
19
20
        // TODO: a fieldgroup should be used here to filter / find by key
21 2
        foreach ($fields as $field) {
22 2
            if (is_callable($field)) {
23
                $field = call_user_func($field);
24
            }
25
26 2
            if ($field->key != $this->key) {
27 1
                continue;
28
            }
29
30 1
            return $field;
31
        }
32
33 1
        return $this->defaultField();
34
    }
35
36 2
    private static function fieldsFromConfig()
37
    {
38 2
        if (static::$fieldsFromConfig) {
0 ignored issues
show
Bug introduced by
Since $fieldsFromConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $fieldsFromConfig to at least protected.
Loading history...
39 1
            return static::$fieldsFromConfig;
40
        }
41
42 2
        return static::$fieldsFromConfig = config('thinktomorrow.chief.settingFields', []);
43
    }
44
45 1
    private function defaultField()
46
    {
47 1
        return InputField::make($this->key)
48 1
                    ->label(ucfirst(str_replace(['-','_','.'], ' ', $this->key)));
0 ignored issues
show
Bug introduced by
The method label() does not exist on Thinktomorrow\Chief\Fields\Types\InputField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

48
                    ->/** @scrutinizer ignore-call */ label(ucfirst(str_replace(['-','_','.'], ' ', $this->key)));
Loading history...
49
    }
50
51 1
    public static function refreshFieldsFromConfig()
52
    {
53 1
        static::$fieldsFromConfig = null;
0 ignored issues
show
Bug introduced by
Since $fieldsFromConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $fieldsFromConfig to at least protected.
Loading history...
54 1
    }
55
}
56