Passed
Push — master ( 22d94f...bc363e )
by Adam
05:37
created

ReservationController::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 0
dl 0
loc 74
ccs 0
cts 38
cp 0
crap 2
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
// TODO
6
use App\Http\Requests\GuestRequest;
7
use App\Models\Reservation;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
10
class ReservationController extends Controller
11
{
12 2
    private function getRouteName()
13
    {
14 2
        return 'reservation';
15
    }
16
17 2
    public function index()
18
    {
19 2
        $title = trans('general.reservations');
20
21 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
22 2
            ->with('guest:id,first_name,last_name')
23 2
            ->with('room:id,number')
24 2
            ->paginate($this->getItemsPerPage());
25
26 2
        if ($dataset->isEmpty()) {
27 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
28
        }
29
30
        $viewData = [
31 2
            'columns'       => $this->getColumns(),
32 2
            'dataset'       => $dataset,
33 2
            'routeName'     => $this->getRouteName(),
34 2
            'title'         => $title,
35
            // TODO
36 2
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.reservation') 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

36
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
Loading history...
37
        ];
38
39 2
        return view('list', $viewData);
40
    }
41
42
    // TODO
43
    public function store(GuestRequest $request, $id = null)
44
    {
45
        if ($id === null) {
46
            $object = new Reservation();
47
        } else {
48
            try {
49
                $object = Reservation::findOrFail($id);
50
            } catch (ModelNotFoundException $e) {
51
                return $this->returnBack([
52
                    'message'     => trans('general.object_not_found'),
53
                    'alert-class' => 'alert-danger',
54
                ]);
55
            }
56
        }
57
58
        $object->fill($request->all());
59
        $object->save();
60
61
        return redirect()->route($this->getRouteName().'.index')
62
            ->with([
63
                'message'     => trans('general.saved'),
64
                'alert-class' => 'alert-success',
65
            ]);
66
    }
67
68
    public function delete($id)
69
    {
70
        Reservation::destroy($id);
71
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
72
73
        return response()->json($data);
74
    }
75
76
    // TODO
77 1
    public function showAddEditForm($id = null)
78
    {
79 1
        if ($id === null) {
80
            $dataset = new Reservation();
81
            $title = trans('navigation.add_reservation');
82
            $submitRoute = route($this->getRouteName().'.postadd');
83
        } else {
84
            try {
85 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
86 1
                ->with('guest:id,first_name,last_name')
87 1
                ->with('room:id,number')
88 1
                ->findOrFail($id);
89 1
            } catch (ModelNotFoundException $e) {
90 1
                return $this->returnBack([
91 1
                    'message'     => trans('general.object_not_found'),
92 1
                    'alert-class' => 'alert-danger',
93
                ]);
94
            }
95
96
            $title = trans('navigation.edit_reservation');
97
            $submitRoute = route($this->getRouteName().'.postedit', $id);
98
        }
99
100
        $viewData = [
101
            'dataset'     => $dataset,
102
            'fields'      => $this->getFields(),
103
            'title'       => $title,
104
            'submitRoute' => $submitRoute,
105
            'routeName'   => $this->getRouteName(),
106
        ];
107
108
        return view('addedit', $viewData);
109
    }
110
111
    // TODO
112
    private function getFields()
113
    {
114
        return [
115
            [
116
                'id'    => 'first_name',
117
                'title' => trans('general.first_name'),
118
                'value' => function (Reservation $data) {
119
                    return $data->first_name;
0 ignored issues
show
Bug introduced by
The property first_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
120
                },
121
                'optional' => [
122
                    'required' => 'required',
123
                ],
124
            ],
125
            [
126
                'id'    => 'last_name',
127
                'title' => trans('general.last_name'),
128
                'value' => function (Reservation $data) {
129
                    return $data->last_name;
0 ignored issues
show
Bug introduced by
The property last_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
130
                },
131
                'optional' => [
132
                    'required' => 'required',
133
                ],
134
            ],
135
            [
136
                'id'    => 'address',
137
                'title' => trans('general.address'),
138
                'value' => function (Reservation $data) {
139
                    return $data->address;
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
140
                },
141
                'optional' => [
142
                    'required' => 'required',
143
                ],
144
            ],
145
            [
146
                'id'    => 'zip_code',
147
                'title' => trans('general.zip_code'),
148
                'value' => function (Reservation $data) {
149
                    return $data->zip_code;
0 ignored issues
show
Bug introduced by
The property zip_code does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
150
                },
151
                'optional' => [
152
                    'required'    => 'required',
153
                    'placeholder' => '00-000',
154
                ],
155
            ],
156
            [
157
                'id'    => 'place',
158
                'title' => trans('general.place'),
159
                'value' => function (Reservation $data) {
160
                    return $data->place;
0 ignored issues
show
Bug introduced by
The property place does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
161
                },
162
                'optional' => [
163
                    'required' => 'required',
164
                ],
165
            ],
166
            [
167
                'id'    => 'PESEL',
168
                'title' => trans('general.PESEL'),
169
                'value' => function (Reservation $data) {
170
                    return $data->PESEL;
0 ignored issues
show
Bug introduced by
The property PESEL does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
171
                },
172
                'optional' => [
173
                    'required'    => 'required',
174
                    'placeholder' => '12345654321',
175
                ],
176
            ],
177
            [
178
                'id'    => 'contact',
179
                'title' => trans('general.contact'),
180
                'value' => function (Reservation $data) {
181
                    return $data->contact;
0 ignored issues
show
Bug introduced by
The property contact does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
182
                },
183
                'type'     => 'textarea',
184
                'optional' => [
185
                    'placeholder' => trans('general.contact_placeholder'),
186
                ],
187
            ],
188
        ];
189
    }
190
191
    // TODO
192 2
    private function getColumns()
193
    {
194
        $dataset = [
195
            [
196 2
                'title' => trans('general.room'),
197 2
                'value' => function (Reservation $data) {
198
                    return $data->room->number;
199 2
                },
200
            ],
201
            [
202 2
                'title' => trans('general.guest'),
203 2
                'value' => function (Reservation $data) {
204
                    return $data->guest->first_name.' '.$data->guest->last_name;
205 2
                },
206
            ],
207
            [
208 2
                'title' => trans('general.date_start'),
209 2
                'value' => function (Reservation $data) {
210
                    return $data->date_start;
211 2
                },
212
            ],
213
            [
214 2
                'title' => trans('general.date_end'),
215 2
                'value' => function (Reservation $data) {
216
                    return $data->date_end;
217 2
                },
218
            ],
219
            [
220 2
                'title' => trans('general.people'),
221 2
                'value' => function (Reservation $data) {
222
                    return $data->people;
223 2
                },
224
            ],
225
        ];
226
227 2
        return $dataset;
228
    }
229
}
230