Passed
Push — master ( 6b12a4...341e7a )
by Adam
05:19
created

ReservationController::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 0
dl 0
loc 74
ccs 0
cts 38
cp 0
crap 2
rs 9.0335
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: Change request
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\Database\Query\Builder;
17
use Illuminate\Support\Facades\Log;
18
use Illuminate\Support\Facades\Session;
19
20
class ReservationController extends Controller implements ManageTableInterface
21
{
22
    protected $reservationTableService;
23
24 3
    public function __construct(ReservationTableService $reservationTableService)
25
    {
26 3
        $this->reservationTableService = $reservationTableService;
27 3
    }
28
29 3
    public function index()
30
    {
31 3
        $title = trans('general.reservations');
32
33 3
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
34 3
            ->with('guest:id,first_name,last_name')
35 3
            ->with('room:id,number')
36 3
            ->paginate($this->getItemsPerPage());
37
38 3
        if ($dataset->isEmpty()) {
39 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
40
        }
41
42
        $viewData = [
43 3
            'columns'       => $this->reservationTableService->getColumns(),
44 3
            'dataset'       => $dataset,
45 3
            'routeName'     => $this->reservationTableService->getRouteName(),
46 3
            'title'         => $title,
47
            // TODO
48 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

48
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
Loading history...
49
        ];
50
51 3
        return view('list', $viewData);
52
    }
53
54
    public function chooseGuest(GuestTableService $guestTableService)
55
    {
56
        $title = trans('navigation.choose_guest');
57
58
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
59
            ->paginate($this->getItemsPerPage());
60
61
        if ($dataset->isEmpty()) {
62
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
63
        }
64
65
        $viewData = [
66
            'columns'         => $guestTableService->getColumns(),
67
            'dataset'         => $dataset,
68
            'routeName'       => $guestTableService->getRouteName(),
69
            'title'           => $title,
70
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search_free_rooms',
71
        ];
72
73
        return view('list', $viewData);
74
    }
75
76
    public function searchFreeRooms($guestId)
77
    {
78
        try {
79
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
80
        } catch (ModelNotFoundException $e) {
81
            return $this->returnBack([
82
                'message'     => trans('general.object_not_found'),
83
                'alert-class' => 'alert-danger',
84
            ]);
85
        }
86
87
        $dataset = new Reservation();
88
        $dataset->guest()->associate($guest);
89
        $title = trans('navigation.search_free_rooms');
90
        $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

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

115
        return redirect()->route($this->reservationTableService->getRouteName().'.choose_free_room', /** @scrutinizer ignore-type */ $guest->id)
Loading history...
116
            ->with($data);
117
    }
118
119
    /**
120
     * @todo
121
     *
122
     * @param RoomTableService $roomTableService
123
     * @param int              $guestId
124
     *
125
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
126
     */
127
    public function chooseFreeRoom(RoomTableService $roomTableService, $guestId)
128
    {
129
        if (!Session::has(['date_start', 'date_end', 'people'])) {
130
            Log::error('Missing one of Session keys: date_start, date_end, people');
131
132
            return $this->returnBack([
133
                'message'     => trans('general.object_not_found'),
134
                'alert-class' => 'alert-danger',
135
            ]);
136
        }
137
138
        Session::reflash();
139
140
        try {
141
            $guest = Guest::select('id', 'first_name', 'last_name')->findOrFail($guestId);
142
        } catch (ModelNotFoundException $e) {
143
            // TODO: logger helper
144
            Log::warning(__CLASS__.'::'.__FUNCTION__.' at '.__LINE__.': '.$e->getMessage());
145
146
            return $this->returnBack([
147
                'message'     => trans('general.object_not_found'),
148
                'alert-class' => 'alert-danger',
149
            ]);
150
        }
151
152
        $dateStart = Session::get('date_start');
153
        $dateEnd = Session::get('date_end');
154
        $people = Session::get('people');
155
156
        $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

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