Completed
Push — master ( 4016c8...bba76d )
by Adam
68:33 queued 62:18
created

ReservationController::showAddEditForm()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.2934

Importance

Changes 0
Metric Value
cc 3
eloc 23
nc 3
nop 1
dl 0
loc 32
ccs 10
cts 21
cp 0.4762
crap 4.2934
rs 8.8571
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(! $this->isReservationDataInSessionCorrect()) {
129
            return $this->returnBack([
130
                'message'     => trans('general.object_not_found'),
131
                'alert-class' => 'alert-danger',
132
            ]);
133
        }
134
135
        try {
136
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
137
        } catch (ModelNotFoundException $e) {
138
            // TODO: logger helper
139
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
140
141
            return $this->returnBack([
142
                'message'     => trans('general.object_not_found'),
143
                'alert-class' => 'alert-danger',
144
            ]);
145
        }
146
147
        $dateStart = Session::get('date_start');
148
        $dateEnd = Session::get('date_end');
149
        $people = Session::get('people');
150
151
        $dateStart = Carbon::parse($dateStart);
152
        $dateEnd = Carbon::parse($dateEnd);
153
154
        $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

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