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
Push — bs4 ( 193743...d4da84 )
by butschster
20:32 queued 14:27
created

FormElementController::selectSearch()   F

Complexity

Conditions 24
Paths 154

Size

Total Lines 129
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 24
eloc 86
c 1
b 1
f 0
nc 154
nop 4
dl 0
loc 129
rs 3.7166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SleepingOwl\Admin\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Database\Eloquent\Model;
9
use SleepingOwl\Admin\Form\Element\SelectAjax;
10
use SleepingOwl\Admin\Form\Element\DependentSelect;
11
use SleepingOwl\Admin\Form\Element\MultiSelectAjax;
12
use SleepingOwl\Admin\Form\Element\MultiDependentSelect;
13
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
14
15
class FormElementController extends Controller
16
{
17
    /**
18
     * @param ModelConfigurationInterface $model
19
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
20
     * @return JsonResponse|mixed
21
     */
22
    public function getModelLogic(ModelConfigurationInterface $model, $id = null)
23
    {
24
        if (! is_null($id)) {
0 ignored issues
show
introduced by
The condition is_null($id) is always true.
Loading history...
25
            $item = $model->getRepository()->find($id);
26
            if (is_null($item) || ! $model->isEditable($item)) {
27
                return new JsonResponse([
28
                    'message' => trans('lang.message.access_denied'),
29
                ], 403);
30
            }
31
32
            return $model->fireEdit($id);
33
        }
34
35
        if (! $model->isCreatable()) {
36
            return new JsonResponse([
37
                'message' => trans('lang.message.access_denied'),
38
            ], 403);
39
        }
40
41
        return $model->fireCreate();
42
    }
43
44
    /**
45
     * @param ModelConfigurationInterface $model
46
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
47
     * @param mixed $payload
48
     * @return JsonResponse|mixed
49
     */
50
    public function getModelLogicPayload(ModelConfigurationInterface $model, $id = null, $payload = [])
51
    {
52
        if (! is_null($id)) {
0 ignored issues
show
introduced by
The condition is_null($id) is always true.
Loading history...
53
            $item = $model->getRepository()->find($id);
54
            if (is_null($item) || ! $model->isEditable($item)) {
55
                return new JsonResponse([
56
                    'message' => trans('lang.message.access_denied'),
57
                ], 403);
58
            }
59
60
            return $model->fireEdit($id, $payload);
61
        }
62
63
        if (! $model->isCreatable()) {
64
            return new JsonResponse([
65
                'message' => trans('lang.message.access_denied'),
66
            ], 403);
67
        }
68
69
        return $model->fireCreate($payload);
0 ignored issues
show
Unused Code introduced by
The call to SleepingOwl\Admin\Contra...Interface::fireCreate() has too many arguments starting with $payload. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        return $model->/** @scrutinizer ignore-call */ fireCreate($payload);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
70
    }
71
72
    /**
73
     * @param Request $request
74
     * @param ModelConfigurationInterface $model
75
     * @param string $field
76
     * @param int|null $id
77
     *
78
     * @return JsonResponse
79
     */
80
    public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null)
