Passed
Push — master ( 18c079...3a05ee )
by Adam
12:39 queued 05:51
created

GuestController::store()   A

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.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\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 16
    public function __construct(GuestTableService $guestTableService)
16
    {
17 16
        $this->guestTableService = $guestTableService;
18 16
    }
19
20 11
    public function index()
21
    {
22 11
        $title = trans('general.guests');
23
24 11
        $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')
25 11
            ->paginate($this->getItemsPerPage());
26
27 11
        if ($dataset->isEmpty()) {
28 2
            $this->addFlashMessage(trans('general.no_guests_in_database'), 'alert-danger');
29
        }
30
31
        $viewData = [
32 11
            'columns'       => $this->guestTableService->getColumns(),
33 11
            'dataset'       => $dataset,
34 11
            'routeName'     => $this->guestTableService->getRouteName(),
35 11
            'title'         => $title,
36 11
            'deleteMessage' => trans('general.delete_associated_reservations'),
37
        ];
38
39 11
        return view('list', $viewData);
40
    }
41
42 7
    public function store(GuestRequest $request, $objectId = null)
43
    {
44 7
        if ($objectId === null) {
45 1
            $object = new Guest();
46
        } else {
47
            try {
48 6
                $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 6
        $object->fill($request->all());
58 6
        $object->save();
59
60 6
        return redirect()->route($this->guestTableService->getRouteName().'.index')
61 6
            ->with([
62 6
                'message'     => trans('general.saved'),
63 6
                '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 8
    public function showAddEditForm($objectId = null)
86
    {
87 8
        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 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 7
            'dataset'     => $dataset,
107 7
            'fields'      => $this->getFields(),
108 7
            'title'       => $title,
109 7
            'submitRoute' => $submitRoute,
110 7
            'routeName'   => $this->guestTableService->getRouteName(),
111
        ];
112
113 7
        return view('addedit', $viewData);
114
    }
115
116 7
    public function getFields()
117
    {
118
        return [
119
            [
120 7
                'id'    => 'first_name',
121 7
                'title' => trans('general.first_name'),
122 7
                'value' => function (Guest $data) {
123 7
                    return $data->first_name;
124 7
                },
125
                'optional' => [
126
                    'required' => 'required',
127
                ],
128
            ],
129
            [
130 7
                'id'    => 'last_name',
131 7
                'title' => trans('general.last_name'),
132 7
                'value' => function (Guest $data) {
133 7
                    return $data->last_name;
134 7
                },
135
                'optional' => [
136
                    'required' => 'required',
137
                ],
138
            ],
139
            [
140 7
                'id'    => 'address',
141 7
                'title' => trans('general.address'),
142 7
                'value' => function (Guest $data) {
143 7
                    return $data->address;
144 7
                },
145
                'optional' => [
146
                    'required' => 'required',
147
                ],
148
            ],
149
            [
150 7
                'id'    => 'zip_code',
151 7
                'title' => trans('general.zip_code'),
152 7
                'value' => function (Guest $data) {
153 7
                    return $data->zip_code;
154 7
                },
155
                'optional' => [
156
                    'required'    => 'required',
157
                    'placeholder' => '00-000',
158
                ],
159
            ],
160
            [
161 7
                'id'    => 'place',
162 7
                'title' => trans('general.place'),
163 7
                'value' => function (Guest $data) {
164 7
                    return $data->place;
165 7
                },
166
                'optional' => [
167
                    'required' => 'required',
168
                ],
169
            ],
170
            [
171 7
                'id'    => 'PESEL',
172 7
                'title' => trans('general.PESEL'),
173 7
                'value' => function (Guest $data) {
174 7
                    return $data->PESEL;
175 7
                },
176
                'optional' => [
177
                    'required'    => 'required',
178
                    'placeholder' => '12345654321',
179
                ],
180
            ],
181
            [
182 7
                'id'    => 'contact',
183 7
                'title' => trans('general.contact'),
184 7
                'value' => function (Guest $data) {
185 7
                    return $data->contact;
186 7
                },
187 7
                'type'     => 'textarea',
188
                'optional' => [
189 7
                    'placeholder' => trans('general.contact_placeholder'),
190
                ],
191
            ],
192
        ];
193
    }
194
}
195