Passed
Branch master (065bfc)
by Adam
05:23
created

ReservationController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
// TODO
6
use App\Http\Requests\GuestRequest;
7
use App\Models\Reservation;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
10
class ReservationController extends Controller
11
{
12
    private function getRouteName()
13
    {
14
        return 'reservation';
15
    }
16
17
    public function index()
18
    {
19
        $title = trans('general.reservations');
20
21
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
22
            ->with('guest:id,first_name,last_name')
23
            ->with('room:id,number')
24
            ->paginate($this->getItemsPerPage());
25
26
        if ($dataset->isEmpty()) {
27
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
28
        }
29
30
        $viewData = [
31
            'columns'       => $this->getColumns(),
32
            'dataset'       => $dataset,
33
            'routeName'     => $this->getRouteName(),
34
            'title'         => $title,
35
            // TODO
36
            '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

36
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
Loading history...
37
        ];
38
39
        return view('list', $viewData);
40
    }
41
42
    // TODO
43
    public function store(GuestRequest $request, $id = null)
44
    {
45
        if ($id === null) {
46
            $object = new Reservation();
47
        } else {
48
            try {
49
                $object = Reservation::findOrFail($id);
50
            } catch (ModelNotFoundException $e) {
51
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

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

51
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
52
                    'message'     => trans('general.object_not_found'),
53
                    'alert-class' => 'alert-danger',
54
                ]);
55
            }
56
        }
57
58
        $object->fill($request->all());
59
        $object->save();
60
61
        return redirect()->route($this->getRouteName().'.index')
62
            ->with([
63
                'message'     => trans('general.saved'),
64
                'alert-class' => 'alert-success',
65
            ]);
66
    }
67
68
    public function delete($id)
69
    {
70
        Reservation::destroy($id);
71
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
72
73
        return response()->json($data);
74
    }
75
76
    // TODO
77
    public function showAddEditForm($id = null)
78
    {
79
        if ($id === null) {
80
            $dataset = new Reservation();
81
            $title = trans('navigation.add_guest');
82
            $submitRoute = route($this->getRouteName().'.postadd');
83
        } else {
84
            try {
85
                $dataset = Reservation::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
86
            } catch (ModelNotFoundException $e) {
87
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

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

87
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
88
                    'message'     => trans('general.object_not_found'),
89
                    'alert-class' => 'alert-danger',
90
                ]);
91
            }
92
93
            $title = trans('navigation.edit_guest');
94
            $submitRoute = route($this->getRouteName().'.postedit', $id);
95
        }
96
97
        $viewData = [
98
            'dataset'     => $dataset,
99
            'fields'      => $this->getFields(),
100
            'title'       => $title,
101
            'submitRoute' => $submitRoute,
102
            'routeName'   => $this->getRouteName(),
103
        ];
104
105
        return view('addedit', $viewData);
106
    }
107
108
    // TODO
109
    private function getFields()
110
    {
111
        return [
112
            [
113
                'id'    => 'first_name',
114
                'title' => trans('general.first_name'),
115
                'value' => function (Reservation $data) {
116
                    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...
117
                },
118
                'optional' => [
119
                    'required' => 'required',
120
                ],
121
            ],
122
            [
123
                'id'    => 'last_name',
124
                'title' => trans('general.last_name'),
125
                'value' => function (Reservation $data) {
126
                    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...
127
                },
128
                'optional' => [
129
                    'required' => 'required',
130
                ],
131
            ],
132
            [
133
                'id'    => 'address',
134
                'title' => trans('general.address'),
135
                'value' => function (Reservation $data) {
136
                    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...
137
                },
138
                'optional' => [
139
                    'required' => 'required',
140
                ],
141
            ],
142
            [
143
                'id'    => 'zip_code',
144
                'title' => trans('general.zip_code'),
145
                'value' => function (Reservation $data) {
146
                    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...
147
                },
148
                'optional' => [
149
                    'required'    => 'required',
150
                    'placeholder' => '00-000',
151
                ],
152
            ],
153
            [
154
                'id'    => 'place',
155
                'title' => trans('general.place'),
156
                'value' => function (Reservation $data) {
157
                    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...
158
                },
159
                'optional' => [
160
                    'required' => 'required',
161
                ],
162
            ],
163
            [
164
                'id'    => 'PESEL',
165
                'title' => trans('general.PESEL'),
166
                'value' => function (Reservation $data) {
167
                    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...
168
                },
169
                'optional' => [
170
                    'required'    => 'required',
171
                    'placeholder' => '12345654321',
172
                ],
173
            ],
174
            [
175
                'id'    => 'contact',
176
                'title' => trans('general.contact'),
177
                'value' => function (Reservation $data) {
178
                    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...
179
                },
180
                'type'     => 'textarea',
181
                'optional' => [
182
                    'placeholder' => trans('general.contact_placeholder'),
183
                ],
184
            ],
185
        ];
186
    }
187
188
    // TODO
189
    private function getColumns()
190
    {
191
        $dataset = [
192
            [
193
                'title' => trans('general.room'),
194
                'value' => function (Reservation $data) {
195
                    return $data->room->number;
196
                },
197
            ],
198
            [
199
                'title' => trans('general.guest'),
200
                'value' => function (Reservation $data) {
201
                    return $data->guest->first_name.' '.$data->guest->last_name;
202
                },
203
            ],
204
            [
205
                'title' => trans('general.date_start'),
206
                'value' => function (Reservation $data) {
207
                    return $data->date_start;
208
                },
209
            ],
210
            [
211
                'title' => trans('general.date_end'),
212
                'value' => function (Reservation $data) {
213
                    return $data->date_end;
214
                },
215
            ],
216
            [
217
                'title' => trans('general.people'),
218
                'value' => function (Reservation $data) {
219
                    return $data->people;
220
                },
221
            ],
222
        ];
223
224
        return $dataset;
225
    }
226
}
227