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

GuestController::getColumns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

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