Passed
Push — master ( 4d06b9...df9caa )
by Adam
02:51
created

GuestController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 208
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 6
dl 48
loc 208
ccs 102
cts 107
cp 0.9533
rs 10

7 Methods

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