Passed
Push — master ( ea0194...0a4772 )
by Adam
05:23
created

GuestController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
ccs 6
cts 9
cp 0.6667
crap 2.1481
rs 9.4285
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\Requests\GuestRequest;
7
use App\Models\Guest;
8
use App\Services\GuestTableService;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
11
class GuestController extends Controller implements ManageTableInterface
12
{
13
    protected $guestTableService;
14
15 12
    public function __construct(GuestTableService $guestTableService)
16
    {
17 12
        $this->guestTableService = $guestTableService;
18 12
    }
19
20 8
    public function index()
21
    {
22 8
        $title = trans('general.guests');
23
24 8
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
25 8
            ->paginate($this->getItemsPerPage());
26
27 8
        if ($dataset->isEmpty()) {
28 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
29
        }
30
31
        $viewData = [
32 8
            'columns'       => $this->guestTableService->getColumns(),
33 8
            'dataset'       => $dataset,
34 8
            'routeName'     => $this->guestTableService->getRouteName(),
35 8
            'title'         => $title,
36
            // TODO
37 8
            'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')),
0 ignored issues
show
Bug introduced by
It seems like trans('general.guest') 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

37
            'deleteMessage' => mb_strtolower(/** @scrutinizer ignore-type */ trans('general.guest')).' '.mb_strtolower(trans('general.number')),
Loading history...
38
        ];
39
40 8
        return view('list', $viewData);
41
    }
42
43 5
    public function store(GuestRequest $request, $objectId = null)
44
    {
45 5
        if ($objectId === null) {
46
            $object = new Guest();
47
        } else {
48
            try {
49 5
                $object = Guest::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 5
        $object->fill($request->all());
59 5
        $object->save();
60
61 5
        return redirect()->route($this->guestTableService->getRouteName().'.index')
62 5
            ->with([
63 5
                'message'     => trans('general.saved'),
64 5
                'alert-class' => 'alert-success',
65
            ]);
66
    }
67
68 2
    public function delete($objectId)
69
    {
70
        try {
71 2
            $object = Guest::findOrFail($objectId);
72
        } catch (ModelNotFoundException $e) {
73
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
74
            return response()->json($data);
75
        }
76
77 2
        $object->reservations()->delete();
78 2
        $object->delete();
79
80 2
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
81 2
        return response()->json($data);
82
    }
83
84
    // TODO
85 7
    public function showAddEditForm($objectId = null)
86
    {
87 7
        if ($objectId === null) {
88 1
            $dataset = new Guest();
89 1
            $title = trans('navigation.add_guest');
90 1
            $submitRoute = route($this->guestTableService->getRouteName().'.postadd');
91
        } else {
92
            try {
93 6
                $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($objectId);
94 1
            } catch (ModelNotFoundException $e) {
95 1
                return $this->returnBack([
96 1
                    'message'     => trans('general.object_not_found'),
97 1
                    'alert-class' => 'alert-danger',
98
                ]);
99
            }
100
101 5
            $title = trans('navigation.edit_guest');
102 5
            $submitRoute = route($this->guestTableService->getRouteName().'.postedit', $objectId);
103
        }
104
105
        $viewData = [
106 6
            'dataset'     => $dataset,
107 6
            'fields'      => $this->getFields(),
108 6
            'title'       => $title,
109 6
            'submitRoute' => $submitRoute,
110 6
            'routeName'   => $this->guestTableService->getRouteName(),
111
        ];
112
113 6
        return view('addedit', $viewData);
114
    }
115
116
    // TODO
117 6
    public function getFields()
118
    {
119
        return [
120
            [
121 6
                'id'    => 'first_name',
122 6
                'title' => trans('general.first_name'),
123 6
                'value' => function (Guest $data) {
124 6
                    return $data->first_name;
125 6
                },
126
                'optional' => [
127
                    'required' => 'required',
128
                ],
129
            ],
130
            [
131 6
                'id'    => 'last_name',
132 6
                'title' => trans('general.last_name'),
133 6
                'value' => function (Guest $data) {
134 6
                    return $data->last_name;
135 6
                },
136
                'optional' => [
137
                    'required' => 'required',
138
                ],
139
            ],
140
            [
141 6
                'id'    => 'address',
142 6
                'title' => trans('general.address'),
143 6
                'value' => function (Guest $data) {
144 6
                    return $data->address;
145 6
                },
146
                'optional' => [
147
                    'required' => 'required',
148
                ],
149
            ],
150
            [
151 6
                'id'    => 'zip_code',
152 6
                'title' => trans('general.zip_code'),
153 6
                'value' => function (Guest $data) {
154 6
                    return $data->zip_code;
155 6
                },
156
                'optional' => [
157
                    'required'    => 'required',
158
                    'placeholder' => '00-000',
159
                ],
160
            ],
161
            [
162 6
                'id'    => 'place',
163 6
                'title' => trans('general.place'),
164 6
                'value' => function (Guest $data) {
165 6
                    return $data->place;
166 6
                },
167
                'optional' => [
168
                    'required' => 'required',
169
                ],
170
            ],
171
            [
172 6
                'id'    => 'PESEL',
173 6
                'title' => trans('general.PESEL'),
174 6
                'value' => function (Guest $data) {
175 6
                    return $data->PESEL;
176 6
                },
177
                'optional' => [
178
                    'required'    => 'required',
179
                    'placeholder' => '12345654321',
180
                ],
181
            ],
182
            [
183 6
                'id'    => 'contact',
184 6
                'title' => trans('general.contact'),
185 6
                'value' => function (Guest $data) {
186 6
                    return $data->contact;
187 6
                },
188 6
                'type'     => 'textarea',
189
                'optional' => [
190 6
                    'placeholder' => trans('general.contact_placeholder'),
191
                ],
192
            ],
193
        ];
194
    }
195
}
196