Completed
Push — master ( fa257f...84e799 )
by Adam
05:01
created

RoomController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Interfaces\ManageTableInterface;
6
use App\Http\Interfaces\TableInterface;
0 ignored issues
show
Bug introduced by
The type App\Http\Interfaces\TableInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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