Completed
Push — master ( 783ca1...ecb9b7 )
by Adam
07:15
created

ReservationController   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 466
Duplicated Lines 0 %

Test Coverage

Coverage 50.61%

Importance

Changes 0
Metric Value
dl 0
loc 466
ccs 125
cts 247
cp 0.5061
rs 9.3999
c 0
b 0
f 0
wmc 33

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A store() 0 22 3
A delete() 0 15 2
A getFields() 0 74 1
A future() 0 23 2
B showAddEditForm() 0 32 3
A current() 0 23 2
A index() 0 22 2
A chooseGuest() 0 20 2
B chooseFreeRoom() 0 48 4
A getSearchFields() 0 49 2
B searchFreeRooms() 0 24 2
B add() 0 37 3
A postSearchFreeRooms() 0 15 2
A isReservationDataInSessionCorrect() 0 11 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Interfaces\ManageTableInterface;
6
use App\Http\Requests\GuestRequest;
7
use App\Http\Requests\ReservationSearchRequest;
8
use App\Models\Guest;
9
use App\Models\Reservation;
10
use App\Models\Room;
11
use App\Services\GuestTableService;
12
use App\Services\ReservationTableService;
13
use App\Services\RoomTableService;
14
use Carbon\Carbon;
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 9
    public function __construct(ReservationTableService $reservationTableService)
24
    {
25 9
        $this->reservationTableService = $reservationTableService;
26 9
    }
27
28 4
    public function index()
29
    {
30 4
        $title = trans('navigation.all_reservations');
31
32 4
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
33 4
            ->with('guest:id,first_name,last_name')
34 4
            ->with('room:id,number')
35 4
            ->orderBy('date_end', 'DESC')
36 4
            ->paginate($this->getItemsPerPage());
37
38 4
        if ($dataset->isEmpty()) {
39 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
40
        }
41
42
        $viewData = [
43 4
            'columns'       => $this->reservationTableService->getColumns(),
44 4
            'dataset'       => $dataset,
45 4
            'routeName'     => $this->reservationTableService->getRouteName(),
46 4
            'title'         => $title,
47
        ];
48
49 4
        return view('list', $viewData);
50
    }
51
52
    public function current()
53
    {
54
        $title = trans('navigation.current_reservations');
55
56
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
57
            ->with('guest:id,first_name,last_name')
58
            ->with('room:id,number')
59
            ->getCurrentReservations()
60
            ->orderBy('date_end')
61
            ->paginate($this->getItemsPerPage());
62
63
        if ($dataset->isEmpty()) {
64
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
65
        }
66
67
        $viewData = [
68
            'columns'       => $this->reservationTableService->getColumns(),
69
            'dataset'       => $dataset,
70
            'routeName'     => $this->reservationTableService->getRouteName(),
71
            'title'         => $title,
72
        ];
73
74
        return view('list', $viewData);
75
    }
76
77
    public function future()
78
    {
79
        $title = trans('navigation.future_reservations');
80
81
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
82
            ->with('guest:id,first_name,last_name')
83
            ->with('room:id,number')
84
            ->getFutureReservations()
85
            ->orderBy('date_end')
86
            ->paginate($this->getItemsPerPage());
87
88
        if ($dataset->isEmpty()) {
89
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
90
        }
91
92
        $viewData = [
93
            'columns'       => $this->reservationTableService->getColumns(),
94
            'dataset'       => $dataset,
95
            'routeName'     => $this->reservationTableService->getRouteName(),
96
            'title'         => $title,
97
        ];
98
99
        return view('list', $viewData);
100
    }
101
102 3
    public function chooseGuest(GuestTableService $guestTableService)
103
    {
104 3
        $title = trans('navigation.choose_guest');
105
106 3
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
107 3
            ->paginate($this->getItemsPerPage());
108
109 3
        if ($dataset->isEmpty()) {
110 1
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
111
        }
112
113
        $viewData = [
114 3
            'columns'         => $guestTableService->getColumns(),
115 3
            'dataset'         => $dataset,
116 3
            'routeName'       => $guestTableService->getRouteName(),
117 3
            'title'           => $title,
118 3
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
119
        ];
120
121 3
        return view('list', $viewData);
122
    }
123
124 3
    public function searchFreeRooms($guestId)
125
    {
126
        try {
127 3
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
128
        } catch (ModelNotFoundException $e) {
129
            return $this->returnBack([
130
                'message'     => trans('general.object_not_found'),
131
                'alert-class' => 'alert-danger',
132
            ]);
133
        }
134
135 3
        $dataset = new Reservation();
136 3
        $dataset->guest()->associate($guest);
137 3
        $title = trans('navigation.search_free_rooms');
138 3
        $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

138
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
139
140
        $viewData = [
141 3
            'dataset'     => $dataset,
142 3
            'fields'      => $this->getSearchFields(),
143 3
            'title'       => $title,
144 3
            'submitRoute' => $submitRoute,
145
        ];
146
147 3
        return view('addedit', $viewData);
148
    }
