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

AbilitiesController::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\Foundation\DataTables\ImportRecordsDataTable;
8
use Exception;
9
use Illuminate\Http\Request;
10
use Cortex\Auth\Models\Ability;
11
use Illuminate\Foundation\Http\FormRequest;
12
use Cortex\Foundation\DataTables\LogsDataTable;
13
use Cortex\Foundation\Importers\DefaultImporter;
14
use Cortex\Foundation\DataTables\ImportLogsDataTable;
15
use Cortex\Foundation\Http\Requests\ImportFormRequest;
16
use Cortex\Auth\DataTables\Adminarea\AbilitiesDataTable;
17
use Cortex\Auth\Http\Requests\Adminarea\AbilityFormRequest;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
use Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest;
20
21
class AbilitiesController extends AuthorizedController
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $resource = Ability::class;
27
28
    /**
29
     * List all abilities.
30
     *
31
     * @param \Cortex\Auth\DataTables\Adminarea\AbilitiesDataTable $abilitiesDataTable
32
     *
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
34
     */
35
    public function index(AbilitiesDataTable $abilitiesDataTable)
36
    {
37
        return $abilitiesDataTable->with([
38
            'id' => 'adminarea-abilities-index-table',
39
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
40
    }
41
42
    /**
43
     * List ability logs.
44
     *
45
     * @param \Cortex\Auth\Models\Ability                 $ability
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(Ability $ability, LogsDataTable $logsDataTable)
51
    {
52
        return $logsDataTable->with([
53
            'resource' => $ability,
54
            'tabs' => 'adminarea.abilities.tabs',
55
            'id' => "adminarea-abilities-{$ability->getRouteKey()}-logs-table",
56
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
57
    }
58
59
    /**
60
     * Import abilities.
61
     *
62
     * @param \Cortex\Auth\Models\Ability                          $ability
63
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
64
     *
65
     * @return \Illuminate\View\View
66
     */
67
    public function import(Ability $ability, 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' => $ability,
71
            'tabs' => 'adminarea.abilities.tabs',
72
            'url' => route('adminarea.abilities.stash'),
73
            'id' => "adminarea-abilities-{$ability->getRouteKey()}-import-table",
74
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
75
    }
76
77
    /**
78
     * Stash abilities.
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 abilities.
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.ability')->getFillable()))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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.ability')->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 ability 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.ability'),
134
            'tabs' => 'adminarea.abilities.tabs',
135
            'id' => 'adminarea-abilities-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new ability.
141
     *
142
     * @param \Illuminate\Http\Request $request
143
     * @param \Cortex\Auth\Models\Role $ability
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $ability a bit more specific; maybe use Ability.
Loading history...
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Ability $ability)
148
    {
149
        return $this->form($request, $ability);
150
    }
151
152
    /**
153
     * Edit given ability.
154
     *
155
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormRequest $request
156
     * @param \Cortex\Auth\Models\Role                                $ability
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $ability a bit more specific; maybe use Ability.
Loading history...
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(AbilityFormRequest $request, Ability $ability)
161
    {
162
        return $this->form($request, $ability);
163
    }
164
165
    /**
166
     * Show ability create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request    $request
169
     * @param \Cortex\Auth\Models\Ability $ability
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Ability $ability)
174
    {
175
        $roles = $request->user($this->getGuard())->can('superadmin')
176
            ? app('cortex.auth.role')->all()->pluck('name', 'id')->toArray()
177
            : $request->user($this->getGuard())->roles->pluck('name', 'id')->toArray();
178
179
        $entityTypes = app('cortex.auth.ability')->distinct()->get(['entity_type'])->pluck('entity_type', 'entity_type')->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
180
181
        asort($roles);
182
183
        return view('cortex/auth::adminarea.pages.ability', compact('ability', 'roles', 'entityTypes'));
184
    }
185
186
    /**
187
     * Store new ability.
188
     *
189
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest $request
190
     * @param \Cortex\Auth\Models\Ability                                    $ability
191
     *
192
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
193
     */
194
    public function store(AbilityFormProcessRequest $request, Ability $ability)
195
    {
196
        return $this->process($request, $ability);
197
    }
198
199
    /**
200
     * Update given ability.
201
     *
202
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest $request
203
     * @param \Cortex\Auth\Models\Ability                                    $ability
204
     *
205
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
206
     */
207
    public function update(AbilityFormProcessRequest $request, Ability $ability)
208
    {
209
        return $this->process($request, $ability);
210
    }
211
212
    /**
213
     * Process stored/updated ability.
214
     *
215
     * @param \Illuminate\Foundation\Http\FormRequest $request
216
     * @param \Cortex\Auth\Models\Ability             $ability
217
     *
218
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
219
     */
220
    protected function process(FormRequest $request, Ability $ability)
221
    {
222
        // Prepare required input fields
223
        $data = $request->validated();
224
225
        // Save ability
226
        $ability->fill($data)->save();
227
228
        return intend([
229
            'url' => route('adminarea.abilities.index'),
230
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.ability'), 'identifier' => $ability->title])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 176 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...
231
        ]);
232
    }
233
234
    /**
235
     * Destroy given ability.
236
     *
237
     * @param \Cortex\Auth\Models\Ability $ability
238
     *
239
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
240
     */
241
    public function destroy(Ability $ability)
242
    {
243
        $ability->delete();
244
245
        return intend([
246
            'url' => route('adminarea.abilities.index'),
247
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.ability'), 'identifier' => $ability->title])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 178 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...
248
        ]);
249
    }
250
}
251