Passed
Push — ft/fields-refactor ( 34e13a...220f3b )
by Ben
81:47
created

Setting::defaultField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
    const HOMEPAGE = 'homepage';
11
12
    public $table = 'settings';
13
    public $timestamps = false;
14
    public $guarded = [];
15
    public $casts = [
16
        'value' => 'json',
17
    ];
18
19
    private static $fieldsFromConfig;
20
21 6
    public static function findByKey(string $key)
22
    {
23 6
        return static::where('key', $key)->first();
24
    }
25
26 2
    public function getFieldAttribute()
27
    {
28 2
        $fields = $this->fieldsFromConfig();
29
30
        // TODO: a fieldgroup should be used here to filter / find by key
31 2
        foreach ($fields as $field) {
32 1
            if (is_callable($field)) {
33
                $field = call_user_func($field);
34
            }
35
36 1
            if ($field->key != $this->key) {
37
                continue;
38
            }
39
40 1
            return $field;
41
        }
42
43 1
        return $this->defaultField();
44
    }
45
46 2
    private static function fieldsFromConfig()
47
    {
48 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...
49
            return static::$fieldsFromConfig;
50
        }
51
52 2
        return static::$fieldsFromConfig = config('thinktomorrow.chief.settingFields', []);
53
    }
54
55 1
    private function defaultField()
56
    {
57 1
        return InputField::make($this->key)
58 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

58
                    ->/** @scrutinizer ignore-call */ label(ucfirst(str_replace(['-','_','.'], ' ', $this->key)));
Loading history...
59
    }
60
61 1
    public static function refreshFieldsFromConfig()
62
    {
63 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...
64 1
    }
65
}
66