149
150 2
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
151
    {
152
        try {
153 2
            $guest = Guest::select('id')->findOrFail($guestId);
154
        } catch (ModelNotFoundException $e) {
155
            return $this->returnBack([
156
                'message'     => trans('general.object_not_found'),
157
                'alert-class' => 'alert-danger',
158
            ]);
159
        }
160
161 2
        $data = $request->only(['date_start', 'date_end', 'people']);
162
163 2
        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

163
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
164 2
            ->with($data);
165
    }
166
167
    /**
168
     * @param RoomTableService $roomTableService
169
     * @param int              $guestId
170
     *
171
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
172
     */
173 2
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
174
    {
175 2
        if (!$this->isReservationDataInSessionCorrect()) {
176
            return $this->returnBack([
177
                'message'     => trans('general.object_not_found'),
178
                'alert-class' => 'alert-danger',
179
            ]);
180
        }
181
182
        try {
183 2
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
184
        } catch (ModelNotFoundException $e) {
185
            // TODO: logger helper
186
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
187
188
            return $this->returnBack([
189
                'message'     => trans('general.object_not_found'),
190
                'alert-class' => 'alert-danger',
191
            ]);
192
        }
193
194 2
        $dateStart = Session::get('date_start');
195 2
        $dateEnd = Session::get('date_end');
196 2
        $people = Session::get('people');
197
198 2
        $dateStart = Carbon::parse($dateStart);
199 2
        $dateEnd = Carbon::parse($dateEnd);
200
201 2
        $title = trans('navigation.choose_room_for').' '.$guest->fullName;
0 ignored issues
show
Bug introduced by
Are you sure trans('navigation.choose_room_for') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

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

201
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
202
203 2
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
204 2
            ->freeRoomsForReservation($dateStart, $dateEnd, $people)
205 2
            ->paginate($this->getItemsPerPage());
206
207 2
        if ($dataset->isEmpty()) {
208 1
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
209
        }
210
211
        $viewData = [
212 2
            'columns'                => $roomTableService->getColumns(),
213 2
            'dataset'                => $dataset,
214 2
            'routeName'              => $roomTableService->getRouteName(),
215 2
            'title'                  => $title,
216 2
            'routeChooseName'        => $this->reservationTableService->getRouteName().'.add',
217 2
            'secondRouteChooseParam' => $guest->id,
218
        ];
219
220 2
        return view('list', $viewData);
221
    }
222
223 1
    public function add($roomId, $guestId)
224
    {
225 1
        if (!$this->isReservationDataInSessionCorrect()) {
226
            return $this->returnBack([
227
                'message'     => trans('general.object_not_found'),
228
                'alert-class' => 'alert-danger',
229
            ]);
230
        }
231
232 1
        $dateStart = Session::get('date_start');
233 1
        $dateEnd = Session::get('date_end');
234 1
        $people = Session::get('people');
235
236
        try {
237 1
            $guest = Guest::select('id')->findOrFail($guestId);
238 1
            $room = Room::select('id')->findOrFail($roomId);
239
        } catch (ModelNotFoundException $e) {
240
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
241
242
            return $this->returnBack([
243
                'message'     => trans('general.object_not_found'),
244
                'alert-class' => 'alert-danger',
245
            ]);
246
        }
247
248 1
        $reservation = new Reservation();
249 1
        $reservation->guest_id = $guest->id;
250 1
        $reservation->room_id = $room->id;
251 1
        $reservation->date_start = $dateStart;
252 1
        $reservation->date_end = $dateEnd;
253 1
        $reservation->people = $people;
254
255 1
        $reservation->save();
256
257 1
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
258
259 1
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
260
    }
261
262
    // TODO
263
    public function store(GuestRequest $request, $objectId = null)
264
    {
265
        if ($objectId === null) {
266
            $object = new Reservation();
267
        } else {
268
            try {
269
                $object = Reservation::findOrFail($objectId);
270
            } catch (ModelNotFoundException $e) {
271
                return $this->returnBack([
272
                    'message'     => trans('general.object_not_found'),
273
                    'alert-class' => 'alert-danger',
274
                ]);
275
            }
276
        }
277
278
        $object->fill($request->all());
279
        $object->save();
280
281
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
282
            ->with([
283
                'message'     => trans('general.saved'),
284
                'alert-class' => 'alert-success',
285
            ]);
286
    }
287
288 1
    public function delete($objectId)
289
    {
290
        try {
291 1
            $object = Reservation::findOrFail($objectId);
292
        } catch (ModelNotFoundException $e) {
293
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
294
295
            return response()->json($data);
296
        }
297
298 1
        $object->delete();
299
300 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
301
302 1
        return response()->json($data);
303
    }
304
305
    // TODO
306 1
    public function showAddEditForm($objectId = null)
