Passed
Push — master ( 9f8bb2...793984 )
by Adam
03:34
created

ReservationController::getFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 47

Duplication

Lines 78
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 78
loc 78
ccs 0
cts 38
cp 0
rs 8.9019
cc 1
eloc 47
nc 1
nop 0
crap 2

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
// TODO
6
use App\Http\Requests\GuestRequest;
7
use App\Models\Reservation;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Facades\Session;
10
11
class ReservationController extends Controller
12
{
13
    private function getRouteName()
14
    {
15
        return 'reservation';
16
    }
17
18 View Code Duplication
    public function index()
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...
19
    {
20
        $title = trans('general.reservations');
21
22
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
23
            ->with('guest:id,first_name,last_name')
24
            ->with('room:id,number')
25
            ->paginate($this->getItemsPerPage());
26
27
        $viewData = [
28
            'columns'       => $this->getColumns(),
29
            'dataset'       => $dataset,
30
            'routeName'     => $this->getRouteName(),
31
            'title'         => $title,
32
            // TODO
33
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
34
        ];
35
36
        if ($dataset->isEmpty() && !Session::has('message')) {
37
            Session::flash('message', trans('general.no_reservations_in_database'));
38
            Session::flash('alert-class', 'alert-danger');
39
        }
40
41
        return view('list', $viewData);
42
    }
43
44
    // TODO
45 View Code Duplication
    public function store(GuestRequest $request, $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...
46
    {
47
        if ($id === null) {
48
            $object = new Reservation();
49
        } else {
50
            try {
51
                $object = Reservation::findOrFail($id);
52
            } catch (ModelNotFoundException $e) {
53
                return Controller::returnBack([
54
                    'message'     => trans('general.object_not_found'),
55
                    'alert-class' => 'alert-danger',
56
                ]);
57
            }
58
        }
59
60
        $object->fill($request->all());
61
        $object->save();
62
63
        return redirect()->route($this->getRouteName().'.index')
64
            ->with([
65
                'message'     => trans('general.saved'),
66
                'alert-class' => 'alert-success',
67
            ]);
68
    }
69
70 View Code Duplication
    public function delete($id)
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...
71
    {
72
        Reservation::destroy($id);
73
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
74
75
        return response()->json($data);
76
    }
77
78
    // TODO
79 View Code Duplication
    public function showAddEditForm($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...
80
    {
81
        if ($id === null) {
82
            $dataset = new Reservation();
83
            $title = trans('navigation.add_guest');
84
            $submitRoute = route($this->getRouteName().'.postadd');
85
        } else {
86
            try {
87
                $dataset = Reservation::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
88
            } catch (ModelNotFoundException $e) {
89
                return Controller::returnBack([
90
                    'message'     => trans('general.object_not_found'),
91
                    'alert-class' => 'alert-danger',
92
                ]);
93
            }
94
95
            $title = trans('navigation.edit_guest');
96
            $submitRoute = route($this->getRouteName().'.postedit', $id);
97
        }
98
99
        $viewData = [
100
            'dataset'     => $dataset,
101
            'fields'      => $this->getFields(),
102
            'title'       => $title,
103
            'submitRoute' => $submitRoute,
104
            'routeName'   => $this->getRouteName(),
105
        ];
106
107
        return view('addedit', $viewData);
108
    }
109
110
    // TODO
111 View Code Duplication
    private function getFields()
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...
112
    {
113
        return [
114
            [
115
                'id'    => 'first_name',
116
                'title' => trans('general.first_name'),
117
                'value' => function (Reservation $data) {
118
                    return $data->first_name;
119
                },
120
                'optional' => [
121
                    'required' => 'required',
122
                ],
123
            ],
124
            [
125
                'id'    => 'last_name',
126
                'title' => trans('general.last_name'),
127
                'value' => function (Reservation $data) {
128
                    return $data->last_name;
129
                },
130
                'optional' => [
131
                    'required' => 'required',
132
                ],
133
            ],
134
            [
135
                'id'    => 'address',
136
                'title' => trans('general.address'),
137
                'value' => function (Reservation $data) {
138
                    return $data->address;
139
                },
140
                'optional' => [
141
                    'required' => 'required',
142
                ],
143
            ],
144
            [
145
                'id'    => 'zip_code',
146
                'title' => trans('general.zip_code'),
147
                'value' => function (Reservation $data) {
148
                    return $data->zip_code;
149
                },
150
                'optional' => [
151
                    'required'    => 'required',
152
                    'placeholder' => '00-000',
153
                ],
154
            ],
155
            [
156
                'id'    => 'place',
157
                'title' => trans('general.place'),
158
                'value' => function (Reservation $data) {
159
                    return $data->place;
160
                },
161
                'optional' => [
162
                    'required' => 'required',
163
                ],
164
            ],
165
            [
166
                'id'    => 'PESEL',
167
                'title' => trans('general.PESEL'),
168
                'value' => function (Reservation $data) {
169
                    return $data->PESEL;
170
                },
171
                'optional' => [
172
                    'required'    => 'required',
173
                    'placeholder' => '12345654321',
174
                ],
175
            ],
176
            [
177
                'id'    => 'contact',
178
                'title' => trans('general.contact'),
179
                'value' => function (Reservation $data) {
180
                    return $data->contact;
181
                },
182
                'type'     => 'textarea',
183
                'optional' => [
184
                    'placeholder' => trans('general.contact_placeholder'),
185
                ],
186
            ],
187
        ];
188
    }
189
190
    // TODO
191
    private function getColumns()
192
    {
193
        $dataset = [
194
            [
195
                'title' => trans('general.room'),
196
                'value' => function (Reservation $data) {
197
                    return $data->room->number;
198
                },
199
            ],
200
            [
201
                'title' => trans('general.guest'),
202
                'value' => function (Reservation $data) {
203
                    return $data->guest->first_name.' '.$data->guest->last_name;
204
                },
205
            ],
206
            [
207
                'title' => trans('general.date_start'),
208
                'value' => function (Reservation $data) {
209
                    return $data->date_start;
210
                },
211
            ],
212
            [
213
                'title' => trans('general.date_end'),
214
                'value' => function (Reservation $data) {
215
                    return $data->date_end;
216
                },
217
            ],
218
            [
219
                'title' => trans('general.people'),
220
                'value' => function (Reservation $data) {
221
                    return $data->people;
222
                },
223
            ],
224
        ];
225
226
        return $dataset;
227
    }
228
}
229