81
    {
82
        $form = $this->getModelLogic($model, $id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type integer; however, parameter $id of SleepingOwl\Admin\Http\C...roller::getModelLogic() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $form = $this->getModelLogic($model, /** @scrutinizer ignore-type */ $id);
Loading history...
83
84
        if ($form instanceof JsonResponse) {
85
            return $form;
86
        }
87
88
        // because field name in MultiDependentSelect ends with '[]'
89
        $fieldPrepared = str_replace('[]', '', $field);
90
91
        /** @var DependentSelect|MultiDependentSelect $element */
92
        $element = $form->getElement($fieldPrepared);
93
94
        if (is_null($element)) {
95
            return new JsonResponse([
96
                'message' => 'Element not found',
97
            ], 404);
98
        }
99
100
        $element->setAjaxParameters(
101
            $request->input('depdrop_all_params', [])
102
        );
103
104
        $options = $element->getOptions();
105
106
        if ($element->isNullable()) {
107
            $options = [null => trans('sleeping_owl::lang.select.nothing')] + $options;
108
        }
109
110
        return new JsonResponse([
111
            'output' => collect($options)->map(function ($value, $key) {
112
                return ['id' => $key, 'name' => $value];
113
            }),
114
            'selected' => $element->getValueFromModel(),
115
        ]);
116
    }
117
118
    /**
119
     * @param Request $request
120
     * @param ModelConfigurationInterface $model
121
     * @param string $field
122
     * @param int|null $id
123
     *
124
     * @return JsonResponse
125
     */
126
    public function multiselectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
127
    {
128
        return $this->selectSearch($request, $model, $field, $id);
129
    }
130
131
    /**
132
     * @param Request $request
133
     * @param ModelConfigurationInterface $model
134
     * @param string $field
135
     * @param int|null $id
136
     *
137
     * @return JsonResponse
138
     */
139
    public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
140
    {
141
        $form = $this->getModelLogic($model, $id);
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type integer; however, parameter $id of SleepingOwl\Admin\Http\C...roller::getModelLogic() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        $form = $this->getModelLogic($model, /** @scrutinizer ignore-type */ $id);
Loading history...
142
143
        if ($form instanceof JsonResponse) {
144
            return $form;
145
        }
146
147
        // because field name in MultiSelectAjax ends with '[]'
148
        $fieldPrepared = str_replace('[]', '', $field);
149
        // process fields with relations: user[role]
150
        $fieldPrepared = strtr($fieldPrepared, ['[' => '.', ']' => '']);
151
152
        /** @var SelectAjax|MultiSelectAjax $element */
153
        $element = $form->getElement($fieldPrepared);
154
155
        if (is_null($element)) {
156
            return new JsonResponse([
157
                'message' => 'Element not found',
158
            ], 404);
159
        }
160
161
        $params = $request->input('depdrop_all_params', []);
162
        $temp = [];
163
        foreach ($params as $key => $val) {
164
            $key = strtr($key, ['[' => '.', ']' => '']);
165
            $key = strtr($key, ['__' => '.']);
166
            $temp[$key] = $val;
167
        }
168
        $params = $temp;
169
170
        $element->setAjaxParameters($params);
171
172
        if (is_callable($closure = $element->getModelForOptionsCallback())) {
173
            $model_classname = $closure($element);
174
        } else {
175
            $model_classname = $element->getModelForOptions();
176
        }
177
        if (is_object($model_classname)) {
178
            $model_classname = get_class($model_classname);
179
        }
180
        if ($model_classname && class_exists($model_classname)) {
181
            $model = new $model_classname;
182
183
            $search = $element->getSearch();
184
            if (is_callable($search)) {
185
                $search = $search($element);
186
            }
187
            $display = $element->getDisplay();
188
            $custom_name = $element->getCustomName();
189
            $exclude = $element->getExclude();
190
191
            if ($request->q && is_object($model)) {
192
                $query = $model;
193
194
                // search logic
195
                $model_table = $model->getTable();
196
                $q = $request->q;
197
                if (is_array($search)) {
198
                    $query = $query->where(function ($query) use ($model_table, $search, $q) {
199
                        foreach ($search as $key => $val) {
200
                            if (is_numeric($key)) {
201
                                $srch = $val;
202
                                $value = '%'.$q.'%';
203
                            } else {
204
                                $srch = $key;
205
                                switch ($val) {
206
                                    case 'equal':
207
                                        $value = $q;
208
                                        break;
209
                                    case 'begins_with':
210
                                        $value = $q.'%';
211
                                        break;
212
                                    case 'ends_with':
213
                                        $value = '%'.$q;
214
                                        break;
215
                                    case 'contains':
216
                                    default:
217
                                        $value = '%'.$q.'%';
218
                                        break;
219
                                }
220
                            }
221
                            $query = $query->orWhere($model_table.'.'.$srch, 'LIKE', $value);
222
                        }
223
                    });
224
                } else {
225
                    $query = $query->where($model_table.'.'.$search, 'LIKE', "%{$request->q}%");
226
                }
227
228
                // exclude result elements by id
229
                if (count($exclude)) {
230
                    $query = $query->whereNotIn($model_table.'.'.$model->getKeyName(), $exclude);
231
                }
232
233
                // call the pre load options query preparer if has be set
234
                if (is_callable($preparer = $element->getLoadOptionsQueryPreparer())) {
235
                    $query = $preparer($element, $query);
236
                }
237
238
                return new JsonResponse(
239
                    $query
240
                        ->get()
241
                        ->map(function (Model $item) use ($display, $element, $custom_name) {
242
                            if (is_string($display)) {
0 ignored issues
show
introduced by
The condition is_string($display) is always true.
Loading history...
243
                                $value = $item->{$display};
244
                            } elseif (is_callable($display)) {
245
                                $value = $display($item, $element);
246
                            } else {
247
                                $value = null;
248
                            }
249
                            if (is_string($custom_name)) {
250
                                $custom_name_value = $item->{$custom_name};
251
                            } elseif (is_callable($custom_name)) {
252
                                $custom_name_value = $custom_name($item, $element);
253
                            } else {
254
                                $custom_name_value = null;
255
                            }
256
257
                            return [
258
                                'tag_name' => $value,
259
                                'id' => $item->id,
260
                                'custom_name' => $custom_name_value,
261
                            ];
262
                        })
263
                );
264
            }
265
        }
266
267
        return new JsonResponse([]);
268
    }
269
}
270