Completed
Push — develop ( 85bc70...7167f7 )
by Abdelrahman
02:34
created

RolesController::stash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Cortex\Auth\Models\Role;
8
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
9
use Exception;
10
use Illuminate\Http\Request;
11
use Illuminate\Foundation\Http\FormRequest;
12
use Cortex\Foundation\DataTables\LogsDataTable;
13
use Cortex\Foundation\Importers\DefaultImporter;
14
use Cortex\Auth\DataTables\Adminarea\RolesDataTable;
15
use Cortex\Foundation\DataTables\ImportLogsDataTable;
16
use Cortex\Foundation\Http\Requests\ImportFormRequest;
17
use Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
use Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest;
20
21
class RolesController extends AuthorizedController
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $resource = Role::class;
27
28
    /**
29
     * List all roles.
30
     *
31
     * @param \Cortex\Auth\DataTables\Adminarea\RolesDataTable $rolesDataTable
32
     *
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
34
     */
35
    public function index(RolesDataTable $rolesDataTable)
36
    {
37
        return $rolesDataTable->with([
38
            'id' => 'adminarea-roles-index-table',
39
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
40
    }
41
42
    /**
43
     * List role logs.
44
     *
45
     * @param \Cortex\Auth\Models\Role                    $role
46
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
47
     *
48
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\JsonRes...e|\Illuminate\View\View?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
49
     */
50
    public function logs(Role $role, LogsDataTable $logsDataTable)
51
    {
52
        return $logsDataTable->with([
53
            'resource' => $role,
54
            'tabs' => 'adminarea.roles.tabs',
55
            'id' => "adminarea-roles-{$role->getRouteKey()}-logs-table",
56
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
57
    }
58
59
    /**
60
     * Import roles.
61
     *
62
     * @param \Cortex\Auth\Models\Role                             $role
63
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
64
     *
65
     * @return \Illuminate\View\View
66
     */
67
    public function import(Role $role, ImportRecordsDataTable $importRecordsDataTable)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $importRecordsDataTable exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
68
    {
69
        return $importRecordsDataTable->with([
70
            'resource' => $role,
71
            'tabs' => 'adminarea.roles.tabs',
72
            'url' => route('adminarea.roles.stash'),
73
            'id' => "adminarea-roles-{$role->getRouteKey()}-import-table",
74
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
75
    }
76
77
    /**
78
     * Stash roles.
79
     *
80
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
81
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
82
     *
83
     * @return void
84
     */
85
    public function stash(ImportFormRequest $request, DefaultImporter $importer)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        // Handle the import
88
        $importer->config['resource'] = $this->resource;
89
        $importer->handleImport();
90
    }
91
92
    /**
93
     * Hoard roles.
94
     *
95
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
96
     *
97
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
98
     */
99
    public function hoard(ImportFormRequest $request)
100
    {
101
        foreach ((array) $request->get('selected_ids') as $recordId) {
102
            $record = app('cortex.foundation.import_record')->find($recordId);
103
104
            try {
105
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.auth.role')->getFillable()))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
106
107
                tap(app('rinvex.auth.role')->firstOrNew($fillable), function ($instance) use ($record) {
108
                    $instance->save() && $record->delete();
109
                });
110
            } catch (Exception $exception) {
111
                $record->notes = $exception->getMessage().(method_exists($exception, 'getMessageBag') ? "\n".json_encode($exception->getMessageBag())."\n\n" : '');
0 ignored issues
show
Bug introduced by
The method getMessageBag() does not exist on Exception. Did you maybe mean getMessage()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
112
                $record->status = 'fail';
113
                $record->save();
114
            }
115
        }
116
117
        return intend([
118
            'back' => true,
119
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
120
        ]);
121
    }
122
123
    /**
124
     * List role import logs.
125
     *
126
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
127
     *
128
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
129
     */
130
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
131
    {
132
        return $importLogsDatatable->with([
133
            'resource' => trans('cortex/auth::common.role'),
134
            'tabs' => 'adminarea.roles.tabs',
135
            'id' => 'adminarea-roles-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new role.
141
     *
142
     * @param \Illuminate\Http\Request $request
143
     * @param \Cortex\Auth\Models\Role $role
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Role $role)
148
    {
149
        return $this->form($request, $role);
150
    }
151
152
    /**
153
     * Edit given role.
154
     *
155
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest $request
156
     * @param \Cortex\Auth\Models\Role                             $role
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(RoleFormRequest $request, Role $role)
161
    {
162
        return $this->form($request, $role);
163
    }
164
165
    /**
166
     * Show role create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request $request
169
     * @param \Cortex\Auth\Models\Role $role
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Role $role)
174
    {
175
        $abilities = $request->user($this->getGuard())->can('superadmin')
176
            ? app('cortex.auth.ability')->all()->groupBy('entity_type')->map->pluck('title', 'id')->toArray()
177
            : $request->user($this->getGuard())->getAbilities()->groupBy('entity_type')->map->pluck('title', 'id')->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
178
179
        ksort($abilities);
180
181
        return view('cortex/auth::adminarea.pages.role', compact('role', 'abilities'));
182
    }
183
184
    /**
185
     * Store new role.
186
     *
187
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
188
     * @param \Cortex\Auth\Models\Role                                    $role
189
     *
190
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
191
     */
192
    public function store(RoleFormProcessRequest $request, Role $role)
193
    {
194
        return $this->process($request, $role);
195
    }
196
197
    /**
198
     * Update given role.
199
     *
200
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
201
     * @param \Cortex\Auth\Models\Role                                    $role
202
     *
203
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
204
     */
205
    public function update(RoleFormProcessRequest $request, Role $role)
206
    {
207
        return $this->process($request, $role);
208
    }
209
210
    /**
211
     * Process stored/updated role.
212
     *
213
     * @param \Illuminate\Foundation\Http\FormRequest $request
214
     * @param \Cortex\Auth\Models\Role                $role
215
     *
216
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
217
     */
218
    protected function process(FormRequest $request, Role $role)
219
    {
220
        // Prepare required input fields
221
        $data = $request->validated();
222
223
        // Save role
224
        $role->fill($data)->save();
225
226
        return intend([
227
            'url' => route('adminarea.roles.index'),
228
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.role'), 'identifier' => $role->title])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 170 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
229
        ]);
230
    }
231
232
    /**
233
     * Destroy given role.
234
     *
235
     * @param \Cortex\Auth\Models\Role $role
236
     *
237
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
238
     */
239
    public function destroy(Role $role)
240
    {
241
        $role->delete();
242
243
        return intend([
244
            'url' => route('adminarea.roles.index'),
245
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.role'), 'identifier' => $role->title])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 172 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
246
        ]);
247
    }
248
}
249