Completed
Push — master ( bd7888...7970f7 )
by Adam
06:00
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
// TODO
7
use App\Http\Requests\GuestRequest;
8
use App\Http\Requests\ReservationSearchRequest;
9
use App\Models\Guest;
10
use App\Models\Reservation;
11
use App\Models\Room;
12
use App\Services\GuestTableService;
13
use App\Services\ReservationTableService;
14
use App\Services\RoomTableService;
15
use Illuminate\Database\Eloquent\ModelNotFoundException;
16
use Illuminate\Support\Facades\Log;
17
use Illuminate\Support\Facades\Session;
18
19
class ReservationController extends Controller implements ManageTableInterface
20
{
21
    protected $reservationTableService;
22
23 2
    public function __construct(ReservationTableService $reservationTableService)
24
    {
25 2
        $this->reservationTableService = $reservationTableService;
26 2
    }
27
28 2
    public function index()
29
    {
30 2
        $title = trans('general.reservations');
31
32 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
33 2
            ->with('guest:id,first_name,last_name')
34 2
            ->with('room:id,number')
35 2
            ->paginate($this->getItemsPerPage());
36
37 2
        if ($dataset->isEmpty()) {
38 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
39
        }
40
41
        $viewData = [
42 2
            'columns'       => $this->reservationTableService->getColumns(),
43 2
            'dataset'       => $dataset,
44 2
            'routeName'     => $this->reservationTableService->getRouteName(),
45 2
            'title'         => $title,
46
            // TODO
47 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

47
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
Loading history...
48
        ];
49
50 2
        return view('list', $viewData);
51
    }
52
53
    public function chooseGuest(GuestTableService $guestTableService)
54
    {
55
        $title = trans('navigation.choose_guest');
56
57
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
58
            ->paginate($this->getItemsPerPage());
59
60
        if ($dataset->isEmpty()) {
61
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
62
        }
63
64
        $viewData = [
65
            'columns'         => $guestTableService->getColumns(),
66
            'dataset'         => $dataset,
67
            'routeName'       => $guestTableService->getRouteName(),
68
            'title'           => $title,
69
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
70
        ];
71
72
        return view('list', $viewData);
73
    }
74
75
    public function searchFreeRooms($guestId)
76
    {
77
        try {
78
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
79
        } catch (ModelNotFoundException $e) {
80
            return $this->returnBack([
81
                'message'     => trans('general.object_not_found'),
82
                'alert-class' => 'alert-danger',
83
            ]);
84
        }
85
86
        $dataset = new Reservation();
87
        $dataset->guest()->associate($guest);
88
        $title = trans('navigation.search_free_rooms');
89
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', $dataset->guest->id);
0 ignored issues
show
Bug introduced by
$dataset->guest->id of type integer is incompatible with the type array expected by parameter $parameters of route(). ( Ignorable by Annotation )

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

89
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
90
91
        $viewData = [
92
            'dataset'     => $dataset,
93
            'fields'      => $this->getSearchFields(),
94
            'title'       => $title,
95
            'submitRoute' => $submitRoute,
96
        ];
97
98
        return view('addedit', $viewData);
99
    }
100
101
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
102
    {
103
        try {
104
            $guest = Guest::select('id')->findOrFail($guestId);
105
        } catch (ModelNotFoundException $e) {
106
            return $this->returnBack([
107
                'message'     => trans('general.object_not_found'),
108
                'alert-class' => 'alert-danger',
109
            ]);
110
        }
111
112
        $data = $request->only(['date_start', 'date_end', 'people']);
113
114
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', $guest->id)
0 ignored issues
show
Bug introduced by
$guest->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

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

114
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
115
            ->with($data);
116
    }
117
118
    // TODO
119
    /**
120
     * @param RoomTableService $roomTableService
121
     * @param int              $guestId
122
     *
123
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
124
     */
125
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
126
    {
127
        if (!Session::has(['date_start', 'date_end', 'people'])) {
128
            Log::error('Missing one of Session keys: date_start, date_end, people');
129
            return $this->returnBack([
130
                'message'     => trans('general.object_not_found'),
131
                'alert-class' => 'alert-danger',
132
            ]);
133
        }
134
135
        Session::reflash();
136
137
        try {
138
            $guest = Guest::select('id')->findOrFail($guestId);
0 ignored issues
show
Unused Code introduced by
The assignment to $guest is dead and can be removed.
Loading history...
139
        } catch (ModelNotFoundException $e) {
140
            // TODO: logger helper
141
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '. $e->getMessage());
142
            return $this->returnBack([
143
                'message'     => trans('general.object_not_found'),
144
                'alert-class' => 'alert-danger',
145
            ]);
146
        }
147
148
        $dateStart = Session::get('date_start');
149
        $dateEnd = Session::get('date_end');
150
        $people = Session::get('people');
151
152
        $title = trans('navigation.choose_room');
153
154
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
155
            ->whereNotIn('id', function ($query) use ($dateStart, $dateEnd) {
156
                $query->select('room_id')->from('reservations')
157
                    ->where('date_start', '<', $dateEnd)
158
                    ->where('date_end', '>', $dateStart);
159
            })
160
            ->where('capacity', '>=', $people)
161
            ->orderBy('capacity')
162
            ->paginate($this->getItemsPerPage());
163
164
        if ($dataset->isEmpty()) {
165
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
166
        }
167
168
        $viewData = [
169
            'columns'         => $roomTableService->getColumns(),
170
            'dataset'         => $dataset,
171
            'routeName'       => $roomTableService->getRouteName(),
172
            'title'           => $title,
173
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
174
        ];
175
176
        return view('list', $viewData);
177
    }
178
179
    // TODO
180
    public function store(GuestRequest $request, $objectId = null)
181
    {
182
        if ($objectId === null) {
183
            $object = new Reservation();
184
        } else {
185
            try {
186
                $object = Reservation::findOrFail($objectId);
187
            } catch (ModelNotFoundException $e) {
188
                return $this->returnBack([
189
                    'message'     => trans('general.object_not_found'),
190
                    'alert-class' => 'alert-danger',
191
                ]);
192
            }
193
        }
194
195
        $object->fill($request->all());
196
        $object->save();
197
198
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
199
            ->with([
200
                'message'     => trans('general.saved'),
201
                'alert-class' => 'alert-success',
202
            ]);
203
    }
204
205
    public function delete($objectId)
206
    {
207
        Reservation::destroy($objectId);
208
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
209
210
        return response()->json($data);
211
    }
212
213
    // TODO
214 1
    public function showAddEditForm($objectId = null)
215
    {
216 1
        if ($objectId === null) {
217
            $dataset = new Reservation();
218
            $title = trans('navigation.add_reservation');
219
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
220
        } else {
221
            try {
222 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
223 1
                ->with('guest:id,first_name,last_name')
224 1
                ->with('room:id,number')
225 1
                ->findOrFail($objectId);
226 1
            } catch (ModelNotFoundException $e) {
227 1
                return $this->returnBack([
228 1
                    'message'     => trans('general.object_not_found'),
229 1
                    'alert-class' => 'alert-danger',
230
                ]);
231
            }
232
233
            $title = trans('navigation.edit_reservation');
234
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
235
        }
236
237
        $viewData = [
238
            'dataset'     => $dataset,
239
            'fields'      => $this->getFields(),
240
            'title'       => $title,
241
            'submitRoute' => $submitRoute,
242
            'routeName'   => $this->reservationTableService->getRouteName(),
243
        ];
244
245
        return view('addedit', $viewData);
246
    }
247
248
    // TODO
249
    public function getSearchFields()
250
    {
251
        return [
252
            [
253
                'id'    => 'guest',
254
                'title' => trans('general.guest'),
255
                'value' => function (Reservation $data) {
256
                    return $data->guest->full_name;
257
                },
258
                'optional' => [
259
                    'readonly' => 'readonly',
260
                ],
261
            ],
262
            [
263
                'id'    => 'date_start',
264
                'title' => trans('general.date_start'),
265
                'value' => function (Reservation $data) {
266
                    return $data->date_start;
267
                },
268
                'type'     => 'date',
269
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
270
                    'required' => 'required',
271
                ],*/
272
            ],
273
            [
274
                'id'    => 'date_end',
275
                'title' => trans('general.date_end'),
276
                'value' => function (Reservation $data) {
277
                    return $data->date_end;
278
                },
279
                'type'     => 'date',
280
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
281
                    'required' => 'required',
282
                ],*/
283
            ],
284
            [
285
                'id'    => 'people',
286
                'title' => trans('general.number_of_people'),
287
                'value' => function () {
288
                    // TODO
289
                    return 1;
290
                },
291
                'type'     => 'number',
292
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
293
                    'required' => 'required',
294
                ],*/
295
            ],
