Radio::afterUpdate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
ccs 0
cts 9
cp 0
crap 12
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Yaro\Jarboe\Table\Fields\Traits\Orderable;
8
use Yaro\Jarboe\Table\Fields\Traits\Relations;
9
10
class Radio extends AbstractField
0 ignored issues
show
Bug introduced by
The type Yaro\Jarboe\Table\Fields\AbstractField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
{
12
    use Orderable;
13
    use Relations;
14
15
    protected $columns = 1;
16
17
    public function isCurrentOption($option, $model = null, $relationIndex = 0): bool
18
    {
19
        if ($this->hasOld()) {
20
            if ($this->isGroupedRelation()) {
21
                $option = crc32($this->relations[$relationIndex]['group']) .'~~~'. $option;
22
            }
23
            return $this->old() == $option;
24
        }
25
26
        if (is_null($model)) {
27
            return $option == $this->getDefault();
28
        }
29
30
        if ($this->isRelationField()) {
31
            $related = $model->{$this->getRelationMethod($relationIndex)};
32
            if ($related) {
33
                $relatedModelClass = get_class($model->{$this->getRelationMethod($relationIndex)}()->getRelated());
34
                $freshRelatedModel = new $relatedModelClass;
35
                $collection = $related;
36
                if (!is_a($related, Collection::class)) {
37
                    $collection = collect([$related]);
38
                }
39
40
                return $collection->contains($freshRelatedModel->getKeyName(), $option);
41
            }
42
            return false;
43
        }
44
45
        return $option == $model->{$this->name};
46
    }
47
48
    public function afterStore($model, Request $request)
49
    {
50
        $this->afterUpdate($model, $request);
51
    }
52
53
    public function afterUpdate($model, Request $request)
54
    {
55
        if (!$this->isRelationField()) {
56
            return;
57
        }
58
        if ($this->isReadonly()) {
59
            return;
60
        }
61
62
        $this->syncRelations($model, $request->get($this->name()));
63
    }
64
65
    public function columns(int $columns)
66
    {
67
        $this->columns = $columns;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return int
74
     */
75
    public function getColumns(): int
76
    {
77
        return $this->columns;
78
    }
79
80
    public function getListView($model)
81
    {
82
        return view('jarboe::crud.fields.radio.list', [
83
            'model' => $model,
84
            'field' => $this,
85
            'options' => $this->getOptions(),
86
        ]);
87
    }
88
89
    public function getEditFormView($model)
90
    {
91
        $template = $this->isReadonly() ? 'readonly' : 'edit';
92
93
        return view('jarboe::crud.fields.radio.'. $template, [
94
            'model' => $model,
95
            'field' => $this,
96
        ]);
97
    }
98
99
    public function getCreateFormView()
100
    {
101
        return view('jarboe::crud.fields.radio.create', [
102
            'field' => $this,
103
        ]);
104
    }
105
}
106