307
    {
308 1
        if ($objectId === null) {
309
            $dataset = new Reservation();
310
            $title = trans('navigation.add_reservation');
311
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
312
        } else {
313
            try {
314 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
315 1
                ->with('guest:id,first_name,last_name')
316 1
                ->with('room:id,number')
317 1
                ->findOrFail($objectId);
318 1
            } catch (ModelNotFoundException $e) {
319 1
                return $this->returnBack([
320 1
                    'message'     => trans('general.object_not_found'),
321 1
                    'alert-class' => 'alert-danger',
322
                ]);
323
            }
324
325
            $title = trans('navigation.edit_reservation');
326
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
327
        }
328
329
        $viewData = [
330
            'dataset'     => $dataset,
331
            'fields'      => $this->getFields(),
332
            'title'       => $title,
333
            'submitRoute' => $submitRoute,
334
            'routeName'   => $this->reservationTableService->getRouteName(),
335
        ];
336
337
        return view('addedit', $viewData);
338
    }
339
340 3
    public function getSearchFields()
341
    {
342
        return [
343
            [
344 3
                'id'    => 'guest',
345 3
                'title' => trans('general.guest'),
346 3
                'value' => function (Reservation $data) {
347 3
                    return $data->guest->full_name;
348 3
                },
349
                'optional' => [
350
                    'readonly' => 'readonly',
351
                ],
352
            ],
353
            [
354 3
                'id'    => 'date_start',
355 3
                'title' => trans('general.date_start'),
356 3
                'value' => function (Reservation $data) {
357 3
                    return $data->date_start;
358 3
                },
359 3
                'type'     => 'text',
360
                'optional' => [
361
                    'required'    => 'required',
362
                    'class'       => 'datepicker start-date',
363
                    'placeholder' => 'dd.mm.rrrr',
364
                ],
365
            ],
366
            [
367 3
                'id'    => 'date_end',
368 3
                'title' => trans('general.date_end'),
369 3
                'value' => function (Reservation $data) {
370 3
                    return $data->date_end;
371 3
                },
372 3
                'type'     => 'text',
373
                'optional' => [
374
                    'required'    => 'required',
375
                    'class'       => 'datepicker end-date',
376
                    'placeholder' => 'dd.mm.rrrr',
377
                ],
378
            ],
379
            [
380 3
                'id'    => 'people',
381 3
                'title' => trans('general.number_of_people'),
382 3
                'value' => function (Reservation $data) {
383 3
                    return $data->people ?: 1;
384 3
                },
385 3
                'type'     => 'number',
386
                'optional' => [
387
                    'required' => 'required',
388
                    'min'      => '1',
389
                ],
390
            ],
391
        ];
392
    }
393
394
    // TODO
395
    public function getFields()
396
    {
397
        return [
398
            [
399
                'id'    => 'first_name',
400
                'title' => trans('general.first_name'),
401
                'value' => function (Reservation $data) {
402
                    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...
403
                },
404
                'optional' => [
405
                    'required' => 'required',
406
                ],
407
            ],
408
            [
409
                'id'    => 'last_name',
410
                'title' => trans('general.last_name'),
411
                'value' => function (Reservation $data) {
412
                    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...
413
                },
414
                'optional' => [
415
                    'required' => 'required',
416
                ],
417
            ],
418
            [
419
                'id'    => 'address',
420
                'title' => trans('general.address'),
421
                'value' => function (Reservation $data) {
422
                    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...
423
                },
424
                'optional' => [
425
                    'required' => 'required',
426
                ],
427
            ],
428
            [
429
                'id'    => 'zip_code',
430
                'title' => trans('general.zip_code'),
431
                'value' => function (Reservation $data) {
432
                    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...
433
                },
434
                'optional' => [
435
                    'required'    => 'required',
436
                    'placeholder' => '00-000',
437
                ],
438
            ],
439
            [
440
                'id'    => 'place',
441
                'title' => trans('general.place'),
442
                'value' => function (Reservation $data) {
443
                    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...
444
                },
445
                'optional' => [
446
                    'required' => 'required',
447
                ],
448
            ],
449
            [
450
                'id'    => 'PESEL',
451
                'title' => trans('general.PESEL'),
452
                'value' => function (Reservation $data) {
453
                    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...
454
                },
455
                'optional' => [
456
                    'required'    => 'required',
457
                    'placeholder' => '12345654321',
458
                ],
459
            ],
460
            [
461
                'id'    => 'contact',
462
                'title' => trans('general.contact'),
463
                'value' => function (Reservation $data) {
464
                    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...
465
                },
466
                'type'     => 'textarea',
467
                'optional' => [
468
                    'placeholder' => trans('general.contact_placeholder'),
469
                ],
470
            ],
471
        ];
472
    }
473
474 2
    private function isReservationDataInSessionCorrect()
475
    {
476 2
        if (!Session::has(['date_start', 'date_end', 'people'])) {
477
            Log::error('Missing one of Session keys: date_start, date_end, people');
478
479
            return false;
480
        }
481
482 2
        Session::reflash();
483
484 2
        return true;
485
    }
486
}
487