Passed
Branch master (065bfc)
by Adam
05:23
created

GuestController::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 0
dl 0
loc 74
ccs 38
cts 38
cp 1
crap 1
rs 9.0335
c 0
b 0
f 0

How to fix   Long Method   

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 App\Http\Controllers;
4
5
use App\Http\Requests\GuestRequest;
6
use App\Models\Guest;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
9
class GuestController extends Controller
10
{
11 5
    private function getRouteName()
12
    {
13 5
        return 'guest';
14
    }
15
16 4
    public function index()
17
    {
18 4
        $title = trans('general.guests');
19
20 4
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
21 4
            ->paginate($this->getItemsPerPage());
22
23 4
        if ($dataset->isEmpty()) {
24 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
25
        }
26
27
        $viewData = [
28 4
            'columns'       => $this->getColumns(),
29 4
            'dataset'       => $dataset,
30 4
            'routeName'     => $this->getRouteName(),
31 4
            'title'         => $title,
32
            // TODO
33 4
            'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.guest') can also be of type array; however, parameter $str of mb_strtolower() does only seem to accept string, 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

33
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.guest')).' '.mb_strtolower(trans('general.number')),
Loading history...
34
        ];
35
36 4
        return view('list', $viewData);
37
    }
38
39 1
    public function store(GuestRequest $request, $id = null)
40
    {
41 1
        if ($id === null) {
42
            $object = new Guest();
43
        } else {
44
            try {
45 1
                $object = Guest::findOrFail($id);
46
            } catch (ModelNotFoundException $e) {
47
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

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

47
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
48
                    'message'     => trans('general.object_not_found'),
49
                    'alert-class' => 'alert-danger',
50
                ]);
51
            }
52
        }
53
54 1
        $object->fill($request->all());
55 1
        $object->save();
56
57 1
        return redirect()->route($this->getRouteName().'.index')
58 1
            ->with([
59 1
                'message'     => trans('general.saved'),
60 1
                'alert-class' => 'alert-success',
61
            ]);
62
    }
63
64 1
    public function delete($id)
65
    {
66 1
        Guest::destroy($id);
67 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
68
69 1
        return response()->json($data);
70
    }
71
72
    // TODO
73 3
    public function showAddEditForm($id = null)
74
    {
75 3
        if ($id === null) {
76 1
            $dataset = new Guest();
77 1
            $title = trans('navigation.add_guest');
78 1
            $submitRoute = route($this->getRouteName().'.postadd');
79
        } else {
80
            try {
81 2
                $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
82 1
            } catch (ModelNotFoundException $e) {
83 1
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

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

83
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
84 1
                    'message'     => trans('general.object_not_found'),
85 1
                    'alert-class' => 'alert-danger',
86
                ]);
87
            }
88
89 1
            $title = trans('navigation.edit_guest');
90 1
            $submitRoute = route($this->getRouteName().'.postedit', $id);
91
        }
92
93
        $viewData = [
94 2
            'dataset'     => $dataset,
95 2
            'fields'      => $this->getFields(),
96 2
            'title'       => $title,
97 2
            'submitRoute' => $submitRoute,
98 2
            'routeName'   => $this->getRouteName(),
99
        ];
100
101 2
        return view('addedit', $viewData);
102
    }
103
104
    // TODO
105 2
    private function getFields()
106
    {
107
        return [
108
            [
109 2
                'id'    => 'first_name',
110 2
                'title' => trans('general.first_name'),
111 2
                'value' => function (Guest $data) {
112 2
                    return $data->first_name;
113 2
                },
114
                'optional' => [
115
                    'required' => 'required',
116
                ],
117
            ],
118
            [
119 2
                'id'    => 'last_name',
120 2
                'title' => trans('general.last_name'),
121 2
                'value' => function (Guest $data) {
122 2
                    return $data->last_name;
123 2
                },
124
                'optional' => [
125
                    'required' => 'required',
126
                ],
127
            ],
128
            [
129 2
                'id'    => 'address',
130 2
                'title' => trans('general.address'),
131 2
                'value' => function (Guest $data) {
132 2
                    return $data->address;
133 2
                },
134
                'optional' => [
135
                    'required' => 'required',
136
                ],
137
            ],
138
            [
139 2
                'id'    => 'zip_code',
140 2
                'title' => trans('general.zip_code'),
141 2
                'value' => function (Guest $data) {
142 2
                    return $data->zip_code;
143 2
                },
144
                'optional' => [
145
                    'required'    => 'required',
146
                    'placeholder' => '00-000',
147
                ],
148
            ],
149
            [
150 2
                'id'    => 'place',
151 2
                'title' => trans('general.place'),
152 2
                'value' => function (Guest $data) {
153 2
                    return $data->place;
154 2
                },
155
                'optional' => [
156
                    'required' => 'required',
157
                ],
158
            ],
159
            [
160 2
                'id'    => 'PESEL',
161 2
                'title' => trans('general.PESEL'),
162 2
                'value' => function (Guest $data) {
163 2
                    return $data->PESEL;
164 2
                },
165
                'optional' => [
166
                    'required'    => 'required',
167
                    'placeholder' => '12345654321',
168
                ],
169
            ],
170
            [
171 2
                'id'    => 'contact',
172 2
                'title' => trans('general.contact'),
173 2
                'value' => function (Guest $data) {
174 2
                    return $data->contact;
175 2
                },
176 2
                'type'     => 'textarea',
177
                'optional' => [
178 2
                    'placeholder' => trans('general.contact_placeholder'),
179
                ],
180
            ],
181
        ];
182
    }
183
184 4
    private function getColumns()
185
    {
186
        $dataset = [
187
            [
188 4
                'title' => trans('general.first_name'),
189 4
                'value' => function (Guest $data) {
190 2
                    return $data->first_name;
191 4
                },
192
            ],
193
            [
194 4
                'title' => trans('general.last_name'),
195 4
                'value' => function (Guest $data) {
196 2
                    return $data->last_name;
197 4
                },
198
            ],
199
            [
200 4
                'title' => trans('general.address'),
201 4
                'value' => function (Guest $data) {
202 2
                    return $data->address.', '.$data->zip_code.' '.$data->place;
203 4
                },
204
            ],
205
            [
206 4
                'title' => trans('general.PESEL'),
207 4
                'value' => function (Guest $data) {
208 2
                    return $data->PESEL;
209 4
                },
210
            ],
211
            [
212 4
                'title' => trans('general.contact'),
213 4
                'value' => function (Guest $data) {
214 2
                    return $data->contact;
215 4
                },
216
            ],
217
        ];
218
219 4
        return $dataset;
220
    }
221
}
222