Passed
Branch master (cdbb92)
by Adam
08:04
created

ReservationController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 415
Duplicated Lines 0 %

Test Coverage

Coverage 14.35%

Importance

Changes 0
Metric Value
dl 0
loc 415
ccs 31
cts 216
cp 0.1435
rs 10
c 0
b 0
f 0
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
B searchFreeRooms() 0 24 2
A chooseGuest() 0 20 2
A __construct() 0 3 1
A getSearchFields() 0 49 1
A getFields() 0 74 1
B showAddEditForm() 0 32 3
A isReservationDataInSessionCorrect() 0 11 2
B chooseFreeRoom() 0 48 4
A store() 0 22 3
A delete() 0 15 2
B add() 0 37 3
A postSearchFreeRooms() 0 15 2
A index() 0 21 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 4
    public function __construct(ReservationTableService $reservationTableService)
24
    {
25 4
        $this->reservationTableService = $reservationTableService;
26 4
    }
27
28 3
    public function index()
29
    {
30 3
        $title = trans('navigation.all_reservations');
31
32 3
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
33 3
            ->with('guest:id,first_name,last_name')
34 3
            ->with('room:id,number')
35 3
            ->paginate($this->getItemsPerPage());
36
37 3
        if ($dataset->isEmpty()) {
38 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
39
        }
40
41
        $viewData = [
42 3
            'columns'       => $this->reservationTableService->getColumns(),
43 3
            'dataset'       => $dataset,
44 3
            'routeName'     => $this->reservationTableService->getRouteName(),
45 3
            'title'         => $title,
46
        ];
47
48 3
        return view('list', $viewData);
49
    }
50
51
    public function chooseGuest(GuestTableService $guestTableService)
52
    {
53
        $title = trans('navigation.choose_guest');
54
55
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
56
            ->paginate($this->getItemsPerPage());
57
58
        if ($dataset->isEmpty()) {
59
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
60
        }
61
62
        $viewData = [
63
            'columns'         => $guestTableService->getColumns(),
64
            'dataset'         => $dataset,
65
            'routeName'       => $guestTableService->getRouteName(),
66
            'title'           => $title,
67
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
68
        ];
69
70
        return view('list', $viewData);
71
    }
72
73
    public function searchFreeRooms($guestId)
74
    {
75
        try {
76
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
77
        } catch (ModelNotFoundException $e) {
78
            return $this->returnBack([
79
                'message'     => trans('general.object_not_found'),
80
                'alert-class' => 'alert-danger',
81
            ]);
82
        }
83
84
        $dataset = new Reservation();
85
        $dataset->guest()->associate($guest);
86
        $title = trans('navigation.search_free_rooms');
87
        $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

87
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
88
89
        $viewData = [
90
            'dataset'     => $dataset,
91
            'fields'      => $this->getSearchFields(),
92
            'title'       => $title,
93
            'submitRoute' => $submitRoute,
94
        ];
95
96
        return view('addedit', $viewData);
97
    }
98
99
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
100
    {
101
        try {
102
            $guest = Guest::select('id')->findOrFail($guestId);
103
        } catch (ModelNotFoundException $e) {
104
            return $this->returnBack([
105
                'message'     => trans('general.object_not_found'),
106
                'alert-class' => 'alert-danger',
107
            ]);
108
        }
109
110
        $data = $request->only(['date_start', 'date_end', 'people']);
111
112
        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

112
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
113
            ->with($data);
114
    }
115
116
    /**
117
     * @param RoomTableService $roomTableService
118
     * @param int              $guestId
119
     *
120
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
121
     */
