Test Failed
Push — master ( ef0807...c259ae )
by Adam
02:47
created

GuestController::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 49
ccs 29
cts 29
cp 1
rs 9.2258
cc 1
eloc 24
nc 1
nop 0
crap 1
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 4
    private function getRouteName()
12
    {
13 4
        return 'guest';
14
    }
15
16
    // TODO
17 3 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 3
        $title = trans('general.guests');
20
21 3
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
22 3
            ->paginate($this->getItemsPerPage());
23
24
        $viewData = [
25 3
            'columns'       => $this->getColumns(),
26 3
            'dataset'       => $dataset,
27 3
            'routeName'     => $this->getRouteName(),
28 3
            'title'         => $title,
29 3
            'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')),
30
        ];
31
32 3
        return view('list', $viewData);
33
    }
34
35
    // TODO
36 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
        if ($id === null) {
39
            $object = new Guest();
40
        } else {
41
            try {
42
                $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
        $object->fill($request->all());
52
        $object->save();
53
54
        return redirect()->route($this->getRouteName().'.index')
55
            ->with([
56
                'message'     => trans('general.saved'),
57
                '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 2 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 2
        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 1
                $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
            $title = trans('general.edit');
87
            $submitRoute = route($this->getRouteName().'.postedit', $id);
88
        }
89
90 1
        $title .= ' '.mb_strtolower(trans('general.guest'));
91
92
        $viewData = [
93 1
            'dataset'     => $dataset,
94 1
            'fields'      => $this->getFields(),
95 1
            'title'       => $title,
96 1
            'submitRoute' => $submitRoute,
97 1
            'routeName'   => $this->getRouteName(),
98
        ];
99
100 1
        return view('addedit', $viewData);
101
    }
102
103
    // TODO
104 1
    private function getFields()
105
    {
106
        return [
107
            [
108 1
                'id'    => 'first_name',
109 1
                'title' => trans('general.first_name'),
110 1
                'value' => function (Guest $data) {
111 1
                    return $data->first_name;
112 1
                },
113
                'optional' => [
114
                    'required' => 'required',
115
                ],
116
            ],
117
            [
118 1
                'id'    => 'last_name',
119 1
                'title' => trans('general.last_name'),
120 1
                'value' => function (Guest $data) {
121 1
                    return $data->last_name;
122 1
                },
123
                'optional' => [
124
                    'required' => 'required',
125
                ],
126
            ],
127
            [
128 1
                'id'    => 'address',
129 1
                'title' => trans('general.address'),
130 1
                'value' => function (Guest $data) {
131 1
                    return $data->address;
132 1
                },
133
                'optional' => [
134
                    'required' => 'required',
135
                ],
136
            ],
137
            [
138 1
                'id'    => 'zip_code',
139 1
                'title' => trans('general.zip_code'),
140 1
                'value' => function (Guest $data) {
141 1
                    return $data->zip_code;
142 1
                },
143 1
                'type'     => 'number',
144
                'optional' => [
145
                    'required'    => 'required',
146
                    'step'        => '0.01',
147
                    'placeholder' => '0.00',
148
                ],
149
            ],
150
            [
151 1
                'id'    => 'place',
152 1
                'title' => trans('general.place'),
153 1
                'value' => function (Guest $data) {
154 1
                    return $data->place;
155 1
                },
156
                'optional' => [
157
                    'required' => 'required',
158
                ],
159
            ],
160
            [
161 1
                'id'    => 'PESEL',
162 1
                'title' => trans('general.PESEL'),
163 1
                'value' => function (Guest $data) {
164 1
                    return $data->PESEL;
165 1
                },
166 1
                'type'     => 'number',
167
                'optional' => [
168
                    'required' => 'required',
169
                ],
170
            ],
171
            [
172 1
                'id'    => 'contact',
173 1
                'title' => trans('general.contact'),
174 1
                'value' => function (Guest $data) {
175 1
                    return $data->contact;
176 1
                },
177 1
                'type' => 'textarea',
178
            ],
179
        ];
180
    }
181
182
    // TODO
183 3
    private function getColumns()
184
    {
185
        $dataset = [
186
            [
187 3
                'title' => trans('general.first_name'),
188 3
                'value' => function (Guest $data) {
189 1
                    return $data->first_name;
190 3
                },
191
            ],
192
            [
193 3
                'title' => trans('general.last_name'),
194 3
                'value' => function (Guest $data) {
195 1
                    return $data->last_name;
196 3
                },
197
            ],
198
            [
199 3
                'title' => trans('general.address'),
200 3
                'value' => function (Guest $data) {
201 1
                    return $data->address;
202 3
                },
203
            ],
204
            [
205 3
                'title' => trans('general.zip_code'),
206 3
                'value' => function (Guest $data) {
207 1
                    return $data->zip_code;
208 3
                },
209
            ],
210
            [
211 3
                'title' => trans('general.place'),
212 3
                'value' => function (Guest $data) {
213 1
                    return $data->place;
214 3
                },
215
            ],
216
            [
217 3
                'title' => trans('general.PESEL'),
218 3
                'value' => function (Guest $data) {
219 1
                    return $data->PESEL;
220 3
                },
221
            ],
222
            [
223 3
                'title' => trans('general.contact'),
224 3
                'value' => function (Guest $data) {
225 1
                    return $data->contact;
226 3
                },
227
            ],
228
        ];
229
230
        return $dataset;
231
    }
232
}
233