Completed
Push — master ( 8506a8...f27bf0 )
by Adam
06:27
created

ReservationController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 347
Duplicated Lines 0 %

Test Coverage

Coverage 14.52%

Importance

Changes 0
Metric Value
dl 0
loc 347
ccs 27
cts 186
cp 0.1452
rs 10
c 0
b 0
f 0
wmc 22

11 Methods

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

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

113
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
114
            ->with($data);
115
    }
116
117
    // TODO
118
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
119
    {
120
        if (!Session::has(['date_start', 'date_end', 'people'])) {
121
            return $this->returnBack([
122
                'message'     => trans('general.object_not_found'),
123
                'alert-class' => 'alert-danger',
124
            ]);
125
        }
126
127
        try {
128
            $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...
129
        } catch (ModelNotFoundException $e) {
130
            return $this->returnBack([
131
                'message'     => trans('general.object_not_found'),
132
                'alert-class' => 'alert-danger',
133
            ]);
134
        }
135
136
        // TODO: walidacja
137
        $dateStart = Session::get('date_start');
138
        $dateEnd = Session::get('date_end');
139
        $people = Session::get('people');
140
141
        Session::reflash();
142
143
        $title = trans('navigation.choose_room');
144
145
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
146
            ->whereNotIn('id', function ($query) use ($dateStart, $dateEnd) {
147
                $query->select('room_id')->from('reservations')
148
                    ->where('date_start', '<', $dateEnd)
149
                    ->where('date_end', '>', $dateStart);
150
            })
151
            ->where('capacity', '>=', $people)
152
            ->orderBy('capacity')
153
            ->paginate($this->getItemsPerPage());
154
155
        if ($dataset->isEmpty()) {
156
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
157
        }
158
159
        $viewData = [
160
            'columns'         => $roomTableService->getColumns(),
161
            'dataset'         => $dataset,
162
            'routeName'       => $roomTableService->getRouteName(),
163
            'title'           => $title,
164
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
165
        ];
166
167
        return view('list', $viewData);
168
    }
169
170
    // TODO
171
    public function store(GuestRequest $request, $objectId = null)
172
    {
173
        if ($objectId === null) {
174
            $object = new Reservation();
175
        } else {
176
            try {
177
                $object = Reservation::findOrFail($objectId);
178
            } catch (ModelNotFoundException $e) {
179
                return $this->returnBack([
180
                    'message'     => trans('general.object_not_found'),
181
                    'alert-class' => 'alert-danger',
182
                ]);
183
            }
184
        }
185
186
        $object->fill($request->all());
187
        $object->save();
188
189
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
190
            ->with([
191
                'message'     => trans('general.saved'),
192
                'alert-class' => 'alert-success',
193
            ]);
194
    }
195
196
    public function delete($objectId)
197
    {
198
        Reservation::destroy($objectId);
199
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
200
201
        return response()->json($data);
202
    }
203
204
    // TODO
205 1
    public function showAddEditForm($objectId = null)
206
    {
207 1
        if ($objectId === null) {
208
            $dataset = new Reservation();
209
            $title = trans('navigation.add_reservation');
210
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
211
        } else {
212
            try {
213 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
214 1
                ->with('guest:id,first_name,last_name')
215 1
                ->with('room:id,number')
216 1
                ->findOrFail($objectId);
217 1
            } catch (ModelNotFoundException $e) {
218 1
                return $this->returnBack([
219 1
                    'message'     => trans('general.object_not_found'),
220 1
                    'alert-class' => 'alert-danger',
221
                ]);
222
            }
223
224
            $title = trans('navigation.edit_reservation');
225
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
226
        }
227
228
        $viewData = [
229
            'dataset'     => $dataset,
230
            'fields'      => $this->getFields(),
231
            'title'       => $title,
232
            'submitRoute' => $submitRoute,
233
            'routeName'   => $this->reservationTableService->getRouteName(),
234
        ];
235
236
        return view('addedit', $viewData);
237
    }
238
239
    // TODO
240
    public function getSearchFields()
241
    {
242
        return [
243
            [
244
                'id'    => 'guest',
245
                'title' => trans('general.guest'),
246
                'value' => function (Reservation $data) {
247
                    return $data->guest->full_name;
248
                },
249
                'optional' => [
250
                    'readonly' => 'readonly',
251
                ],
252
            ],
253
            [
254
                'id'    => 'date_start',
255
                'title' => trans('general.date_start'),
256
                'value' => function (Reservation $data) {
257
                    return $data->date_start;
258
                },
259
                'type'     => 'date',
260
                /*'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...
261
                    'required' => 'required',
262
                ],*/
263
            ],
264
            [
265
                'id'    => 'date_end',
266
                'title' => trans('general.date_end'),
267
                'value' => function (Reservation $data) {
268
                    return $data->date_end;
269
                },
270
                'type'     => 'date',
271
                /*'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...
272
                    'required' => 'required',
273
                ],*/
274
            ],
275
            [
276
                'id'    => 'people',
277
                'title' => trans('general.number_of_people'),
278
                'value' => function () {
279
                    // TODO
280
                    return 1;
281
                },
282
                'type'     => 'number',
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
    }
289
290
    // TODO
291
    public function getFields()
292
    {
293
        return [
294
            [
295
                'id'    => 'first_name',
296
                'title' => trans('general.first_name'),
297
                'value' => function (Reservation $data) {
298
                    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...
299
                },
300
                'optional' => [
301
                    'required' => 'required',
302
                ],
303
            ],
304
            [
305
                'id'    => 'last_name',
306
                'title' => trans('general.last_name'),
307
                'value' => function (Reservation $data) {
308
                    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...
309
                },
310
                'optional' => [
311
                    'required' => 'required',
312
                ],
313
            ],
314
            [
315
                'id'    => 'address',
316
                'title' => trans('general.address'),
317
                'value' => function (Reservation $data) {
318
                    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...
319
                },
320
                'optional' => [
321
                    'required' => 'required',
322
                ],
323
            ],
324
            [
325
                'id'    => 'zip_code',
326
                'title' => trans('general.zip_code'),
327
                'value' => function (Reservation $data) {
328
                    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...
329
                },
330
                'optional' => [
331
                    'required'    => 'required',
332
                    'placeholder' => '00-000',
333
                ],
334
            ],
335
            [
336
                'id'    => 'place',
337
                'title' => trans('general.place'),
338
                'value' => function (Reservation $data) {
339
                    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...
340
                },
341
                'optional' => [
342
                    'required' => 'required',
343
                ],
344
            ],
345
            [
346
                'id'    => 'PESEL',
347
                'title' => trans('general.PESEL'),
348
                'value' => function (Reservation $data) {
349
                    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...
350
                },
351
                'optional' => [
352
                    'required'    => 'required',
353
                    'placeholder' => '12345654321',
354
                ],
355
            ],
356
            [
357
                'id'    => 'contact',
358
                'title' => trans('general.contact'),
359
                'value' => function (Reservation $data) {
360
                    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...
361
                },
362
                'type'     => 'textarea',
363
                'optional' => [
364
                    'placeholder' => trans('general.contact_placeholder'),
365
                ],
366
            ],
367
        ];
368
    }
369
}
370