Completed
Push — master ( 7aba56...94581c )
by Adam
02:47
created

RoomController::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 59
ccs 0
cts 42
cp 0
rs 9.597
cc 1
eloc 37
nc 1
nop 0
crap 2

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