Completed
Push — master ( 793984...065bfc )
by Adam
03:06
created

RoomController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 191
Duplicated Lines 27.23 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.15%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
c 1
b 1
f 0
lcom 1
cbo 6
dl 52
loc 191
ccs 98
cts 103
cp 0.9515
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteName() 0 4 1
A index() 21 21 2
B store() 24 24 3
A delete() 7 7 1
B showAddEditForm() 0 32 3
A getFields() 0 57 1
B getColumns() 0 37 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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')),
33
        ];
34
35 4
        return view('list', $viewData);
36
    }
37
38 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...
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([
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 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...
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([
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'));
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