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

GuardiansController::stash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\Guardian;
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\GuardiansDataTable;
17
use Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
20
class GuardiansController extends AuthorizedController
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $resource = Guardian::class;
26
27
    /**
28
     * List all guardians.
29
     *
30
     * @param \Cortex\Auth\DataTables\Adminarea\GuardiansDataTable $guardiansDataTable
31
     *
32
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
33
     */
34
    public function index(GuardiansDataTable $guardiansDataTable)
35
    {
36
        return $guardiansDataTable->with([
37
            'id' => 'adminarea-guardians-index-table',
38
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
39
    }
40
41
    /**
42
     * List guardian logs.
43
     *
44
     * @param \Cortex\Auth\Models\Guardian                $guardian
45
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
46
     *
47
     * @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...
48
     */
49
    public function logs(Guardian $guardian, LogsDataTable $logsDataTable)
50
    {
51
        return $logsDataTable->with([
52
            'resource' => $guardian,
53
            'tabs' => 'adminarea.guardians.tabs',
54
            'id' => "adminarea-guardians-{$guardian->getRouteKey()}-logs-table",
55
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
56
    }
57
58
    /**
59
     * Import guardians.
60
     *
61
     * @param \Cortex\Auth\Models\Guardian                         $guardian
62
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
63
     *
64
     * @return \Illuminate\View\View
65
     */
66
    public function import(Guardian $guardian, 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...
67
    {
68
        return $importRecordsDataTable->with([
69
            'resource' => $guardian,
70
            'tabs' => 'adminarea.guardians.tabs',
71
            'url' => route('adminarea.guardians.stash'),
72
            'id' => "adminarea-attributes-{$guardian->getRouteKey()}-import-table",
73
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
74
    }
75
76
    /**
77
     * Stash guardians.
78
     *
79
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
80
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
81
     *
82
     * @return void
83
     */
84
    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...
85
    {
86
        // Handle the import
87
        $importer->config['resource'] = $this->resource;
88
        $importer->config['name'] = 'username';
89
        $importer->handleImport();
90
    }
91
92
    /**
93
     * Hoard guardians.
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.guardian')->getFillable()))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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.guardian')->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 guardian 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.guardian'),
134
            'tabs' => 'adminarea.guardians.tabs',
135
            'id' => 'adminarea-guardians-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new guardian.
141
     *
142
     * @param \Illuminate\Http\Request     $request
143
     * @param \Cortex\Auth\Models\Guardian $guardian
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Guardian $guardian)
148
    {
149
        return $this->form($request, $guardian);
150
    }
151
152
    /**
153
     * Edit given guardian.
154
     *
155
     * @param \Illuminate\Http\Request     $request
156
     * @param \Cortex\Auth\Models\Guardian $guardian
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(Request $request, Guardian $guardian)
161
    {
162
        return $this->form($request, $guardian);
163
    }
164
165
    /**
166
     * Show guardian create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request     $request
169
     * @param \Cortex\Auth\Models\Guardian $guardian
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Guardian $guardian)
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...
174
    {
175
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
176
177
        return view('cortex/auth::adminarea.pages.guardian', compact('guardian', 'tags'));
178
    }
179
180
    /**
181
     * Store new guardian.
182
     *
183
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
184
     * @param \Cortex\Auth\Models\Guardian                             $guardian
185
     *
186
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
187
     */
188
    public function store(GuardianFormRequest $request, Guardian $guardian)
189
    {
190
        return $this->process($request, $guardian);
191
    }
192
193
    /**
194
     * Update given guardian.
195
     *
196
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
197
     * @param \Cortex\Auth\Models\Guardian                             $guardian
198
     *
199
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
200
     */
201
    public function update(GuardianFormRequest $request, Guardian $guardian)
202
    {
203
        return $this->process($request, $guardian);
204
    }
205
206
    /**
207
     * Process stored/updated guardian.
208
     *
209
     * @param \Illuminate\Foundation\Http\FormRequest $request
210
     * @param \Cortex\Auth\Models\Guardian            $guardian
211
     *
212
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
213
     */
214
    protected function process(FormRequest $request, Guardian $guardian)
215
    {
216
        // Prepare required input fields
217
        $data = $request->validated();
218
219
        // Save guardian
220
        $guardian->fill($data)->save();
221
222
        return intend([
223
            'url' => route('adminarea.guardians.index'),
224
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 181 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...
225
        ]);
226
    }
227
228
    /**
229
     * Destroy given guardian.
230
     *
231
     * @param \Cortex\Auth\Models\Guardian $guardian
232
     *
233
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
234
     */
235
    public function destroy(Guardian $guardian)
236
    {
237
        $guardian->delete();
238
239
        return intend([
240
            'url' => route('adminarea.guardians.index'),
241
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 183 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...
242
        ]);
243
    }
244
}
245