ManagersController::importLogs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Cortex\Auth\Models\Manager;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Foundation\Importers\DefaultImporter;
13
use Cortex\Foundation\DataTables\ActivitiesDataTable;
14
use Cortex\Foundation\DataTables\ImportLogsDataTable;
15
use Cortex\Foundation\Http\Requests\ImportFormRequest;
16
use Cortex\Auth\DataTables\Adminarea\ManagersDataTable;
17
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
18
use Cortex\Auth\Http\Requests\Adminarea\ManagerFormRequest;
19
use Cortex\Foundation\Http\Controllers\AuthorizedController;
20
use Cortex\Auth\Http\Requests\Adminarea\ManagerAttributesFormRequest;
21
22
class ManagersController extends AuthorizedController
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $resource = Manager::class;
28
29
    /**
30
     * List all managers.
31
     *
32
     * @param \Cortex\Auth\DataTables\Adminarea\ManagersDataTable $managersDataTable
33
     *
34
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
35
     */
36
    public function index(ManagersDataTable $managersDataTable)
37
    {
38
        return $managersDataTable->with([
39
            'id' => 'adminarea-managers-index-table',
40
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
41
    }
42
43
    /**
44
     * List manager logs.
45
     *
46
     * @param \Cortex\Auth\Models\Manager                 $manager
47
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
48
     *
49
     * @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...
50
     */
51
    public function logs(Manager $manager, LogsDataTable $logsDataTable)
52
    {
53
        return $logsDataTable->with([
54
            'resource' => $manager,
55
            'tabs' => 'adminarea.managers.tabs',
56
            'id' => "adminarea-managers-{$manager->getRouteKey()}-logs-table",
57
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
58
    }
59
60
    /**
61
     * Get a listing of the resource activities.
62
     *
63
     * @param \Cortex\Auth\Models\Manager                       $manager
64
     * @param \Cortex\Foundation\DataTables\ActivitiesDataTable $activitiesDataTable
65
     *
66
     * @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...
67
     */
68
    public function activities(Manager $manager, ActivitiesDataTable $activitiesDataTable)
69
    {
70
        return $activitiesDataTable->with([
71
            'resource' => $manager,
72
            'tabs' => 'adminarea.managers.tabs',
73
            'id' => "adminarea-managers-{$manager->getRouteKey()}-activities-table",
74
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
75
    }
76
77
    /**
78
     * Show the form for create/update of the given resource attributes.
79
     *
80
     * @param \Illuminate\Http\Request    $request
81
     * @param \Cortex\Auth\Models\Manager $manager
82
     *
83
     * @return \Illuminate\View\View
84
     */
85
    public function attributes(Request $request, Manager $manager)
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
        return view('cortex/auth::adminarea.pages.manager-attributes', compact('manager'));
88
    }
89
90
    /**
91
     * Process the account update form.
92
     *
93
     * @param \Cortex\Auth\Http\Requests\Adminarea\ManagerAttributesFormRequest $request
94
     * @param \Cortex\Auth\Models\Manager                                       $manager
95
     *
96
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
97
     */
98
    public function updateAttributes(ManagerAttributesFormRequest $request, Manager $manager)
99
    {
100
        $data = $request->validated();
101
102
        // Update profile
103
        $manager->fill($data)->save();
104
105
        return intend([
106
            'back' => true,
107
            'with' => ['success' => trans('cortex/auth::messages.account.updated_attributes')],
108
        ]);
109
    }
110
111
    /**
112
     * Import managers.
113
     *
114
     * @param \Cortex\Auth\Models\Manager                          $manager
115
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
116
     *
117
     * @return \Illuminate\View\View
118
     */
119
    public function import(Manager $manager, ImportRecordsDataTable $importRecordsDataTable)
120
    {
121
        return $importRecordsDataTable->with([
122
            'resource' => $manager,
123
            'tabs' => 'adminarea.managers.tabs',
124
            'url' => route('adminarea.managers.stash'),
125
            'id' => "adminarea-attributes-{$manager->getRouteKey()}-import-table",
126
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
127
    }
128
129
    /**
130
     * Stash managers.
131
     *
132
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
133
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
134
     *
135
     * @return void
136
     */
137
    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...
138
    {
139
        // Handle the import
140
        $importer->config['resource'] = $this->resource;
141
        $importer->config['name'] = 'username';
142
        $importer->handleImport();
143
    }
144
145
    /**
146
     * Hoard managers.
147
     *
148
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
149
     *
150
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
151
     */
152
    public function hoard(ImportFormRequest $request)
153
    {
154
        foreach ((array) $request->get('selected_ids') as $recordId) {
155
            $record = app('cortex.foundation.import_record')->find($recordId);
156
157
            try {
158
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.auth.manager')->getFillable()))->toArray();
159
160
                tap(app('rinvex.auth.manager')->firstOrNew($fillable), function ($instance) use ($record) {
161
                    $instance->save() && $record->delete();
162
                });
163
            } catch (Exception $exception) {
164
                $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...
165
                $record->status = 'fail';
166
                $record->save();
167
            }
168
        }
169
170
        return intend([
171
            'back' => true,
172
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
173
        ]);
174
    }
175
176
    /**
177
     * List manager import logs.
178
     *
179
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
180
     *
181
     * @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...
182
     */
183
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
184
    {
185
        return $importLogsDatatable->with([
186
            'resource' => trans('cortex/auth::common.manager'),
187
            'tabs' => 'adminarea.managers.tabs',
188
            'id' => 'adminarea-managers-import-logs-table',
189
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
190
    }
191
192
    /**
193
     * Show manager create/edit form.
194
     *
195
     * @param \Illuminate\Http\Request    $request
196
     * @param \Cortex\Auth\Models\Manager $manager
197
     *
198
     * @return \Illuminate\View\View
199
     */
200
    protected function form(Request $request, Manager $manager)
201
    {
202
        $countries = collect(countries())->map(function ($country, $code) {
203
            return [
204
                'id' => $code,
205
                'text' => $country['name'],
206
                'emoji' => $country['emoji'],
207
            ];
208
        })->values();
209
210
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
211
        $languages = collect(languages())->pluck('name', 'iso_639_1');
212
        $genders = ['male' => trans('cortex/auth::common.male'), 'female' => trans('cortex/auth::common.female')];
213
        $abilities = $request->user($this->getGuard())->getManagedAbilities();
214
        $roles = $request->user($this->getGuard())->getManagedRoles();
215
216
        $tenants = app('rinvex.tenants.tenant')->all()->pluck('name', 'id')->toArray();
217
218
        asort($tenants);
219
220
        return view('cortex/auth::adminarea.pages.manager', compact('manager', 'abilities', 'tenants', 'roles', 'countries', 'languages', 'genders', 'tags'));
221
    }
222
223
    /**
224
     * Store new manager.
225
     *
226
     * @param \Cortex\Auth\Http\Requests\Adminarea\ManagerFormRequest $request
227
     * @param \Cortex\Auth\Models\Manager                             $manager
228
     *
229
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
230
     */
231
    public function store(ManagerFormRequest $request, Manager $manager)
232
    {
233
        return $this->process($request, $manager);
234
    }
235
236
    /**
237
     * Update given manager.
238
     *
239
     * @param \Cortex\Auth\Http\Requests\Adminarea\ManagerFormRequest $request
240
     * @param \Cortex\Auth\Models\Manager                             $manager
241
     *
242
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
243
     */
244
    public function update(ManagerFormRequest $request, Manager $manager)
245
    {
246
        return $this->process($request, $manager);
247
    }
248
249
    /**
250
     * Process stored/updated manager.
251
     *
252
     * @param \Illuminate\Foundation\Http\FormRequest $request
253
     * @param \Cortex\Auth\Models\Manager             $manager
254
     *
255
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
256
     */
257
    protected function process(FormRequest $request, Manager $manager)
258
    {
259
        // Prepare required input fields
260
        $data = $request->validated();
261
262
        ! $request->hasFile('profile_picture')
263
        || $manager->addMediaFromRequest('profile_picture')
264
                ->sanitizingFileName(function ($fileName) {
265
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
266
                })
267
                ->toMediaCollection('profile_picture', config('cortex.foundation.media.disk'));
268
269
        ! $request->hasFile('cover_photo')
270
        || $manager->addMediaFromRequest('cover_photo')
271
                ->sanitizingFileName(function ($fileName) {
272
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
273
                })
274
                ->toMediaCollection('cover_photo', config('cortex.foundation.media.disk'));
275
276
        // Save manager
277
        $manager->fill($data)->save();
278
279
        return intend([
280
            'url' => route('adminarea.managers.index'),
281
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.manager'), 'identifier' => $manager->username])],
282
        ]);
283
    }
284
285
    /**
286
     * Destroy given manager.
287
     *
288
     * @param \Cortex\Auth\Models\Manager $manager
289
     *
290
     * @throws \Exception
291
     *
292
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
293
     */
294
    public function destroy(Manager $manager)
295
    {
296
        $manager->delete();
297
298
        return intend([
299
            'url' => route('adminarea.managers.index'),
300
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.manager'), 'identifier' => $manager->username])],
301
        ]);
302
    }
303
}
304