Passed
Push — master ( 7cf260...fcb529 )
by Adam
05:25
created

GuestController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 92.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 182
ccs 87
cts 94
cp 0.9255
rs 10
c 1
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 21 2
A __construct() 0 3 1
A delete() 0 16 2
B showAddEditForm() 0 29 3
A getFields() 0 74 1
A store() 0 22 3
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 13
    public function __construct(GuestTableService $guestTableService)
16
    {
17 13
        $this->guestTableService = $guestTableService;
18 13
    }
19
20 9
    public function index()
21
    {
22 9
        $title = trans('general.guests');
23
24 9
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
25 9
            ->paginate($this->getItemsPerPage());
26
27 9
        if ($dataset->isEmpty()) {
28 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
29
        }
30
31
        $viewData = [
32 9
            'columns'       => $this->guestTableService->getColumns(),
33 9
            'dataset'       => $dataset,
34 9
            'routeName'     => $this->guestTableService->getRouteName(),
35 9
            'title'         => $title,
36
            // TODO
37 9
            '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 9
        return view('list', $viewData);
41
    }
42
43 6
    public function store(GuestRequest $request, $objectId = null)
44
    {
45 6
        if ($objectId === null) {
46 1
            $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 6
        $object->fill($request->all());
59 6
        $object->save();
60
61 6
        return redirect()->route($this->guestTableService->getRouteName().'.index')
62 6
            ->with([
63 6
                'message'     => trans('general.saved'),
64 6
                '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
75
            return response()->json($data);
76
        }
77
78 2
        $object->reservations()->delete();
79 2
        $object->delete();
80
81 2
        $data = ['class' => 'alert-success', 'message' => trans('general.deleted')];
82
83 2
        return response()->json($data);
84
    }
85
86
    // TODO
87 8
    public function showAddEditForm($objectId = null)
88
    {
89 8
        if ($objectId === null) {
90 2
            $dataset = new Guest();
91 2
            $title = trans('navigation.add_guest');
92 2
            $submitRoute = route($this->guestTableService->getRouteName().'.postadd');
93
        } else {
94
            try {
95 6
                $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($objectId);
96 1
            } catch (ModelNotFoundException $e) {
97 1
                return $this->returnBack([
98 1
                    'message'     => trans('general.object_not_found'),
99 1
                    'alert-class' => 'alert-danger',
100
                ]);
101
            }
102
103 5
            $title = trans('navigation.edit_guest');
104 5
            $submitRoute = route($this->guestTableService->getRouteName().'.postedit', $objectId);
105
        }
106
107
        $viewData = [
108 7
            'dataset'     => $dataset,
109 7
            'fields'      => $this->getFields(),
110 7
            'title'       => $title,
111 7
            'submitRoute' => $submitRoute,
112 7
            'routeName'   => $this->guestTableService->getRouteName(),
113
        ];
114
115 7
        return view('addedit', $viewData);
116
    }
117
118
    // TODO
119 7
    public function getFields()
120
    {
121
        return [
122
            [
123 7
                'id'    => 'first_name',
124 7
                'title' => trans('general.first_name'),
125 7
                'value' => function (Guest $data) {
126 7
                    return $data->first_name;
127 7
                },
128
                'optional' => [
129
                    'required' => 'required',
130
                ],
131
            ],
132
            [
133 7
                'id'    => 'last_name',
134 7
                'title' => trans('general.last_name'),
135 7
                'value' => function (Guest $data) {
136 7
                    return $data->last_name;
137 7
                },
138
                'optional' => [
139
                    'required' => 'required',
140
                ],
141
            ],
142
            [
143 7
                'id'    => 'address',
144 7
                'title' => trans('general.address'),
145 7
                'value' => function (Guest $data) {
146 7
                    return $data->address;
147 7
                },
148
                'optional' => [
149
                    'required' => 'required',
150
                ],
151
            ],
152
            [
153 7
                'id'    => 'zip_code',
154 7
                'title' => trans('general.zip_code'),
155 7
                'value' => function (Guest $data) {
156 7
                    return $data->zip_code;
157 7
                },
158
                'optional' => [
159
                    'required'    => 'required',
160
                    'placeholder' => '00-000',
161
                ],
162
            ],
163
            [
164 7
                'id'    => 'place',
165 7
                'title' => trans('general.place'),
166 7
                'value' => function (Guest $data) {
167 7
                    return $data->place;
168 7
                },
169
                'optional' => [
170
                    'required' => 'required',
171
                ],
172
            ],
173
            [
174 7
                'id'    => 'PESEL',
175 7
                'title' => trans('general.PESEL'),
176 7
                'value' => function (Guest $data) {
177 7
                    return $data->PESEL;
178 7
                },
179
                'optional' => [
180
                    'required'    => 'required',
181
                    'placeholder' => '12345654321',
182
                ],
183
            ],
184
            [
185 7
                'id'    => 'contact',
186 7
                'title' => trans('general.contact'),
187 7
                'value' => function (Guest $data) {
188 7
                    return $data->contact;
189 7
                },
190 7
                'type'     => 'textarea',
191
                'optional' => [
192 7
                    'placeholder' => trans('general.contact_placeholder'),
193
                ],
194
            ],
195
        ];
196
    }
197
}
198