Passed
Branch master (065bfc)
by Adam
05:23
created

RoomController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 5
    private function getRouteName()
12
    {
13 5
        return 'room';
14
    }
15
16 4
    public function index()
17
    {
18 4
        $title = trans('general.rooms');
19
20 4
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
21 4
            ->paginate($this->getItemsPerPage());
22
23 4
        if ($dataset->isEmpty()) {
24 2
            $this->addFlashMessage(trans('general.no_rooms_in_database'), 'alert-danger');
25
        }
26
27
        $viewData = [
28 4
            'columns'       => $this->getColumns(),
29 4
            'dataset'       => $dataset,
30 4
            'routeName'     => $this->getRouteName(),
31 4
            'title'         => $title,
32 4
            'deleteMessage' => mb_strtolower(trans('general.room')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.room') can also be of type array; however, parameter $str of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.room')).' '.mb_strtolower(trans('general.number')),
Loading history...
33
        ];
34
35 4
        return view('list', $viewData);
36
    }
37
38 1
    public function store(RoomRequest $request, $id = null)
39
    {
40 1
        if ($id === null) {
41
            $object = new Room();
42
        } else {
43
            try {
44 1
                $object = Room::findOrFail($id);
45
            } catch (ModelNotFoundException $e) {
46
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
47
                    'message'     => trans('general.object_not_found'),
48
                    'alert-class' => 'alert-danger',
49
                ]);
50
            }
51
        }
52
53 1
        $object->fill($request->all());
54 1
        $object->save();
55
56 1
        return redirect()->route($this->getRouteName().'.index')
57 1
            ->with([
58 1
                'message'     => trans('general.saved'),
59 1
                'alert-class' => 'alert-success',
60
            ]);
61
    }
62
63 1
    public function delete($id)
64
    {
65 1
        Room::destroy($id);
66 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
67
68 1
        return response()->json($data);
69
    }
70
71 3
    public function showAddEditForm($id = null)
72
    {
73 3
        if ($id === null) {
74 1
            $dataset = new Room();
75 1
            $title = trans('general.add');
76 1
            $submitRoute = route($this->getRouteName().'.postadd');
77
        } else {
78
            try {
79 2
                $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')->findOrFail($id);
80 1
            } catch (ModelNotFoundException $e) {
81 1
                return Controller::returnBack([
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Controller::returnBack() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                return Controller::/** @scrutinizer ignore-call */ returnBack([
Loading history...
82 1
                    'message'     => trans('general.object_not_found'),
83 1
                    'alert-class' => 'alert-danger',
84
                ]);
85
            }
86
87 1
            $title = trans('general.edit');
88 1
            $submitRoute = route($this->getRouteName().'.postedit', $id);
89
        }
90
91 2
        $title .= ' '.mb_strtolower(trans('general.room'));
0 ignored issues
show
Bug introduced by
It seems like trans('general.room') can also be of type array; however, parameter $str of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $title .= ' '.mb_strtolower(/** @scrutinizer ignore-type */ trans('general.room'));
Loading history...
92
93
        $viewData = [
94 2
            'dataset'     => $dataset,
95 2
            'fields'      => $this->getFields(),
96 2
            'title'       => $title,
97 2
            'submitRoute' => $submitRoute,
98 2
            'routeName'   => $this->getRouteName(),
99
        ];
100
101 2
        return view('addedit', $viewData);
102
    }
103
104 2
    private function getFields()
105
    {
106
        return [
107
            [
108 2
                'id'    => 'number',
109 2
                'title' => trans('general.number'),
110 2
                'value' => function (Room $data) {
111 2
                    return $data->number;
112 2
                },
113
                'optional' => [
114
                    'required' => 'required',
115
                ],
116
            ],
117
            [
118 2
                'id'    => 'floor',
119 2
                'title' => trans('general.floor'),
120 2
                'value' => function (Room $data) {
121 2
                    return $data->floor;
122 2
                },
123 2
                'type'     => 'number',
124
                'optional' => [
125
                    'required' => 'required',
126
                ],
127
            ],
128
            [
129 2
                'id'    => 'capacity',
130 2
                'title' => trans('general.capacity'),
131 2
                'value' => function (Room $data) {
132 2
                    return $data->capacity;
133 2
                },
134 2
                'type'     => 'number',
135
                'optional' => [
136
                    'required' => 'required',
137
                ],
138
            ],
139
            [
140 2
                'id'    => 'price',
141 2
                'title' => trans('general.price'),
142 2
                'value' => function (Room $data) {
143 2
                    return $data->price;
144 2
                },
145 2
                'type'     => 'number',
146
                'optional' => [
147
                    'step'        => '0.01',
148
                    'placeholder' => '0.00',
149
                ],
150
            ],
151
            [
152 2
                'id'    => 'comment',
153 2
                'title' => trans('general.comment'),
154 2
                'value' => function (Room $data) {
155 2
                    return $data->comment;
156 2
                },
157 2
                'type' => 'textarea',
158
            ],
159
        ];
160
    }
161
162 4
    private function getColumns()
163
    {
164
        $dataset = [
165
            [
166 4
                'title' => trans('general.number'),
167 4
                'value' => function (Room $data) {
168 2
                    return $data->number;
169 4
                },
170
            ],
171
            [
172 4
                'title' => trans('general.floor'),
173 4
                'value' => function (Room $data) {
174 2
                    return $data->floor;
175 4
                },
176
            ],
177
            [
178 4
                'title' => trans('general.capacity'),
179 4
                'value' => function (Room $data) {
180 2
                    return $data->capacity;
181 4
                },
182
            ],
183
            [
184 4
                'title' => trans('general.price'),
185 4
                'value' => function (Room $data) {
186 2
                    return $data->price;
187 4
                },
188
            ],
189
            [
190 4
                'title' => trans('general.comment'),
191 4
                'value' => function (Room $data) {
192 2
                    return $data->comment;
193 4
                },
194
            ],
195
        ];
196
197 4
        return $dataset;
198
    }
199
}
200