Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

Radio::afterStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
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 View Code Duplication
            if ($this->isGroupedRelation()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        if ($this->isRelationField()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 shouldSkip(Request $request)
49
    {
50
        return $this->isRelationField();
51
    }
52
53
    public function afterStore($model, Request $request)
54
    {
55
        $this->afterUpdate($model, $request);
56
    }
57
58 View Code Duplication
    public function afterUpdate($model, Request $request)
59
    {
60
        if (!$this->isRelationField()) {
61
            return;
62
        }
63
        if ($this->isReadonly()) {
64
            return;
65
        }
66
67
        $this->syncRelations($model, $request->get($this->name()));
68
    }
69
70
    public function columns(int $columns)
71
    {
72
        $this->columns = $columns;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getColumns(): int
81
    {
82
        return $this->columns;
83
    }
84
85
    public function getListView($model)
86
    {
87
        return view('jarboe::crud.fields.radio.list', [
88
            'model' => $model,
89
            'field' => $this,
90
            'options' => $this->getOptions(),
91
        ]);
92
    }
93
94
    public function getEditFormView($model)
95
    {
96
        $template = $this->isReadonly() ? 'readonly' : 'edit';
97
98
        return view('jarboe::crud.fields.radio.'. $template, [
99
            'model' => $model,
100
            'field' => $this,
101
        ]);
102
    }
103
104
    public function getCreateFormView()
105
    {
106
        return view('jarboe::crud.fields.radio.create', [
107
            'field' => $this,
108
        ]);
109
    }
110
}
111