296
        ];
297
    }
298
299
    // TODO
300
    public function getFields()
301
    {
302
        return [
303
            [
304
                'id'    => 'first_name',
305
                'title' => trans('general.first_name'),
306
                'value' => function (Reservation $data) {
307
                    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...
308
                },
309
                'optional' => [
310
                    'required' => 'required',
311
                ],
312
            ],
313
            [
314
                'id'    => 'last_name',
315
                'title' => trans('general.last_name'),
316
                'value' => function (Reservation $data) {
317
                    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...
318
                },
319
                'optional' => [
320
                    'required' => 'required',
321
                ],
322
            ],
323
            [
324
                'id'    => 'address',
325
                'title' => trans('general.address'),
326
                'value' => function (Reservation $data) {
327
                    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...
328
                },
329
                'optional' => [
330
                    'required' => 'required',
331
                ],
332
            ],
333
            [
334
                'id'    => 'zip_code',
335
                'title' => trans('general.zip_code'),
336
                'value' => function (Reservation $data) {
337
                    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...
338
                },
339
                'optional' => [
340
                    'required'    => 'required',
341
                    'placeholder' => '00-000',
342
                ],
343
            ],
344
            [
345
                'id'    => 'place',
346
                'title' => trans('general.place'),
347
                'value' => function (Reservation $data) {
348
                    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...
349
                },
350
                'optional' => [
351
                    'required' => 'required',
352
                ],
353
            ],
354
            [
355
                'id'    => 'PESEL',
356
                'title' => trans('general.PESEL'),
357
                'value' => function (Reservation $data) {
358
                    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...
359
                },
360
                'optional' => [
361
                    'required'    => 'required',
362
                    'placeholder' => '12345654321',
363
                ],
364
            ],
365
            [
366
                'id'    => 'contact',
367
                'title' => trans('general.contact'),
368
                'value' => function (Reservation $data) {
369
                    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...
370
                },
371
                'type'     => 'textarea',
372
                'optional' => [
373
                    'placeholder' => trans('general.contact_placeholder'),
374
                ],
375
            ],
376
        ];
377
    }
378
}
379