Completed
Push — master ( f05a87...6b2b65 )
by wen
14:04
created

MultiSelect::finishSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
c 1
b 0
f 0
rs 9.6666
cc 3
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
8
class MultiSelect extends Select
9
{
10
    protected $defaultValue = [];
11
12
    public function getValue()
13
    {
14
        $value = $this->getValueFromModel();
15
        if (empty($value)) {
16
            return $value;
17
        }
18
19
        if ($this->isOptionsModel() && $this->isRelation()) {
20
            $model = $this->getOptionsModel();
21
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
22
23
            $value = $value->pluck($key)->map(function ($item) {
24
                return (string)$item;
25
            });
26
        }
27
        return $value;
28
    }
29
30
    protected function isOptionsModel()
31
    {
32
        return is_string($this->options) || $this->options instanceof Model;
33
    }
34
35
    protected function isRelation()
36
    {
37
        return method_exists($this->getModel(), $this->getName());
38
    }
39
40
    public function save()
41
    {
42
        if (!($this->isOptionsModel() && $this->isRelation())) {
43
            parent::save();
44
        }
45
    }
46
47
    public function finishSave()
48
    {
49
        if (!($this->isOptionsModel() && $this->isRelation())) {
50
            return;
51
        }
52
        $attribute = $this->getName();
53
54
        $relation = $this->getModel()->{$attribute}();
0 ignored issues
show
Unused Code introduced by
$relation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
    }
56
57
    public function toArray()
58
    {
59
        return parent::toArray() + [
60
                'multiple' => true,
61
            ];
62
    }
63
}
64