Completed
Push — master ( 7970f7...a844a5 )
by Adam
12:03 queued 05:54
created

ReservationController::chooseFreeRoom()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 34
nc 4
nop 2
dl 0
loc 54
ccs 0
cts 34
cp 0
crap 20
rs 9.0306
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Interfaces\ManageTableInterface;
6
// TODO
7
use App\Http\Requests\GuestRequest;
8
use App\Http\Requests\ReservationSearchRequest;
9
use App\Models\Guest;
10
use App\Models\Reservation;
11
use App\Models\Room;
12
use App\Services\GuestTableService;
13
use App\Services\ReservationTableService;
14
use App\Services\RoomTableService;
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 2
    public function __construct(ReservationTableService $reservationTableService)
24
    {
25 2
        $this->reservationTableService = $reservationTableService;
26 2
    }
27
28 2
    public function index()
29
    {
30 2
        $title = trans('general.reservations');
31
32 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
33 2
            ->with('guest:id,first_name,last_name')
34 2
            ->with('room:id,number')
35 2
            ->paginate($this->getItemsPerPage());
36
37 2
        if ($dataset->isEmpty()) {
38 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
39
        }
40
41
        $viewData = [
42 2
            'columns'       => $this->reservationTableService->getColumns(),
43 2
            'dataset'       => $dataset,
44 2
            'routeName'     => $this->reservationTableService->getRouteName(),
45 2
            'title'         => $title,
46
            // TODO
47 2
            '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 2
        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
    // TODO
119
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')->findOrFail($guestId);
0 ignored issues
show
Unused Code introduced by
The assignment to $guest is dead and can be removed.
Loading history...
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
        $title = trans('navigation.choose_room');
156
157
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
158
            ->whereNotIn('id', function ($query) use ($dateStart, $dateEnd) {
159
                $query->select('room_id')->from('reservations')
160
                    ->where('date_start', '<', $dateEnd)
161
                    ->where('date_end', '>', $dateStart);
162
            })
163
            ->where('capacity', '>=', $people)
164
            ->orderBy('capacity')
165
            ->paginate($this->getItemsPerPage());
166
167
        if ($dataset->isEmpty()) {
168
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
169
        }
170
171
        $viewData = [
172
            'columns'         => $roomTableService->getColumns(),
173
            'dataset'         => $dataset,
174
            'routeName'       => $roomTableService->getRouteName(),
175
            'title'           => $title,
176
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
177
        ];
178
179
        return view('list', $viewData);
180
    }
181
182
    // TODO
183
    public function store(GuestRequest $request, $objectId = null)
184
    {
185
        if ($objectId === null) {
186
            $object = new Reservation();
187
        } else {
188
            try {
189
                $object = Reservation::findOrFail($objectId);
190
            } catch (ModelNotFoundException $e) {
191
                return $this->returnBack([
192
                    'message'     => trans('general.object_not_found'),
193
                    'alert-class' => 'alert-danger',
194
                ]);
195
            }
196
        }
197
198
        $object->fill($request->all());
199
        $object->save();
200
201
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
202
            ->with([
203
                'message'     => trans('general.saved'),
204
                'alert-class' => 'alert-success',
205
            ]);
206
    }
207
208
    public function delete($objectId)
209
    {
210
        Reservation::destroy($objectId);
211
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
212
213
        return response()->json($data);
214
    }
215
216
    // TODO
217 1
    public function showAddEditForm($objectId = null)
218
    {
219 1
        if ($objectId === null) {
220
            $dataset = new Reservation();
221
            $title = trans('navigation.add_reservation');
222
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
223
        } else {
224
            try {
225 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
226 1
                ->with('guest:id,first_name,last_name')
227 1
                ->with('room:id,number')
228 1
                ->findOrFail($objectId);
229 1
            } catch (ModelNotFoundException $e) {
230 1
                return $this->returnBack([
231 1
                    'message'     => trans('general.object_not_found'),
232 1
                    'alert-class' => 'alert-danger',
233
                ]);
234
            }
235
236
            $title = trans('navigation.edit_reservation');
237
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
238
        }
239
240
        $viewData = [
241
            'dataset'     => $dataset,
242
            'fields'      => $this->getFields(),
243
            'title'       => $title,
244
            'submitRoute' => $submitRoute,
245
            'routeName'   => $this->reservationTableService->getRouteName(),
246
        ];
247
248
        return view('addedit', $viewData);
249
    }
250
251
    // TODO
252
    public function getSearchFields()
253
    {
254
        return [
255
            [
256
                'id'    => 'guest',
257
                'title' => trans('general.guest'),
258
                'value' => function (Reservation $data) {
259
                    return $data->guest->full_name;
260
                },
261
                'optional' => [
262
                    'readonly' => 'readonly',
263
                ],
264
            ],
265
            [
266
                'id'    => 'date_start',
267
                'title' => trans('general.date_start'),
268
                'value' => function (Reservation $data) {
269
                    return $data->date_start;
270
                },
271
                'type'     => 'date',
272
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
273
                    'required' => 'required',
274
                ],*/
275
            ],
276
            [
277
                'id'    => 'date_end',
278
                'title' => trans('general.date_end'),
279
                'value' => function (Reservation $data) {
280
                    return $data->date_end;
281
                },
282
                'type'     => 'date',
283
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
284
                    'required' => 'required',
285
                ],*/
286
            ],
287
            [
288
                'id'    => 'people',
289
                'title' => trans('general.number_of_people'),
290
                'value' => function () {
291
                    // TODO
292
                    return 1;
293
                },
294
                'type'     => 'number',
295
                /*'optional' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
296
                    'required' => 'required',
297
                ],*/
298
            ],
299
        ];
