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 — master ( b864c9...ed9166 )
by butschster
12:41
created

FormElementController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 164
Duplicated Lines 54.88 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 90
loc 164
c 0
b 0
f 0
ccs 0
cts 113
cp 0
rs 10
wmc 23
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
C dependentSelect() 0 45 7
C multiselectSearch() 45 45 8
C selectSearch() 45 45 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 SleepingOwl\Admin\Form\Element\DependentSelect;
9
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
10
11
class FormElementController extends Controller
12
{
13
    /**
14
     * @param Request $request
15
     * @param ModelConfigurationInterface $model
16
     * @param string $field
17
     * @param int|null $id
18
     *
19
     * @return JsonResponse
20
     */
21
    public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null)
22
    {
23
        if (! is_null($id)) {
24
            $item = $model->getRepository()->find($id);
25
            if (is_null($item) || ! $model->isEditable($item)) {
26
                return new JsonResponse([
27
                    'message' => trans('lang.message.access_denied'),
28
                ], 403);
29
            }
30
31
            $form = $model->fireEdit($id);
32
        } else {
33
            if (! $model->isCreatable()) {
34
                return new JsonResponse([
35
                    'message' => trans('lang.message.access_denied'),
36
                ], 403);
37
            }
38
39
            $form = $model->fireCreate();
40
        }
41
42
        /** @var DependentSelect $element */
43
        if (is_null($element = $form->getElement($field))) {
44
            return new JsonResponse([
45
                'message' => 'Element not found',
46
            ], 404);
47
        }
48
49
        $element->setAjaxParameters(
50
            $request->input('depdrop_all_params', [])
51
        );
52
53
        $options = $element->getOptions();
54
55
        if ($element->isNullable()) {
56
            $options = [null => trans('sleeping_owl::lang.select.nothing')] + $options;
57
        }
58
59
        return new JsonResponse([
60
            'output' => collect($options)->map(function ($value, $key) {
61
                return ['id' => $key, 'name' => $value];
62
            }),
63
            'selected' => $element->getValueFromModel(),
64
        ]);
65
    }
66
67
    /**
68
     * @param Request $request
69
     * @param ModelConfigurationInterface $model
70
     * @param string $field
71
     * @param int|null $id
72
     *
73
     * @return JsonResponse
74
     */
75 View Code Duplication
    public function multiselectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
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...
76
    {
77
        if (! is_null($id)) {
78
            $item = $model->getRepository()->find($id);
79
            if (is_null($item) || ! $model->isEditable($item)) {
80
                return new JsonResponse([
81
                    'message' => trans('lang.message.access_denied'),
82
                ], 403);
83
            }
84
85
            $form = $model->fireEdit($id);
86
        } else {
87
            if (! $model->isCreatable()) {
88
                return new JsonResponse([
89
                    'message' => trans('lang.message.access_denied'),
90
                ], 403);
91
            }
92
93
            $form = $model->fireCreate();
94
        }
95
96
        /** @var DependentSelect $element */
97
        if (is_null($element = $form->getElement($field))) {
98
            return new JsonResponse([
99
                'message' => 'Element not found',
100
            ], 404);
101
        }
102
103
        $field = $request->field;
104
        $model = new $request->model;
105
106
        if ($request->q && is_object($model)) {
107
            return new JsonResponse(
108
                $model::where($request->field, 'like', "%{$request->q}%")
109
                    ->get()
110
                    ->map(function ($item) use ($field) {
111
                        return [
112
                            'tag_name'    => $item->{$field},
113
                            'id'          => $item->id,
114
                            'custom_name' => $item->custom_name,
115
                        ];
116
                    })
117
            );
118
        }
119
    }
120
121
    /**
122
     * @param Request $request
123
     * @param ModelConfigurationInterface $model
124
     * @param string $field
125
     * @param int|null $id
126
     *
127
     * @return JsonResponse
128
     */
129 View Code Duplication
    public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null)
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...
130
    {
131
        if (! is_null($id)) {
132
            $item = $model->getRepository()->find($id);
133
            if (is_null($item) || ! $model->isEditable($item)) {
134
                return new JsonResponse([
135
                    'message' => trans('lang.message.access_denied'),
136
                ], 403);
137
            }
138
139
            $form = $model->fireEdit($id);
140
        } else {
141
            if (! $model->isCreatable()) {
142
                return new JsonResponse([
143
                    'message' => trans('lang.message.access_denied'),
144
                ], 403);
145
            }
146
147
            $form = $model->fireCreate();
148
        }
149
150
        /** @var DependentSelect $element */
151
        if (is_null($element = $form->getElement($field))) {
152
            return new JsonResponse([
153
                'message' => 'Element not found',
154
            ], 404);
155
        }
156
157
        $field = $request->field;
158
        $model = new $request->model;
159
160
        if ($request->q && is_object($model)) {
161
            return new JsonResponse(
162
                $model::where($request->field, 'like', "%{$request->q}%")
163
                    ->get()
164
                    ->map(function ($item) use ($field) {
165
                        return [
166
                            'tag_name'    => $item->{$field},
167
                            'id'          => $item->id,
168
                            'custom_name' => $item->custom_name,
169
                        ];
170
                    })
171
            );
172
        }
173
    }
174
}
175