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

ReservationController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 12
rs 9.2
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
// TODO
8
use App\Http\Requests\GuestRequest;
9
use App\Models\Guest;
10
use App\Models\Reservation;
11
use App\Services\GuestTableService;
12
use App\Services\ReservationTableService;
13
use Illuminate\Database\Eloquent\ModelNotFoundException;
14
15
class ReservationController extends Controller implements ManageTableInterface
16
{
17
    protected $reservationTableService;
18
19 2
    public function __construct(ReservationTableService $reservationTableService)
20
    {
21 2
        $this->reservationTableService = $reservationTableService;
22 2
    }
23
24 2
    public function index()
25
    {
26 2
        $title = trans('general.reservations');
27
28 2
        $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
29 2
            ->with('guest:id,first_name,last_name')
30 2
            ->with('room:id,number')
31 2
            ->paginate($this->getItemsPerPage());
32
33 2
        if ($dataset->isEmpty()) {
34 2
            $this->addFlashMessage(trans('general.no_reservations_in_database'), 'alert-danger');
35
        }
36
37
        $viewData = [
38 2
            'columns'       => $this->reservationTableService->getColumns(),
39 2
            'dataset'       => $dataset,
40 2
            'routeName'     => $this->reservationTableService->getRouteName(),
41 2
            'title'         => $title,
42
            // TODO
43 2
            'deleteMessage' => mb_strtolower(trans('general.reservation')).' '.mb_strtolower(trans('general.number')),
44
        ];
45
46 2
        return view('list', $viewData);
47
    }
48
49
    public function chooseGuest(GuestTableService $guestTableService)
50
    {
51
        $title = trans('general.choose_guest');
52
53
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
54
            ->paginate($this->getItemsPerPage());
55
56
        if ($dataset->isEmpty()) {
57
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
58
        }
59
60
        $viewData = [
61
            'columns'         => $guestTableService->getColumns(),
62
            'dataset'         => $dataset,
63
            'routeName'       => $guestTableService->getRouteName(),
64
            'title'           => $title,
65
            'routeChooseName' => $this->reservationTableService->getRouteName().'.search',
66
        ];
67
68
        return view('list', $viewData);
69
    }
70
71
    // TODO
72
    public function store(GuestRequest $request, $objectId = null)
73
    {
74
        if ($objectId === null) {
75
            $object = new Reservation();
76
        } else {
77
            try {
78
                $object = Reservation::findOrFail($objectId);
79
            } catch (ModelNotFoundException $e) {
80
                return $this->returnBack([
81
                    'message'     => trans('general.object_not_found'),
82
                    'alert-class' => 'alert-danger',
83
                ]);
84
            }
85
        }
86
87
        $object->fill($request->all());
88
        $object->save();
89
90
        return redirect()->route($this->reservationTableService->getRouteName().'.index')
91
            ->with([
92
                'message'     => trans('general.saved'),
93
                'alert-class' => 'alert-success',
94
            ]);
95
    }
96
97
    public function delete($objectId)
98
    {
99
        Reservation::destroy($objectId);
100
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
101
102
        return response()->json($data);
103
    }
104
105
    // TODO
106 1
    public function showAddEditForm($objectId = null)
107
    {
108 1
        if ($objectId === null) {
109
            $dataset = new Reservation();
110
            $title = trans('navigation.add_reservation');
111
            $submitRoute = route($this->reservationTableService->getRouteName().'.postadd');
112
        } else {
113
            try {
114 1
                $dataset = Reservation::select('id', 'room_id', 'guest_id', 'date_start', 'date_end', 'people')
115 1
                ->with('guest:id,first_name,last_name')
116 1
                ->with('room:id,number')
117 1
                ->findOrFail($objectId);
118 1
            } catch (ModelNotFoundException $e) {
119 1
                return $this->returnBack([
120 1
                    'message'     => trans('general.object_not_found'),
121 1
                    'alert-class' => 'alert-danger',
122
                ]);
123
            }
124
125
            $title = trans('navigation.edit_reservation');
126
            $submitRoute = route($this->reservationTableService->getRouteName().'.postedit', $objectId);
127
        }
128
129
        $viewData = [
130
            'dataset'     => $dataset,
131
            'fields'      => $this->getFields(),
132
            'title'       => $title,
133
            'submitRoute' => $submitRoute,
134
            'routeName'   => $this->reservationTableService->getRouteName(),
135
        ];
136
137
        return view('addedit', $viewData);
138
    }
139
140
    // TODO
141
    public function getFields()
142
    {
143
        return [
144
            [
145
                'id'    => 'first_name',
146
                'title' => trans('general.first_name'),
147
                'value' => function (Reservation $data) {
148
                    return $data->first_name;
0 ignored issues
show
Bug introduced by
The property first_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
149
                },
150
                'optional' => [
151
                    'required' => 'required',
152
                ],
153
            ],
154
            [
155
                'id'    => 'last_name',
156
                'title' => trans('general.last_name'),
157
                'value' => function (Reservation $data) {
158
                    return $data->last_name;
0 ignored issues
show
Bug introduced by
The property last_name does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
159
                },
160
                'optional' => [
161
                    'required' => 'required',
162
                ],
163
            ],
164
            [
165
                'id'    => 'address',
166
                'title' => trans('general.address'),
167
                'value' => function (Reservation $data) {
168
                    return $data->address;
0 ignored issues
show
Bug introduced by
The property address does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
169
                },
170
                'optional' => [
171
                    'required' => 'required',
172
                ],
173
            ],
174
            [
175
                'id'    => 'zip_code',
176
                'title' => trans('general.zip_code'),
177
                'value' => function (Reservation $data) {
178
                    return $data->zip_code;
0 ignored issues
show
Bug introduced by
The property zip_code does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
179
                },
180
                'optional' => [
181
                    'required'    => 'required',
182
                    'placeholder' => '00-000',
183
                ],
184
            ],
185
            [
186
                'id'    => 'place',
187
                'title' => trans('general.place'),
188
                'value' => function (Reservation $data) {
189
                    return $data->place;
0 ignored issues
show
Bug introduced by
The property place does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
190
                },
191
                'optional' => [
192
                    'required' => 'required',
193
                ],
194
            ],
195
            [
196
                'id'    => 'PESEL',
197
                'title' => trans('general.PESEL'),
198
                'value' => function (Reservation $data) {
199
                    return $data->PESEL;
0 ignored issues
show
Bug introduced by
The property PESEL does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
200
                },
201
                'optional' => [
202
                    'required'    => 'required',
203
                    'placeholder' => '12345654321',
204
                ],
205
            ],
206
            [
207
                'id'    => 'contact',
208
                'title' => trans('general.contact'),
209
                'value' => function (Reservation $data) {
210
                    return $data->contact;
0 ignored issues
show
Bug introduced by
The property contact does not seem to exist on App\Models\Reservation. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
211
                },
212
                'type'     => 'textarea',
213
                'optional' => [
214
                    'placeholder' => trans('general.contact_placeholder'),
215
                ],
216
            ],
217
        ];
218
    }
219
}
220