Completed
Push — master ( fe124f...1ae642 )
by Adam
13:03 queued 07:15
created

ReservationController::postSearchFreeRooms()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 6
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
// TODO
7
use App\Http\Requests\GuestRequest;
8
use App\Http\Requests\ReservationSearchRequest;
9
use App\Models\Guest;
10
use App\Models\Room;
11
use App\Models\Reservation;
12
use App\Services\GuestTableService;
13
use App\Services\RoomTableService;
14
use App\Services\ReservationTableService;
15
use Illuminate\Database\Eloquent\ModelNotFoundException;
16
17
class ReservationController extends Controller implements ManageTableInterface
18
{
19
    protected $reservationTableService;
20
21 2
    public function __construct(ReservationTableService $reservationTableService)
22
    {
23 2
        $this->reservationTableService = $reservationTableService;
24 2
    }
25
26 2
    public function index()
27
    {
28 2
        $title = trans('general.reservations');
29
30 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
31 2
            ->with('guest:id,first_name,last_name')
32 2
            ->with('room:id,number')
33 2
            ->paginate($this->getItemsPerPage());
34
35 2
        if ($dataset->isEmpty()) {
36 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
37
        }
38
39
        $viewData = [
40 2
            'columns'       => $this->reservationTableService->getColumns(),
41 2
            'dataset'       => $dataset,
42 2
            'routeName'     => $this->reservationTableService->getRouteName(),
43 2
            'title'         => $title,
44
            // TODO
45 2
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
46
        ];
47
48 2
        return view('list', $viewData);
49
    }
50
51
    public function chooseGuest(GuestTableService $guestTableService)
52
    {
53
        $title = trans('general.choose_guest');
54
55
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
56
            ->paginate($this->getItemsPerPage());
57
58
        if ($dataset->isEmpty()) {
59
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
60
        }
61
62
        $viewData = [
63
            'columns'         => $guestTableService->getColumns(),
64
            'dataset'         => $dataset,
65
            'routeName'       => $guestTableService->getRouteName(),
66
            'title'           => $title,
67
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
68
        ];
69
70
        return view('list', $viewData);
71
    }
72
73
    public function searchFreeRooms($guestId)
74
    {
75
        try {
76
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
77
        } catch (ModelNotFoundException $e) {
78
            return $this->returnBack([
79
                'message'     => trans('general.object_not_found'),
80
                'alert-class' => 'alert-danger',
81
            ]);
82
        }
83
84
        $dataset = new Reservation();
85
        $dataset->guest()->associate($guest);
86
        $title = trans('navigation.search_free_rooms');
87
        $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

87
        $submitRoute = route($this->reservationTableService->getRouteName().'.post_search_free_rooms', /** @scrutinizer ignore-type */ $dataset->guest->id);
Loading history...
88
89
        $viewData = [
90
            'dataset'     => $dataset,
91
            'fields'      => $this->getSearchFields(),
92
            'title'       => $title,
93
            'submitRoute' => $submitRoute,
94
        ];
95
96
        return view('addedit', $viewData);
97
    }
98
99
    public function postSearchFreeRooms(ReservationSearchRequest $request, $guestId = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    public function postSearchFreeRooms(/** @scrutinizer ignore-unused */ ReservationSearchRequest $request, $guestId = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

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