Passed
Push — master ( e815f2...b4cf01 )
by Adam
03:17
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
8
class GuestController extends Controller
9
{
10
    private function getRouteName()
11
    {
12
        return 'guest';
13
    }
14
15
    // TODO
16 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
        $title = trans('general.guests');
19
20
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
21
            ->paginate($this->getItemsPerPage());
22
23
        $viewData = [
24
            'columns'       => $this->getColumns(),
25
            'dataset'       => $dataset,
26
            'routeName'     => $this->getRouteName(),
27
            'title'         => $title,
28
            'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')),
29
        ];
30
31
        return view('list', $viewData);
32
    }
33
34
	// TODO
35 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...
36
    {
37
        if ($id === null) {
38
            $object = new Guest();
39
        } else {
40
            try {
41
                $object = Guest::findOrFail($id);
42
            } catch (ModelNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class App\Http\Controllers\ModelNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
43
                return Controller::returnBack([
44
                    'message'     => trans('general.object_not_found'),
45
                    'alert-class' => 'alert-danger',
46
                ]);
47
            }
48
        }
49
50
        $object->fill($request->all());
51
        $object->save();
52
53
        return redirect()->route($this->getRouteName().'.index')
54
            ->with([
55
                'message'     => trans('general.saved'),
56
                'alert-class' => 'alert-success',
57
            ]);
58
    }
59
60
    // TODO
61 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
        Guest::destroy($id);
64
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
65
66
        return response()->json($data);
67
    }
68
69
    // TODO
70 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
        if ($id === null) {
73
            $dataset = new Guest();
74
            $title = trans('general.add');
75
            $submitRoute = route($this->getRouteName().'.postadd');
76
        } else {
77
            try {
78
                $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id);
79
            } catch (ModelNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class App\Http\Controllers\ModelNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
                return Controller::returnBack([
81
                    'message'     => trans('general.object_not_found'),
82
                    'alert-class' => 'alert-danger',
83
                ]);
84
            }
85
86
            $title = trans('general.edit');
87
            $submitRoute = route($this->getRouteName().'.postedit', $id);
88
        }
89
90
        $title .= ' '.mb_strtolower(trans('general.guest'));
91
92
        $viewData = [
93
            'dataset'     => $dataset,
94
            'fields'      => $this->getFields(),
95
            'title'       => $title,
96
            'submitRoute' => $submitRoute,
97
            'routeName'   => $this->getRouteName(),
98
        ];
99
100
        return view('addedit', $viewData);
101
    }
102
103
    // TODO
104
    private function getFields()
105
    {
106
        return [
107
            [
108
                'id'    => 'first_name',
109
                'title' => trans('general.first_name'),
110
                'value' => function (Guest $data) {
111
                    return $data->first_name;
112
                },
113
                'optional' => [
114
                    'required' => 'required',
115
                ],
116
            ],
117
            [
118
                'id'    => 'last_name',
119
                'title' => trans('general.last_name'),
120
                'value' => function (Guest $data) {
121
                    return $data->last_name;
122
                },
123
                'optional' => [
124
                    'required' => 'required',
125
                ],
126
            ],
127
            [
128
                'id'    => 'address',
129
                'title' => trans('general.address'),
130
                'value' => function (Guest $data) {
131
                    return $data->address;
132
                },
133
                'optional' => [
134
                    'required' => 'required',
135
                ],
136
            ],
137
            [
138
                'id'    => 'zip_code',
139
                'title' => trans('general.zip_code'),
140
                'value' => function (Guest $data) {
141
                    return $data->zip_code;
142
                },
143
                'type'     => 'number',
144
                'optional' => [
145
                	'required' => 'required',
146
                    'step'        => '0.01',
147
                    'placeholder' => '0.00',
148
                ],
149
            ],
150
            [
151
                'id'    => 'place',
152
                'title' => trans('general.place'),
153
                'value' => function (Guest $data) {
154
                    return $data->place;
155
                },
156
                'optional' => [
157
                    'required' => 'required',
158
                ],
159
            ],
160
            [
161
                'id'    => 'PESEL',
162
                'title' => trans('general.PESEL'),
163
                'value' => function (Guest $data) {
164
                    return $data->PESEL;
165
                },
166
                'type'     => 'number',
167
                'optional' => [
168
                    'required' => 'required',
169
                ],
170
            ],
171
            [
172
                'id'    => 'contact',
173
                'title' => trans('general.contact'),
174
                'value' => function (Guest $data) {
175
                    return $data->contact;
176
                },
177
                'type' => 'textarea',
178
            ],
179
        ];
180
    }
181
182
    // TODO
183
    private function getColumns()
184
    {
185
        $dataset = [
186
            [
187
                'title' => trans('general.first_name'),
188
                'value' => function (Guest $data) {
189
                    return $data->first_name;
190
                },
191
            ],
192
            [
193
                'title' => trans('general.last_name'),
194
                'value' => function (Guest $data) {
195
                    return $data->last_name;
196
                },
197
            ],
198
            [
199
                'title' => trans('general.address'),
200
                'value' => function (Guest $data) {
201
                    return $data->address;
202
                },
203
            ],
204
            [
205
                'title' => trans('general.zip_code'),
206
                'value' => function (Guest $data) {
207
                    return $data->zip_code;
208
                },
209
            ],
210
            [
211
                'title' => trans('general.place'),
212
                'value' => function (Guest $data) {
213
                    return $data->place;
214
                },
215
            ],
216
            [
217
                'title' => trans('general.PESEL'),
218
                'value' => function (Guest $data) {
219
                    return $data->PESEL;
220
                },
221
            ],
222
            [
223
                'title' => trans('general.contact'),
224
                'value' => function (Guest $data) {
225
                    return $data->contact;
226
                },
227
            ],
228
        ];
229
230
        return $dataset;
231
    }
232
}
233