Passed
Push — master ( ea0194...0a4772 )
by Adam
05:23
created

ReservationController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 2.2109
rs 9.4285
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 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
            // 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
     * @param RoomTableService $roomTableService
120
     * @param int              $guestId
121
     *
122
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
123
     */
124
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
125
    {
126
        if (!$this->isReservationDataInSessionCorrect()) {
127
            return $this->returnBack([
128
                'message'     => trans('general.object_not_found'),
129
                'alert-class' => 'alert-danger',
130
            ]);
131
        }
132
133
        try {
134
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
135
        } catch (ModelNotFoundException $e) {
136
            // TODO: logger helper
137
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
138
139
            return $this->returnBack([
140
                'message'     => trans('general.object_not_found'),
141
                'alert-class' => 'alert-danger',
142
            ]);
143
        }
144
145
        $dateStart = Session::get('date_start');
146
        $dateEnd = Session::get('date_end');
147
        $people = Session::get('people');
148
149
        $dateStart = Carbon::parse($dateStart);
150
        $dateEnd = Carbon::parse($dateEnd);
151
152
        $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

152
        $title = /** @scrutinizer ignore-type */ trans('navigation.choose_room_for').' '.$guest->fullName;
Loading history...
153
154
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
155
            ->freeRoomsForReservation($dateStart, $dateEnd, $people)
156
            ->paginate($this->getItemsPerPage());
157
158
        if ($dataset->isEmpty()) {
159
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
160
        }
161
162
        $viewData = [
163
            'columns'                => $roomTableService->getColumns(),
164
            'dataset'                => $dataset,
165
            'routeName'              => $roomTableService->getRouteName(),
166
            'title'                  => $title,
167
            'routeChooseName'        => $this->reservationTableService->getRouteName().'.add',
168
            'secondRouteChooseParam' => $guest->id,
169
        ];
170
171
        return view('list', $viewData);
172
    }
173
174
    public function add($roomId, $guestId)
175
    {
176
        if (!$this->isReservationDataInSessionCorrect()) {
177
            return $this->returnBack([
178
                'message'     => trans('general.object_not_found'),
179
                'alert-class' => 'alert-danger',
180
            ]);
181
        }
182
183
        $dateStart = Session::get('date_start');
184
        $dateEnd = Session::get('date_end');
185
        $people = Session::get('people');
186
187
        try {
188
            $guest = Guest::select('id')->findOrFail($guestId);
189
            $room = Room::select('id')->findOrFail($roomId);
190
        } catch (ModelNotFoundException $e) {
191
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
192
193
            return $this->returnBack([
194
                'message'     => trans('general.object_not_found'),
195
                'alert-class' => 'alert-danger',
196
            ]);
197
        }
198
199
        $reservation = new Reservation();
200
        $reservation->guest_id = $guest->id;
201
        $reservation->room_id = $room->id;
202
        $reservation->date_start = $dateStart;
203
        $reservation->date_end = $dateEnd;
204
        $reservation->people = $people;
205
206
        $reservation->save();
207
208
        $this->addFlashMessage(trans('general.saved'), 'alert-success');
209
210
        return redirect()->route($this->reservationTableService->getRouteName().'.index');
211
    }
212
213
    // TODO
214
    public function store(GuestRequest $request, $objectId = null)
215
    {
216
        if ($objectId === null) {
217
            $object = new Reservation();
218
        } else {
219
            try {
220
                $object = Reservation::findOrFail($objectId);
221
            } catch (ModelNotFoundException $e) {
222
                return $this->returnBack([
223
                    'message'     => trans('general.object_not_found'),
224
                    'alert-class' => 'alert-danger',
225
                ]);
226
            }
227
        }
228
229
        $object->fill($request->all());
230
        $object->save();
231
232
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
233
            ->with([
234
                'message'     => trans('general.saved'),
235
                'alert-class' => 'alert-success',
236
            ]);
237
    }
238
239 1
    public function delete($objectId)
240
    {
241
        try {
242 1
            $object = Reservation::findOrFail($objectId);
243
        } catch (ModelNotFoundException $e) {
244
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
245
            return response()->json($data);
246
        }
247
248 1
        $object->delete();
249
250 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
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