RoomController::getFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 54
ccs 24
cts 24
cp 1
crap 1
rs 9.376
c 0
b 0
f 0

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