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