Passed
Push — master ( e024e1...22d94f )
by Adam
05:19
created

ReservationController::getColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1.0117

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 36
ccs 17
cts 22
cp 0.7727
crap 1.0117
rs 8.8571
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 1
    private function getRouteName()
13
    {
14 1
        return 'reservation';
15
    }
16
17 1
    public function index()
18
    {
19 1
        $title = trans('general.reservations');
20
21 1
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
22 1
            ->with('guest:id,first_name,last_name')
23 1
            ->with('room:id,number')
24 1
            ->paginate($this->getItemsPerPage());
25
26 1
        if ($dataset->isEmpty()) {
27 1
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
28
        }
29
30
        $viewData = [
31 1
            'columns'       => $this->getColumns(),
32 1
            'dataset'       => $dataset,
33 1
            'routeName'     => $this->getRouteName(),
34 1
            'title'         => $title,
35
            // TODO
36 1
            '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 1
        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 $this->returnBack([
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 $this->returnBack([
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 1
    private function getColumns()
190
    {
191
        $dataset = [
192
            [
193 1
                'title' => trans('general.room'),
194 1
                'value' => function (Reservation $data) {
195
                    return $data->room->number;
196 1
                },
197
            ],
198
            [
199 1
                'title' => trans('general.guest'),
200 1
                'value' => function (Reservation $data) {
201
                    return $data->guest->first_name.' '.$data->guest->last_name;
202 1
                },
203
            ],
204
            [
205 1
                'title' => trans('general.date_start'),
206 1
                'value' => function (Reservation $data) {
207
                    return $data->date_start;
208 1
                },
209
            ],
210
            [
211 1
                'title' => trans('general.date_end'),
212 1
                'value' => function (Reservation $data) {
213
                    return $data->date_end;
214 1
                },
215
            ],
216
            [
217 1
                'title' => trans('general.people'),
218 1
                'value' => function (Reservation $data) {
219
                    return $data->people;
220 1
                },
221
            ],
222
        ];
223
224 1
        return $dataset;
225
    }
226
}
227