Completed
Push — master ( df9caa...85af72 )
by Adam
06:56
created

GuestController::getColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

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