Passed
Push — master ( 9f8bb2...793984 )
by Adam
03:34
created

GuestController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 214
Duplicated Lines 75.7 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 6
dl 162
loc 214
ccs 106
cts 111
cp 0.955
rs 10

7 Methods

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