Completed
Push — master ( 793984...065bfc )
by Adam
03:06
created

GuestController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 213
Duplicated Lines 75.59 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 6
dl 161
loc 213
ccs 105
cts 110
cp 0.9545
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteName() 0 4 1
A index() 22 22 2
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
use App\Http\Requests\GuestRequest;
6
use App\Models\Guest;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
9
class GuestController extends Controller
10
{
11 5
    private function getRouteName()
12
    {
13 5
        return 'guest';
14
    }
15
16 4 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...
17
    {
18 4
        $title = trans('general.guests');
19
20 4
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
21 4
            ->paginate($this->getItemsPerPage());
22
23 4
        if ($dataset->isEmpty()) {
24 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
25
        }
26
27
        $viewData = [
28 4
            'columns'       => $this->getColumns(),
29 4
            'dataset'       => $dataset,
30 4
            'routeName'     => $this->getRouteName(),
31 4
            'title'         => $title,
32
            // TODO
33 4
            'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')),
34
        ];
35
36 4
        return view('list', $viewData);
37
    }
38
39 1 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 1
        if ($id === null) {
42
            $object = new Guest();
43
        } else {
44
            try {
45 1
                $object = Guest::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 1
        $object->fill($request->all());
55 1
        $object->save();
56
57 1
        return redirect()->route($this->getRouteName().'.index')
58 1
            ->with([
59 1
                'message'     => trans('general.saved'),
60 1
                'alert-class' => 'alert-success',
61
            ]);
62
    }
63
64 1 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 1
        Guest::destroy($id);
67 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
68
69 1
        return response()->json($data);
70
    }
71
72
    // TODO
73 3 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 3
        if ($id === null) {
76 1
            $dataset = new Guest();
77 1
            $title = trans('navigation.add_guest');
78 1
            $submitRoute = route($this->getRouteName().'.postadd');
79
        } else {
80
            try {
81 2
                $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
82 1
            } catch (ModelNotFoundException $e) {
83 1
                return Controller::returnBack([
84 1
                    'message'     => trans('general.object_not_found'),
85 1
                    'alert-class' => 'alert-danger',
86
                ]);
87
            }
88
89 1
            $title = trans('navigation.edit_guest');
90 1
            $submitRoute = route($this->getRouteName().'.postedit', $id);
91
        }
92
93
        $viewData = [
94 2
            'dataset'     => $dataset,
95 2
            'fields'      => $this->getFields(),
96 2
            'title'       => $title,
97 2
            'submitRoute' => $submitRoute,
98 2
            'routeName'   => $this->getRouteName(),
99
        ];
100
101 2
        return view('addedit', $viewData);
102
    }
103
104
    // TODO
105 2 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 2
                'id'    => 'first_name',
110 2
                'title' => trans('general.first_name'),
111 2
                'value' => function (Guest $data) {
112 2
                    return $data->first_name;
113 2
                },
114
                'optional' => [
115
                    'required' => 'required',
116
                ],
117
            ],
118
            [
119 2
                'id'    => 'last_name',
120 2
                'title' => trans('general.last_name'),
121 2
                'value' => function (Guest $data) {
122 2
                    return $data->last_name;
123 2
                },
124
                'optional' => [
125
                    'required' => 'required',
126
                ],
127
            ],
128
            [
129 2
                'id'    => 'address',
130 2
                'title' => trans('general.address'),
131 2
                'value' => function (Guest $data) {
132 2
                    return $data->address;
133 2
                },
134
                'optional' => [
135
                    'required' => 'required',
136
                ],
137
            ],
138
            [
139 2
                'id'    => 'zip_code',
140 2
                'title' => trans('general.zip_code'),
141 2
                'value' => function (Guest $data) {
142 2
                    return $data->zip_code;
143 2
                },
144
                'optional' => [
145
                    'required'    => 'required',
146
                    'placeholder' => '00-000',
147
                ],
148
            ],
149
            [
150 2
                'id'    => 'place',
151 2
                'title' => trans('general.place'),
152 2
                'value' => function (Guest $data) {
153 2
                    return $data->place;
154 2
                },
155
                'optional' => [
156
                    'required' => 'required',
157
                ],
158
            ],
159
            [
160 2
                'id'    => 'PESEL',
161 2
                'title' => trans('general.PESEL'),
162 2
                'value' => function (Guest $data) {
163 2
                    return $data->PESEL;
164 2
                },
165
                'optional' => [
166
                    'required'    => 'required',
167
                    'placeholder' => '12345654321',
168
                ],
169
            ],
170
            [
171 2
                'id'    => 'contact',
172 2
                'title' => trans('general.contact'),
173 2
                'value' => function (Guest $data) {
174 2
                    return $data->contact;
175 2
                },
176 2
                'type'     => 'textarea',
177
                'optional' => [
178 2
                    'placeholder' => trans('general.contact_placeholder'),
179
                ],
180
            ],
181
        ];
182
    }
183
184 4
    private function getColumns()
185
    {
186
        $dataset = [
187
            [
188 4
                'title' => trans('general.first_name'),
189 4
                'value' => function (Guest $data) {
190 2
                    return $data->first_name;
191 4
                },
192
            ],
193
            [
194 4
                'title' => trans('general.last_name'),
195 4
                'value' => function (Guest $data) {
196 2
                    return $data->last_name;
197 4
                },
198
            ],
199
            [
200 4
                'title' => trans('general.address'),
201 4
                'value' => function (Guest $data) {
202 2
                    return $data->address.', '.$data->zip_code.' '.$data->place;
203 4
                },
204
            ],
205
            [
206 4
                'title' => trans('general.PESEL'),
207 4
                'value' => function (Guest $data) {
208 2
                    return $data->PESEL;
209 4
                },
210
            ],
211
            [
212 4
                'title' => trans('general.contact'),
213 4
                'value' => function (Guest $data) {
214 2
                    return $data->contact;
215 4
                },
216
            ],
217
        ];
218
219 4
        return $dataset;
220
    }
221
}
222