Completed
Push — master ( a76360...3b8ebd )
by Manuel
04:51 queued 03:32
created

OptionField   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A make() 0 23 2
1
<?php
2
3
namespace Oscer\Cms\Backend\Resources\Fields;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Str;
8
9
class OptionField
10
{
11
    public static function make(string $key, string $type)
12
    {
13
        [$tab, $label] = explode('.', $key);
0 ignored issues
show
Bug introduced by
The variable $tab does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $label does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
14
15
        $resolveValueCallback = function (Field $field) {
16
            // We access the value property of the Option model
17
            return $field->model->value;
18
        };
19
20
        $fillResourceCallback = function (Model $model, Request $request) {
21
            // We need to replace the "." with an "_" because dot notation acts like array access
22
            $value = $request->input(Str::replaceFirst('.', '_', $model->key));
23
            // We Write the value into the value property of the Option model
24
            $model->value = $value;
25
        };
26
27
        switch ($type) {
28
            case 'text':
29
                return TextField::make($key, $label, $resolveValueCallback, $fillResourceCallback);
30
            default:
31
                throw new \Exception('unknown field type');
32
        }
33
    }
34
}
35