Completed
Push — master ( df9caa...85af72 )
by Adam
06:56
created

ReservationController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 213
Duplicated Lines 74.65 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 8
dl 159
loc 213
ccs 0
cts 110
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteName() 0 4 1
A index() 20 20 1
B store() 24 24 3
A delete() 7 7 1
B showAddEditForm() 30 30 3
B getFields() 78 78 1
B getColumns() 0 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function index()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $viewData = [
27
            'columns'       => $this->getColumns(),
28
            'dataset'       => $dataset,
29
            'routeName'     => $this->getRouteName(),
30
            'title'         => $title,
31
            // TODO
32
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
33
        ];
34
35
        return view('list', $viewData);
36
    }
37
38
    // TODO
39 View Code Duplication
    public function store(GuestRequest $request, $id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        if ($id === null) {
42
            $object = new Reservation();
43
        } else {
44
            try {
45
                $object = Reservation::findOrFail($id);
46
            } catch (ModelNotFoundException $e) {
47
                return Controller::returnBack([
48
                    'message'     => trans('general.object_not_found'),
49
                    'alert-class' => 'alert-danger',
50
                ]);
51
            }
52
        }
53
54
        $object->fill($request->all());
55
        $object->save();
56
57
        return redirect()->route($this->getRouteName().'.index')
58
            ->with([
59
                'message'     => trans('general.saved'),
60
                'alert-class' => 'alert-success',
61
            ]);
62
    }
63
64 View Code Duplication
    public function delete($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        Reservation::destroy($id);
67
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
68
69
        return response()->json($data);
70
    }
71
72
    // TODO
73 View Code Duplication
    public function showAddEditForm($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        if ($id === null) {
76
            $dataset = new Reservation();
77
            $title = trans('navigation.add_guest');
78
            $submitRoute = route($this->getRouteName().'.postadd');
79
        } else {
80
            try {
81
                $dataset = Reservation::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
82
            } catch (ModelNotFoundException $e) {
83
                return Controller::returnBack([
84
                    'message'     => trans('general.object_not_found'),
85
                    'alert-class' => 'alert-danger',
86
                ]);
87
            }
88
89
            $title = trans('navigation.edit_guest');
90
            $submitRoute = route($this->getRouteName().'.postedit', $id);
91
        }
92
93
        $viewData = [
94
            'dataset'     => $dataset,
95
            'fields'      => $this->getFields(),
96
            'title'       => $title,
97
            'submitRoute' => $submitRoute,
98
            'routeName'   => $this->getRouteName(),
99
        ];
100
101
        return view('addedit', $viewData);
102
    }
103
104
    // TODO
105 View Code Duplication
    private function getFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        return [
108
            [
109
                'id'    => 'first_name',
110
                'title' => trans('general.first_name'),
111
                'value' => function (Reservation $data) {
112
                    return $data->first_name;
113
                },
114
                'optional' => [
115
                    'required' => 'required',
116
                ],
117
            ],
118
            [
119
                'id'    => 'last_name',
120
                'title' => trans('general.last_name'),
121
                'value' => function (Reservation $data) {
122
                    return $data->last_name;
123
                },
124
                'optional' => [
125
                    'required' => 'required',
126
                ],
127
            ],
128
            [
129
                'id'    => 'address',
130
                'title' => trans('general.address'),
131
                'value' => function (Reservation $data) {
132
                    return $data->address;
133
                },
134
                'optional' => [
135
                    'required' => 'required',
136
                ],
137
            ],
138
            [
139
                'id'    => 'zip_code',
140
                'title' => trans('general.zip_code'),
141
                'value' => function (Reservation $data) {
142
                    return $data->zip_code;
143
                },
144
                'optional' => [
145
                    'required'    => 'required',
146
                    'placeholder' => '00-000',
147
                ],
148
            ],
149
            [
150
                'id'    => 'place',
151
                'title' => trans('general.place'),
152
                'value' => function (Reservation $data) {
153
                    return $data->place;
154
                },
155
                'optional' => [
156
                    'required' => 'required',
157
                ],
158
            ],
159
            [
160
                'id'    => 'PESEL',
161
                'title' => trans('general.PESEL'),
162
                'value' => function (Reservation $data) {
163
                    return $data->PESEL;
164
                },
165
                'optional' => [
166
                    'required'    => 'required',
167
                    'placeholder' => '12345654321',
168
                ],
169
            ],
170
            [
171
                'id'    => 'contact',
172
                'title' => trans('general.contact'),
173
                'value' => function (Reservation $data) {
174
                    return $data->contact;
175
                },
176
                'type'     => 'textarea',
177
                'optional' => [
178
                    'placeholder' => trans('general.contact_placeholder'),
179
                ],
180
            ],
181
        ];
182
    }
183
184
    // TODO
185
    private function getColumns()
186
    {
187
        $dataset = [
188
            [
189
                'title' => trans('general.room'),
190
                'value' => function (Reservation $data) {
191
                    return $data->room->number;
192
                },
193
            ],
194
            [
195
                'title' => trans('general.guest'),
196
                'value' => function (Reservation $data) {
197
                    return $data->guest->first_name.' '.$data->guest->last_name;
198
                },
199
            ],
200
            [
201
                'title' => trans('general.date_start'),
202
                'value' => function (Reservation $data) {
203
                    return $data->date_start;
204
                },
205
            ],
206
            [
207
                'title' => trans('general.date_end'),
208
                'value' => function (Reservation $data) {
209
                    return $data->date_end;
210
                },
211
            ],
212
            [
213
                'title' => trans('general.people'),
214
                'value' => function (Reservation $data) {
215
                    return $data->people;
216
                },
217
            ],
218
        ];
219
220
        return $dataset;
221
    }
222
}
223