Passed
Push — master ( fd530b...c40cd5 )
by Adam
02:41
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 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
            ],
173
        ];
174
    }
175
176
    // TODO
177 4
    private function getColumns()
178
    {
179
        $dataset = [
180
            [
181 4
                'title' => trans('general.first_name'),
182 4
                'value' => function (Guest $data) {
183 2
                    return $data->first_name;
184 4
                },
185
            ],
186
            [
187 4
                'title' => trans('general.last_name'),
188 4
                'value' => function (Guest $data) {
189 2
                    return $data->last_name;
190 4
                },
191
            ],
192
            [
193 4
                'title' => trans('general.address'),
194 4
                'value' => function (Guest $data) {
195 2
                    return $data->address;
196 4
                },
197
            ],
198
            [
199 4
                'title' => trans('general.zip_code'),
200 4
                'value' => function (Guest $data) {
201 2
                    return $data->zip_code;
202 4
                },
203
            ],
204
            [
205 4
                'title' => trans('general.place'),
206 4
                'value' => function (Guest $data) {
207 2
                    return $data->place;
208 4
                },
209
            ],
210
            [
211 4
                'title' => trans('general.PESEL'),
212 4
                'value' => function (Guest $data) {
213 2
                    return $data->PESEL;
214 4
                },
215
            ],
216
            [
217 4
                'title' => trans('general.contact'),
218 4
                'value' => function (Guest $data) {
219 2
                    return $data->contact;
220 4
                },
221
            ],
222
        ];
223
224
        return $dataset;
225
    }
226
}
227