300
    }
301
302
    // TODO
303
    public function getFields()
304
    {
305
        return [
306
            [
307
                'id'    => 'first_name',
308
                'title' => trans('general.first_name'),
309
                'value' => function (Reservation $data) {
310
                    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...
311
                },
312
                'optional' => [
313
                    'required' => 'required',
314
                ],
315
            ],
316
            [
317
                'id'    => 'last_name',
318
                'title' => trans('general.last_name'),
319
                'value' => function (Reservation $data) {
320
                    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...
321
                },
322
                'optional' => [
323
                    'required' => 'required',
324
                ],
325
            ],
326
            [
327
                'id'    => 'address',
328
                'title' => trans('general.address'),
329
                'value' => function (Reservation $data) {
330
                    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...
331
                },
332
                'optional' => [
333
                    'required' => 'required',
334
                ],
335
            ],
336
            [
337
                'id'    => 'zip_code',
338
                'title' => trans('general.zip_code'),
339
                'value' => function (Reservation $data) {
340
                    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...
341
                },
342
                'optional' => [
343
                    'required'    => 'required',
344
                    'placeholder' => '00-000',
345
                ],
346
            ],
347
            [
348
                'id'    => 'place',
349
                'title' => trans('general.place'),
350
                'value' => function (Reservation $data) {
351
                    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...
352
                },
353
                'optional' => [
354
                    'required' => 'required',
355
                ],
356
            ],
357
            [
358
                'id'    => 'PESEL',
359
                'title' => trans('general.PESEL'),
360
                'value' => function (Reservation $data) {
361
                    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...
362
                },
363
                'optional' => [
364
                    'required'    => 'required',
365
                    'placeholder' => '12345654321',
366
                ],
367
            ],
368
            [
369
                'id'    => 'contact',
370
                'title' => trans('general.contact'),
371
                'value' => function (Reservation $data) {
372
                    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...
373
                },
374
                'type'     => 'textarea',
375
                'optional' => [
376
                    'placeholder' => trans('general.contact_placeholder'),
377
                ],
378
            ],
379
        ];
380
    }
381
}
382