GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#688)
by
unknown
18:52
created

MultiSelect::isDeleteRelatedItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Collection;
7
8
class MultiSelect extends Select
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $taggable = false;
14
15
    /**
16
     * @var \Closure
17
     */
18
    protected $syncCallback;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $deleteRelatedItem = false;
24
25
    /**
26
     * @var string
27
     */
28
    protected $view = 'form.element.multiselect';
29
30
    /**
31
     * @return string
32
     */
33
    public function getName()
34
    {
35
        return parent::getName().'[]';
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function getValueFromModel()
42
    {
43
        $value = parent::getValueFromModel();
44
45
        if (is_array($value)) {
46
            foreach ($value as $key => $val) {
47
                $value[$key] = (int) $val;
48
            }
49
        }
50
51
        if ($value instanceof Collection && $value->count() > 0) {
52
            $value = $value->pluck($value->first()->getKeyName())->all();
53
        }
54
55
        if ($value instanceof Collection) {
56
            $value = $value->toArray();
57
        }
58
59
        return $value;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isTaggable()
66
    {
67
        return $this->taggable;
68
    }
69
70
    /**
71
     * @return $this
72
     */
73
    public function taggable()
74
    {
75
        $this->taggable = true;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return \Closure
82
     */
83
    public function getSyncCallback()
84
    {
85
        return $this->syncCallback;
86
    }
87
88
    /**
89
     * @param \Closure $callable
90
     * @return $this
91
     */
92
    public function setSyncCallback(\Closure $callable)
93
    {
94
        $this->syncCallback = $callable;
95
96
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isDeleteRelatedItem()
103
    {
104
        return $this->deleteRelatedItem;
105
    }
106
107
    /**
108
     * @return $this
109
     */
110
    public function deleteRelatedItem()
111
    {
112
        $this->deleteRelatedItem = true;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function toArray()
121
    {
122
        $this->setHtmlAttributes([
123
            'id'    => $this->getName(),
124
            'class' => 'form-control',
125
            'multiple',
126
        ]);
127
128
        if ($this->isTaggable()) {
129
            $this->setHtmlAttribute('class', 'input-taggable');
130
        }
131
132
        if ($this->isReadonly()) {
133
            $this->setHtmlAttribute('disabled', 'disabled');
134
        }
135
136
        return [
137
                'tagable'    => $this->isTaggable(),
138
                'attributes' => $this->getHtmlAttributes(),
139
            ] + parent::toArray();
140
    }
141
142
    /**
143
     * @param \Illuminate\Http\Request $request
144
     *
145
     * @return void
146
     */
147
    public function save(\Illuminate\Http\Request $request)
148
    {
149
        if (is_null($this->getModelForOptions())) {
150
            parent::save($request);
151
        }
152
    }
153
154
    /**
155
     * @param \Illuminate\Http\Request $request
156
     *
157
     * @return void
158
     */
159
    public function afterSave(\Illuminate\Http\Request $request)
160
    {
161
        if (is_null($this->getModelForOptions())) {
162
            return;
163
        }
164
165
        if ($this->isValueSkipped()) {
166
            return;
167
        }
168
169
        $attribute = $this->getModelAttributeKey();
170
171
        if (is_null($request->input($this->getPath()))) {
172
            $values = [];
173
        } else {
174
            $values = $this->getValueFromModel();
175
        }
176
177
        $relation = $this->getModel()->{$attribute}();
178
179
        if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) {
180
            $this->syncBelongsToManyRelation($relation, $values);
181
        } elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) {
182
            $this->deleteOldItemsFromHasManyRelation($relation, $values);
183
            $this->attachItemsToHasManyRelation($relation, $values);
184
        }
185
    }
186
187
    /**
188
     * @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation
189
     * @param array $values
190
     *
191
     * @return void
192
     */
193
    protected function syncBelongsToManyRelation(
194
        \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation,
195
        array $values
196
    ) {
197
        foreach ($values as $i => $value) {
198
            if (! array_key_exists($value, $this->getOptions()) && $this->isTaggable()) {
199
                $model = clone $this->getModelForOptions();
200
                $model->{$this->getDisplay()} = $value;
201
                $model->save();
202
203
                $values[$i] = $model->getKey();
204
            }
205
        }
206
207
        if (is_callable($callback = $this->getSyncCallback())) {
208
            $callbackModel = $this->getModel();
209
            $callback($values, $callbackModel);
210
211
            return;
212
        }
213
214
        $relation->sync($values);
215
    }
216
217
    /**
218
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
219
     * @param array $values
220
     */
221
    protected function deleteOldItemsFromHasManyRelation(
222
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
223
        array $values
224
    ) {
225
        $items = $relation->get();
226
227
        foreach ($items as $item) {
228
            if (! in_array($item->getKey(), $values)) {
229
                if ($this->isDeleteRelatedItem()) {
230
                    $item->delete();
231
                } else {
232
                    $item->{$this->getForeignKeyNameFromRelation($relation)} = null;
233
                    $item->save();
234
                }
235
            }
236
        }
237
    }
238
239
    /**
240
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
241
     * @param array $values
242
     */
243
    protected function attachItemsToHasManyRelation(
244
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
245
        array $values
246
    ) {
247
        foreach ($values as $i => $value) {
248
            /** @var Model $model */
249
            $model = clone $this->getModelForOptions();
250
            $item = $model->find($value);
251
252
            if (is_null($item)) {
253
                if (! $this->isTaggable()) {
254
                    continue;
255
                }
256
257
                $model->{$this->getDisplay()} = $value;
258
                $item = $model;
259
            }
260
261
            $relation->save($item);
262
        }
263
    }
264
}
265