Passed
Push — master ( e6a988...7a5c4e )
by Adam
02:49
created

GuestController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 222
Duplicated Lines 36.04 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 6
dl 80
loc 222
ccs 110
cts 115
cp 0.9565
rs 10

7 Methods

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