Completed
Push — master ( df9caa...85af72 )
by Adam
06:56
created

RoomController::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3.4098

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 24
loc 24
ccs 9
cts 14
cp 0.6429
rs 8.9713
cc 3
eloc 16
nc 3
nop 2
crap 3.4098
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 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.rooms');
19
20 4
        $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')
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.room')).' '.mb_strtolower(trans('general.number')),
29
        ];
30
31 4
        return view('list', $viewData);
32
    }
33
34 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...
35
    {
36 1
        if ($id === null) {
37
            $object = new Room();
38
        } else {
39
            try {
40 1
                $object = Room::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
        Room::destroy($id);
62 1
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
63
64 1
        return response()->json($data);
65
    }
66
67 3
    public function showAddEditForm($id = null)
68
    {
69 3
        if ($id === null) {
70 1
            $dataset = new Room();
71 1
            $title = trans('general.add');
72 1
            $submitRoute = route($this->getRouteName().'.postadd');
73
        } else {
74
            try {
75 2
                $dataset = Room::select('id', 'number', 'floor', 'capacity', 'price', 'comment')->findOrFail($id);
76 1
            } catch (ModelNotFoundException $e) {
77 1
                return Controller::returnBack([
78 1
                    'message'     => trans('general.object_not_found'),
79 1
                    'alert-class' => 'alert-danger',
80
                ]);
81
            }
82
83 1
            $title = trans('general.edit');
84 1
            $submitRoute = route($this->getRouteName().'.postedit', $id);
85
        }
86
87 2
        $title .= ' '.mb_strtolower(trans('general.room'));
88
89
        $viewData = [
90 2
            'dataset'     => $dataset,
91 2
            'fields'      => $this->getFields(),
92 2
            'title'       => $title,
93 2
            'submitRoute' => $submitRoute,
94 2
            'routeName'   => $this->getRouteName(),
95
        ];
96
97 2
        return view('addedit', $viewData);
98
    }
99
100 2
    private function getFields()
101
    {
102
        return [
103
            [
104 2
                'id'    => 'number',
105 2
                'title' => trans('general.number'),
106 2
                'value' => function (Room $data) {
107 2
                    return $data->number;
108 2
                },
109
                'optional' => [
110
                    'required' => 'required',
111
                ],
112
            ],
113
            [
114 2
                'id'    => 'floor',
115 2
                'title' => trans('general.floor'),
116 2
                'value' => function (Room $data) {
117 2
                    return $data->floor;
118 2
                },
119 2
                'type'     => 'number',
120
                'optional' => [
121
                    'required' => 'required',
122
                ],
123
            ],
124
            [
125 2
                'id'    => 'capacity',
126 2
                'title' => trans('general.capacity'),
127 2
                'value' => function (Room $data) {
128 2
                    return $data->capacity;
129 2
                },
130 2
                'type'     => 'number',
131
                'optional' => [
132
                    'required' => 'required',
133
                ],
134
            ],
135
            [
136 2
                'id'    => 'price',
137 2
                'title' => trans('general.price'),
138 2
                'value' => function (Room $data) {
139 2
                    return $data->price;
140 2
                },
141 2
                'type'     => 'number',
142
                'optional' => [
143
                    'step'        => '0.01',
144
                    'placeholder' => '0.00',
145
                ],
146
            ],
147
            [
148 2
                'id'    => 'comment',
149 2
                'title' => trans('general.comment'),
150 2
                'value' => function (Room $data) {
151 2
                    return $data->comment;
152 2
                },
153 2
                'type' => 'textarea',
154
            ],
155
        ];
156
    }
157
158 4
    private function getColumns()
159
    {
160
        $dataset = [
161
            [
162 4
                'title' => trans('general.number'),
163 4
                'value' => function (Room $data) {
164 2
                    return $data->number;
165 4
                },
166
            ],
167
            [
168 4
                'title' => trans('general.floor'),
169 4
                'value' => function (Room $data) {
170 2
                    return $data->floor;
171 4
                },
172
            ],
173
            [
174 4
                'title' => trans('general.capacity'),
175 4
                'value' => function (Room $data) {
176 2
                    return $data->capacity;
177 4
                },
178
            ],
179
            [
180 4
                'title' => trans('general.price'),
181 4
                'value' => function (Room $data) {
182 2
                    return $data->price;
183 4
                },
184
            ],
185
            [
186 4
                'title' => trans('general.comment'),
187 4
                'value' => function (Room $data) {
188 2
                    return $data->comment;
189 4
                },
190
            ],
191
        ];
192
193 4
        return $dataset;
194
    }
195
}
196