122
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
123
    {
124
        if (!$this->isReservationDataInSessionCorrect()) {
125
            return $this->returnBack([
126
                'message'     => trans('general.object_not_found'),
127
                'alert-class' => 'alert-danger',
128
            ]);
129
        }
130
131
        try {
132
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
133
        } catch (ModelNotFoundException $e) {
134
            // TODO: logger helper
135
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
136
137
            return $this->returnBack([
138
                'message'     => trans('general.object_not_found'),
139
                'alert-class' => 'alert-danger',
140
            ]);
141
        }
142
143
        $dateStart = Session::get('date_start');
144
        $dateEnd = Session::get('date_end');
145
        $people = Session::get('people');
146
147
        $dateStart = Carbon::parse($dateStart);
148
        $dateEnd = Carbon::parse($dateEnd);
149
150
        $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

150
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
151
152
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
153
            ->freeRoomsForReservation($dateStart, $dateEnd, $people)
154
            ->paginate($this->getItemsPerPage());
155
156
        if ($dataset->isEmpty()) {
157
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
158
        }
159
160
        $viewData = [
161
            'columns'                => $roomTableService->getColumns(),
162
            'dataset'                => $dataset,
163
            'routeName'              => $roomTableService->getRouteName(),
164
            'title'                  => $title,
165
            'routeChooseName'        => $this->reservationTableService->getRouteName().'.add',
166
            'secondRouteChooseParam' => $guest->id,
167
        ];
168
169
        return view('list', $viewData);
170
    }
171
172
    public function add($roomId, $guestId)
173
    {
174
        if (!$this->isReservationDataInSessionCorrect()) {
175
            return $this->returnBack([
176
                'message'     => trans('general.object_not_found'),
177
                'alert-class' => 'alert-danger',
178
            ]);
179
        }
180
181
        $dateStart = Session::get('date_start');
182
        $dateEnd = Session::get('date_end');
183
        $people = Session::get('people');
184
185
        try {
186
            $guest = Guest::select('id')->findOrFail($guestId);
187
            $room = Room::select('id')->findOrFail($roomId);
188
        } catch (ModelNotFoundException $e) {
189
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
190
191
            return $this->returnBack([
192
                'message'     => trans('general.object_not_found'),
193
                'alert-class' => 'alert-danger',
194
            ]);
195
        }
196
197
        $reservation = new Reservation();
198
        $reservation->guest_id = $guest->id;
199
        $reservation->room_id = $room->id;
200
        $reservation->date_start = $dateStart;
201
        $reservation->date_end = $dateEnd;
202
        $reservation->people = $people;
203
204
        $reservation->save();
205
206
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
207
208
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
209
    }
210
211
    // TODO
212
    public function store(GuestRequest $request, $objectId = null)
213
    {
214
        if ($objectId === null) {
215
            $object = new Reservation();
216
        } else {
217
            try {
218
                $object = Reservation::findOrFail($objectId);
219
            } catch (ModelNotFoundException $e) {
220
                return $this->returnBack([
221
                    'message'     => trans('general.object_not_found'),
222
                    'alert-class' => 'alert-danger',
223
                ]);
224
            }
225
        }
226
227
        $object->fill($request->all());
228
        $object->save();
229
230
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
231
            ->with([
232
                'message'     => trans('general.saved'),
233
                'alert-class' => 'alert-success',
234
            ]);
235
    }
236
237 1
    public function delete($objectId)
238
    {
239
        try {
240 1
            $object = Reservation::findOrFail($objectId);
241
        } catch (ModelNotFoundException $e) {
242
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
243
244
            return response()->json($data);
245
        }
246
247 1
        $object->delete();
248
249 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
250
251 1
        return response()->json($data);
252
    }
253
254
    // TODO
255 1
    public function showAddEditForm($objectId = null)
256
    {
257 1
        if ($objectId === null) {
258
            $dataset = new Reservation();
259
            $title = trans('navigation.add_reservation');
260
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
261
        } else {
262
            try {
263 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
264 1
                ->with('guest:id,first_name,last_name')
265 1
                ->with('room:id,number')
266 1
                ->findOrFail($objectId);
267 1
            } catch (ModelNotFoundException $e) {
268 1
                return $this->returnBack([
269 1
                    'message'     => trans('general.object_not_found'),
270 1
                    'alert-class' => 'alert-danger',
271
                ]);
272
            }
273
274
            $title = trans('navigation.edit_reservation');
275
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
276
        }
277
278
        $viewData = [
279
            'dataset'     => $dataset,
280
            'fields'      => $this->getFields(),
281
            'title'       => $title,
282
            'submitRoute' => $submitRoute,
283
            'routeName'   => $this->reservationTableService->getRouteName(),
284
        ];
285
286
        return view('addedit', $viewData);
287
    }
