Test Failed
Push — master ( 37356f...e6a988 )
by Adam
02:49
created

GuestController::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 24
loc 24
ccs 0
cts 14
cp 0
rs 8.9713
cc 3
eloc 16
nc 3
nop 2
crap 12
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 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 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 2
                'type'     => 'number',
144
                'optional' => [
145
                    'required'    => 'required',
146
                    'step'        => '0.01',
147
                    'placeholder' => '0.00',
148
                ],
149
            ],
150
            [
151 2
                'id'    => 'place',
152 2
                'title' => trans('general.place'),
153 2
                'value' => function (Guest $data) {
154 2
                    return $data->place;
155 2
                },
156
                'optional' => [
157
                    'required' => 'required',
158
                ],
159
            ],
160
            [
161 2
                'id'    => 'PESEL',
162 2
                'title' => trans('general.PESEL'),
163 2
                'value' => function (Guest $data) {
164 2
                    return $data->PESEL;
165 2
                },
166 2
                'type'     => 'number',
167
                'optional' => [
168
                    'required' => 'required',
169
                ],
170
            ],
171
            [
172 2
                'id'    => 'contact',
173 2
                'title' => trans('general.contact'),
174 2
                'value' => function (Guest $data) {
175 2
                    return $data->contact;
176 2
                },
177 2
                'type' => 'textarea',
178
            ],
179
        ];
180
    }
181
182
    // TODO
183 4
    private function getColumns()
184
    {
185
        $dataset = [
186
            [
187 4
                'title' => trans('general.first_name'),
188 4
                'value' => function (Guest $data) {
189 2
                    return $data->first_name;
190 4
                },
191
            ],
192
            [
193 4
                'title' => trans('general.last_name'),
194 4
                'value' => function (Guest $data) {
195 2
                    return $data->last_name;
196 4
                },
197
            ],
198
            [
199 4
                'title' => trans('general.address'),
200 4
                'value' => function (Guest $data) {
201 2
                    return $data->address;
202 4
                },
203
            ],
204
            [
205 4
                'title' => trans('general.zip_code'),
206 4
                'value' => function (Guest $data) {
207 2
                    return $data->zip_code;
208 4
                },
209
            ],
210
            [
211 4
                'title' => trans('general.place'),
212 4
                'value' => function (Guest $data) {
213 2
                    return $data->place;
214 4
                },
215
            ],
216
            [
217 4
                'title' => trans('general.PESEL'),
218 4
                'value' => function (Guest $data) {
219 2
                    return $data->PESEL;
220 4
                },
221
            ],
222
            [
223 4
                'title' => trans('general.contact'),
224 4
                'value' => function (Guest $data) {
225 2
                    return $data->contact;
226 4
                },
227
            ],
228
        ];
229
230
        return $dataset;
231
    }
232
}
233