Completed
Push — master ( 545216...bb1888 )
by wen
14:38
created

MultiSelect::isOptionsModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6
7
class MultiSelect extends Select
8
{
9
    protected $defaultValue = [];
10
11
    public function getValue()
12
    {
13
        $value = $this->getValueFromModel();
14
        if (empty($value)) {
15
            return [];
16
        }
17
18
        if ($this->isOptionsModel() && $this->isRelation()) {
19
            $model = $this->getOptionsModel();
20
            $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
21
22
            return $value->pluck($key)->map(function ($item) {
23
                return (string)$item;
24
            });
25
        } elseif (is_string($value)) {
26
            if (strpos($value, ',') !== false) {
27
                return explode(',', $value);
28
            }
29
            return (array)$value;
30
        }
31
    }
32
33
    public function save()
34
    {
35
        if (!($this->isOptionsModel() && $this->isRelation())) {
36
            parent::save();
37
        }
38
    }
39
40 View Code Duplication
    public function finishSave()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        if (!($this->isOptionsModel() && $this->isRelation())) {
43
            return;
44
        }
45
        $attribute = $this->getName();
46
        $values = $this->getValueFromRequest();
47
48
        $relation = $this->getModel()->{$attribute}();
49
        if ($relation instanceof BelongsToMany) {
50
            $relation->sync($values);
51
        }
52
    }
53
54
    protected function isOptionsModel()
55
    {
56
        return is_string($this->options) || $this->options instanceof Model;
0 ignored issues
show
Bug introduced by
The class Sco\Admin\Form\Elements\Model does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
    }
58
59
    protected function isRelation()
60
    {
61
        return method_exists($this->getModel(), $this->getName());
62
    }
63
64
    public function toArray()
65
    {
66
        return parent::toArray() + [
67
                'multiple' => true,
68
            ];
69
    }
70
}
71