288
289
    public function getSearchFields()
290
    {
291
        return [
292
            [
293
                'id'    => 'guest',
294
                'title' => trans('general.guest'),
295
                'value' => function (Reservation $data) {
296
                    return $data->guest->full_name;
297
                },
298
                'optional' => [
299
                    'readonly' => 'readonly',
300
                ],
301
            ],
302
            [
303
                'id'    => 'date_start',
304
                'title' => trans('general.date_start'),
305
                'value' => function (Reservation $data) {
306
                    return $data->date_start;
307
                },
308
                'type'     => 'text',
309
                'optional' => [
310
                    'required'    => 'required',
311
                    'class'       => 'datepicker start-date',
312
                    'placeholder' => 'dd.mm.rrrr',
313
                ],
314
            ],
315
            [
316
                'id'    => 'date_end',
317
                'title' => trans('general.date_end'),
318
                'value' => function (Reservation $data) {
319
                    return $data->date_end;
320
                },
321
                'type'     => 'text',
322
                'optional' => [
323
                    'required'    => 'required',
324
                    'class'       => 'datepicker end-date',
325
                    'placeholder' => 'dd.mm.rrrr',
326
                ],
327
            ],
328
            [
329
                'id'    => 'people',
330
                'title' => trans('general.number_of_people'),
331
                'value' => function () {
332
                    return 1;
333
                },
334
                'type'     => 'number',
335
                'optional' => [
336
                    'required' => 'required',
337
                    'min'      => '1',
338
                ],
339
            ],
340
        ];
341
    }
342
343
    // TODO
344
    public function getFields()
345
    {
346
        return [
347
            [
348
                'id'    => 'first_name',
349
                'title' => trans('general.first_name'),
350
                'value' => function (Reservation $data) {
351
                    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...
352
                },
353
                'optional' => [
354
                    'required' => 'required',
355
                ],
356
            ],
357
            [
358
                'id'    => 'last_name',
359
                'title' => trans('general.last_name'),
360
                'value' => function (Reservation $data) {
361
                    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...
362
                },
363
                'optional' => [
364
                    'required' => 'required',
365
                ],
366
            ],
367
            [
368
                'id'    => 'address',
369
                'title' => trans('general.address'),
370
                'value' => function (Reservation $data) {
371
                    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...
372
                },
373
                'optional' => [
374
                    'required' => 'required',
375
                ],
376
            ],
377
            [
378
                'id'    => 'zip_code',
379
                'title' => trans('general.zip_code'),
380
                'value' => function (Reservation $data) {
381
                    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...
382
                },
383
                'optional' => [
384
                    'required'    => 'required',
385
                    'placeholder' => '00-000',
386
                ],
387
            ],
388
            [
389
                'id'    => 'place',
390
                'title' => trans('general.place'),
391
                'value' => function (Reservation $data) {
392
                    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...
393
                },
394
                'optional' => [
395
                    'required' => 'required',
396
                ],
397
            ],
398
            [
399
                'id'    => 'PESEL',
400
                'title' => trans('general.PESEL'),
401
                'value' => function (Reservation $data) {
402
                    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...
403
                },
404
                'optional' => [
405
                    'required'    => 'required',
406
                    'placeholder' => '12345654321',
407
                ],
408
            ],
409
            [
410
                'id'    => 'contact',
411
                'title' => trans('general.contact'),
412
                'value' => function (Reservation $data) {
413
                    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...
414
                },
415
                'type'     => 'textarea',
416
                'optional' => [
417
                    'placeholder' => trans('general.contact_placeholder'),
418
                ],
419
            ],
420
        ];
421
    }
422
423
    private function isReservationDataInSessionCorrect()
424
    {
425
        if (!Session::has(['date_start', 'date_end', 'people'])) {
426
            Log::error('Missing one of Session keys: date_start, date_end, people');
427
428
            return false;
429
        }
430
431
        Session::reflash();
432
433
        return true;
434
    }
435
}
436