Completed
Push — master ( d781ee...2f5f2b )
by wen
26:40
created

SelectOptionsFromModel::setOptionsLabelAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use InvalidArgumentException;
7
8
trait SelectOptionsFromModel
9
{
10
    protected $optionsLabelAttribute;
11
12
    protected $optionsValueAttribute;
13
14
    /**
15
     * 获取 options 标题字段
16
     *
17
     * @return string
18
     */
19
    public function getOptionsLabelAttribute()
20
    {
21
        return $this->optionsLabelAttribute;
22
    }
23
24
    /**
25
     * 设置 options 标题字段
26
     *
27
     * @param string $value
28
     *
29
     * @return $this
30
     */
31
    public function setOptionsLabelAttribute($value)
32
    {
33
        $this->optionsLabelAttribute = $value;
34
35
        return $this;
36
    }
37
38
    /**
39
     * 获取 options value 字段
40
     *
41
     * @return string
42
     */
43
    public function getOptionsValueAttribute()
44
    {
45
        return $this->optionsValueAttribute;
46
    }
47
48
    /**
49
     * 设置 options value 字段
50
     *
51
     * @param string $value
52
     *
53
     * @return $this
54
     */
55
    public function setOptionsValueAttribute($value)
56
    {
57
        $this->optionsValueAttribute = $value;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return Model
64
     */
65 View Code Duplication
    public function getOptionsModel()
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...
66
    {
67
        $model = $this->options;
0 ignored issues
show
Bug introduced by
The property options does not seem to exist. Did you mean optionsLabelAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
69
        if (is_string($model)) {
70
            $model = app($model);
71
        }
72
73
        if (!($model instanceof Model)) {
74
            throw new InvalidArgumentException(
75
                sprintf(
76
                    'The select options class must be instanced of "%s".',
77
                    Model::class
78
                )
79
            );
80
        }
81
82
        return $model;
83
    }
84
85
    /**
86
     *
87
     * @return array
88
     */
89
    protected function setOptionsFromModel()
90
    {
91
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
92
            throw new InvalidArgumentException('The select options must set label attribute');
93
        }
94
95
        $model = $this->getOptionsModel();
96
97
        // $repository = app(RepositoryInterface::class);
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
        // $repository->setModel($model);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
100
        // $results = $repository->getQuery()->get();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
        /**
102
         * @var \Illuminate\Support\Collection $results
103
         */
104
        $results = $model->get();
105
106
        $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
107
108
        return $results->pluck($label, $key);
109
    }
110
}
111