Completed
Push — master ( 6ffea5...ef6d85 )
by Adam
104:53 queued 98:00
created

ReservationController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 23
ccs 14
cts 14
cp 1
crap 2
rs 9.0856
c 0
b 0
f 0
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 3
    public function __construct(ReservationTableService $reservationTableService)
24
    {
25 3
        $this->reservationTableService = $reservationTableService;
26 3
    }
27
28 3
    public function index()
29
    {
30 3
        $title = trans('general.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
            // TODO
47 3
            '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 3
        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
    /**
119
     * @todo
120
     *
121
     * @param RoomTableService $roomTableService
122
     * @param int              $guestId
123
     *
124
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
125
     */
126
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
127
    {
128
        if (!Session::has(['date_start', 'date_end', 'people'])) {
129
            Log::error('Missing one of Session keys: date_start, date_end, people');
130
131
            return $this->returnBack([
132
                'message'     => trans('general.object_not_found'),
133
                'alert-class' => 'alert-danger',
134
            ]);
135
        }
136
137
        Session::reflash();
138
139
        try {
140
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
141
        } catch (ModelNotFoundException $e) {
142
            // TODO: logger helper
143
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
144
145
            return $this->returnBack([
146
                'message'     => trans('general.object_not_found'),
147
                'alert-class' => 'alert-danger',
148
            ]);
149
        }
150
151
        $dateStart = Session::get('date_start');
152
        $dateEnd = Session::get('date_end');
153
        $people = Session::get('people');
154
155
        $dateStart = Carbon::parse($dateStart);
156
        $dateEnd = Carbon::parse($dateEnd);
157
158
        $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

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