Completed
Push — master ( c139be...7e212d )
by wen
14:16
created

SelectOptionsFromModel::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 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use InvalidArgumentException;
7
8
trait SelectOptionsFromModel
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $optionsLabelAttribute;
14
15
    /**
16
     * @var string
17
     */
18
    protected $optionsValueAttribute;
19
20
    /**
21
     * Get the options label attribute.
22
     *
23
     * @return string
24
     */
25
    public function getOptionsLabelAttribute()
26
    {
27
        return $this->optionsLabelAttribute;
28
    }
29
30
    /**
31
     * Set the options label attribute.
32
     *
33
     * @param string $value
34
     *
35
     * @return $this
36
     */
37
    public function setOptionsLabelAttribute($value)
38
    {
39
        $this->optionsLabelAttribute = $value;
40
41
        return $this;
42
    }
43
44
    /**
45
     * Get the options value attribute.
46
     *
47
     * @return string
48
     */
49
    public function getOptionsValueAttribute()
50
    {
51
        return $this->optionsValueAttribute;
52
    }
53
54
    /**
55
     * Set the options value attribute.
56
     *
57
     * @param string $value
58
     *
59
     * @return $this
60
     */
61
    public function setOptionsValueAttribute($value)
62
    {
63
        $this->optionsValueAttribute = $value;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Get the options model.
70
     *
71
     * @return Model
72
     */
73 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...
74
    {
75
        $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...
76
77
        if (is_string($model)) {
78
            $model = app($model);
79
        }
80
81
        if (! ($model instanceof Model)) {
82
            throw new InvalidArgumentException(
83
                sprintf(
84
                    'The %s element[%s] options class must be instanced of "%s".',
85
                    $this->getType(),
0 ignored issues
show
Bug introduced by
It seems like getType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
86
                    $this->getName(),
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
                    Model::class
88
                )
89
            );
90
        }
91
92
        return $model;
93
    }
94
95
    protected function isOptionsModel()
96
    {
97
        return is_string($this->options) || $this->options instanceof Model;
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...
98
    }
99
100
    /**
101
     * Get the options from model.
102
     *
103
     * @return \Illuminate\Support\Collection
104
     */
105
    protected function getOptionsFromModel()
106
    {
107
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
108
            throw new InvalidArgumentException(
109
                sprintf(
110
                    'The %s element[%s] options must set label attribute',
111
                    $this->getType(),
0 ignored issues
show
Bug introduced by
It seems like getType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
112
                    $this->getName()
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
113
                )
114
            );
115
        }
116
117
        $model = $this->getOptionsModel();
118
119
        /**
120
         * @var \Illuminate\Support\Collection $results
121
         */
122
        $results = $model->get();
123
124
        $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
125
126
        return $results->pluck($label, $key);
127
    }
128
}
129