Test Setup Failed
Push — master ( 50b429...4d06b9 )
by Adam
02:07
created

GuestController::getRouteName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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
                'optional' => [
173
                    'placeholder' => trans('general.contact_placeholder'),
174
                ],
175
            ],
176
        ];
177 4
    }
178
179
    private function getColumns()
180
    {
181 4
        $dataset = [
182 4
            [
183 2
                'title' => trans('general.first_name'),
184 4
                'value' => function (Guest $data) {
185
                    return $data->first_name;
186
                },
187 4
            ],
188 4
            [
189 2
                'title' => trans('general.last_name'),
190 4
                'value' => function (Guest $data) {
191
                    return $data->last_name;
192
                },
193 4
            ],
194 4
            [
195 2
                'title' => trans('general.address'),
196 4
                'value' => function (Guest $data) {
197
                    return $data->address.', '.$data->zip_code.' '.$data->place;
198
                },
199 4
            ],
200 4
            [
201 2
                'title' => trans('general.PESEL'),
202 4
                'value' => function (Guest $data) {
203
                    return $data->PESEL;
204
                },
205 4
            ],
206 4
            [
207 2
                'title' => trans('general.contact'),
208 4
                'value' => function (Guest $data) {
209
                    return $data->contact;
210
                },
211 4
            ],
212 4
        ];
213 2
214 4
        return $dataset;
215
    }
216
}
217