Passed
Push — master ( 9f8bb2...793984 )
by Adam
03:34
created

RoomController::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
ccs 30
cts 30
cp 1
rs 9.6818
cc 1
eloc 35
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\RoomRequest;
6
use App\Models\Room;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Illuminate\Support\Facades\Session;
9
10
class RoomController extends Controller
11
{
12 5
    private function getRouteName()
13
    {
14 5
        return 'room';
15
    }
16
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.rooms');
20
21 4
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
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.room')).' '.mb_strtolower(trans('general.number')),
30
        ];
31
32 4
        if ($dataset->isEmpty() && !Session::has('message')) {
33 2
            Session::flash('message', trans('general.no_rooms_in_database'));
34 2
            Session::flash('alert-class', 'alert-danger');
35
        }
36
37 4
        return view('list', $viewData);
38
    }
39
40 1 View Code Duplication
    public function store(RoomRequest $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...
41
    {
42 1
        if ($id === null) {
43
            $object = new Room();
44
        } else {
45
            try {
46 1
                $object = Room::findOrFail($id);
47
            } catch (ModelNotFoundException $e) {
48
                return Controller::returnBack([
49
                    'message'     => trans('general.object_not_found'),
50
                    'alert-class' => 'alert-danger',
51
                ]);
52
            }
53
        }
54
55 1
        $object->fill($request->all());
56 1
        $object->save();
57
58 1
        return redirect()->route($this->getRouteName().'.index')
59 1
            ->with([
60 1
                'message'     => trans('general.saved'),
61 1
                'alert-class' => 'alert-success',
62
            ]);
63
    }
64
65 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...
66
    {
67 1
        Room::destroy($id);
68 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
69
70 1
        return response()->json($data);
71
    }
72
73 3
    public function showAddEditForm($id = null)
74
    {
75 3
        if ($id === null) {
76 1
            $dataset = new Room();
77 1
            $title = trans('general.add');
78 1
            $submitRoute = route($this->getRouteName().'.postadd');
79
        } else {
80
            try {
81 2
                $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')->findOrFail($id);
82 1
            } catch (ModelNotFoundException $e) {
83 1
                return Controller::returnBack([
84 1
                    'message'     => trans('general.object_not_found'),
85 1
                    'alert-class' => 'alert-danger',
86
                ]);
87
            }
88
89 1
            $title = trans('general.edit');
90 1
            $submitRoute = route($this->getRouteName().'.postedit', $id);
91
        }
92
93 2
        $title .= ' '.mb_strtolower(trans('general.room'));
94
95
        $viewData = [
96 2
            'dataset'     => $dataset,
97 2
            'fields'      => $this->getFields(),
98 2
            'title'       => $title,
99 2
            'submitRoute' => $submitRoute,
100 2
            'routeName'   => $this->getRouteName(),
101
        ];
102
103 2
        return view('addedit', $viewData);
104
    }
105
106 2
    private function getFields()
107
    {
108
        return [
109
            [
110 2
                'id'    => 'number',
111 2
                'title' => trans('general.number'),
112 2
                'value' => function (Room $data) {
113 2
                    return $data->number;
114 2
                },
115
                'optional' => [
116
                    'required' => 'required',
117
                ],
118
            ],
119
            [
120 2
                'id'    => 'floor',
121 2
                'title' => trans('general.floor'),
122 2
                'value' => function (Room $data) {
123 2
                    return $data->floor;
124 2
                },
125 2
                'type'     => 'number',
126
                'optional' => [
127
                    'required' => 'required',
128
                ],
129
            ],
130
            [
131 2
                'id'    => 'capacity',
132 2
                'title' => trans('general.capacity'),
133 2
                'value' => function (Room $data) {
134 2
                    return $data->capacity;
135 2
                },
136 2
                'type'     => 'number',
137
                'optional' => [
138
                    'required' => 'required',
139
                ],
140
            ],
141
            [
142 2
                'id'    => 'price',
143 2
                'title' => trans('general.price'),
144 2
                'value' => function (Room $data) {
145 2
                    return $data->price;
146 2
                },
147 2
                'type'     => 'number',
148
                'optional' => [
149
                    'step'        => '0.01',
150
                    'placeholder' => '0.00',
151
                ],
152
            ],
153
            [
154 2
                'id'    => 'comment',
155 2
                'title' => trans('general.comment'),
156 2
                'value' => function (Room $data) {
157 2
                    return $data->comment;
158 2
                },
159 2
                'type' => 'textarea',
160
            ],
161
        ];
162
    }
163
164 4
    private function getColumns()
165
    {
166
        $dataset = [
167
            [
168 4
                'title' => trans('general.number'),
169 4
                'value' => function (Room $data) {
170 2
                    return $data->number;
171 4
                },
172
            ],
173
            [
174 4
                'title' => trans('general.floor'),
175 4
                'value' => function (Room $data) {
176 2
                    return $data->floor;
177 4
                },
178
            ],
179
            [
180 4
                'title' => trans('general.capacity'),
181 4
                'value' => function (Room $data) {
182 2
                    return $data->capacity;
183 4
                },
184
            ],
185
            [
186 4
                'title' => trans('general.price'),
187 4
                'value' => function (Room $data) {
188 2
                    return $data->price;
189 4
                },
190
            ],
191
            [
192 4
                'title' => trans('general.comment'),
193 4
                'value' => function (Room $data) {
194 2
                    return $data->comment;
195 4
                },
196
            ],
197
        ];
198
199 4
        return $dataset;
200
    }
201
}
202