GuestController::store()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 3
rs 9.7666
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 17
    public function __construct(GuestTableService $guestTableService)
16
    {
17 17
        $this->guestTableService = $guestTableService;
18 17
    }
19
20 12
    public function index()
21
    {
22 12
        $title = trans('general.guests');
23
24 12
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
25 12
            ->paginate($this->getItemsPerPage());
26
27 12
        if ($dataset->isEmpty()) {
28 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
29
        }
30
31
        $viewData = [
32 12
            'columns'       => $this->guestTableService->getColumns(),
33 12
            'dataset'       => $dataset,
34 12
            'routeName'     => $this->guestTableService->getRouteName(),
35 12
            'title'         => $title,
36 12
            'deleteMessage' => trans('general.delete_associated_reservations'),
37
        ];
38
39 12
        return view('list', $viewData);
40
    }
41
42 8
    public function store(GuestRequest $request, $objectId = null)
43
    {
44 8
        if ($objectId === null) {
45 1
            $object = new Guest();
46
        } else {
47
            try {
48 7
                $object = Guest::findOrFail($objectId);
49 1
            } catch (ModelNotFoundException $e) {
50 1
                return $this->returnBack([
51 1
                    'message'     => trans('general.object_not_found'),
52 1
                    'alert-class' => 'alert-danger',
53
                ]);
54
            }
55
        }
56
57 7
        $object->fill($request->all());
58 7
        $object->save();
59
60 7
        return redirect()->route($this->guestTableService->getRouteName().'.index')
61 7
            ->with([
62 7
                'message'     => trans('general.saved'),
63 7
                'alert-class' => 'alert-success',
64
            ]);
65
    }
66
67 3
    public function delete($objectId)
68
    {
69
        try {
70 3
            $object = Guest::findOrFail($objectId);
71 1
        } catch (ModelNotFoundException $e) {
72 1
            $data = ['class' => 'alert-danger', 'message' => trans('general.object_not_found')];
73
74 1
            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
82 2
        return response()->json($data);
83
    }
84
85 9
    public function showAddEditForm($objectId = null)
86
    {
87 9
        if ($objectId === null) {
88 2
            $dataset = new Guest();
89 2
            $title = trans('navigation.add_guest');
90 2
            $submitRoute = route($this->guestTableService->getRouteName().'.postadd');
91
        } else {
92
            try {
93 7
                $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 6
            $title = trans('navigation.edit_guest');
102 6
            $submitRoute = route($this->guestTableService->getRouteName().'.postedit', $objectId);
103
        }
104
105
        $viewData = [
106 8
            'dataset'     => $dataset,
107 8
            'fields'      => $this->getFields(),
108 8
            'title'       => $title,
109 8
            'submitRoute' => $submitRoute,
110 8
            'routeName'   => $this->guestTableService->getRouteName(),
111
        ];
112
113 8
        return view('addedit', $viewData);
114
    }
115
116 8
    public function getFields()
117
    {
118
        return [
119
            [
120 8
                'id'    => 'first_name',
121 8
                'title' => trans('general.first_name'),
122
                'value' => function (Guest $data) {
123 8
                    return $data->first_name;
124 8
                },
125
                'optional' => [
126
                    'required' => 'required',
127
                ],
128
            ],
129
            [
130 8
                'id'    => 'last_name',
131 8
                'title' => trans('general.last_name'),
132
                'value' => function (Guest $data) {
133 8
                    return $data->last_name;
134 8
                },
135
                'optional' => [
136
                    'required' => 'required',
137
                ],
138
            ],
139
            [
140 8
                'id'    => 'address',
141 8
                'title' => trans('general.address'),
142
                'value' => function (Guest $data) {
143 8
                    return $data->address;
144 8
                },
145
                'optional' => [
146
                    'required' => 'required',
147
                ],
148
            ],
149
            [
150 8
                'id'    => 'zip_code',
151 8
                'title' => trans('general.zip_code'),
152
                'value' => function (Guest $data) {
153 8
                    return $data->zip_code;
154 8
                },
155
                'optional' => [
156
                    'required'    => 'required',
157
                    'placeholder' => '00-000',
158
                ],
159
            ],
160
            [
161 8
                'id'    => 'place',
162 8
                'title' => trans('general.place'),
163
                'value' => function (Guest $data) {
164 8
                    return $data->place;
165 8
                },
166
                'optional' => [
167
                    'required' => 'required',
168
                ],
169
            ],
170
            [
171 8
                'id'    => 'PESEL',
172 8
                'title' => trans('general.PESEL'),
173
                'value' => function (Guest $data) {
174 8
                    return $data->PESEL;
175 8
                },
176
                'optional' => [
177
                    'required'    => 'required',
178
                    'placeholder' => '12345654321',
179
                ],
180
            ],
181
            [
182 8
                'id'    => 'contact',
183 8
                'title' => trans('general.contact'),
184
                'value' => function (Guest $data) {
185 8
                    return $data->contact;
186 8
                },
187 8
                'type'     => 'textarea',
188
                'optional' => [
189 8
                    'placeholder' => trans('general.contact_placeholder'),
190
                ],
191
            ],
192
        ];
193
    }
194
}
195