MembersController   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 13

Importance

Changes 0
Metric Value
wmc 20
lcom 4
cbo 13
dl 0
loc 278
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A logs() 0 8 1
A activities() 0 8 1
A attributes() 0 4 1
A updateAttributes() 0 12 1
A import() 0 9 1
A stash() 0 7 1
A hoard() 0 23 5
A importLogs() 0 8 1
A form() 0 18 1
A store() 0 4 1
A update() 0 4 1
A process() 0 27 3
A destroy() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Managerarea;
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Cortex\Auth\Models\Member;
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\Managerarea\MembersDataTable;
17
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
18
use Cortex\Auth\Http\Requests\Managerarea\MemberFormRequest;
19
use Cortex\Foundation\Http\Controllers\AuthorizedController;
20
use Cortex\Auth\Http\Requests\Managerarea\MemberAttributesFormRequest;
21
22
class MembersController extends AuthorizedController
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected $resource = Member::class;
28
29
    /**
30
     * List all members.
31
     *
32
     * @param \Cortex\Auth\DataTables\Managerarea\MembersDataTable $membersDataTable
33
     *
34
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
35
     */
36
    public function index(MembersDataTable $membersDataTable)
37
    {
38
        return $membersDataTable->with([
39
            'id' => 'managerarea-members-index-table',
40
        ])->render('cortex/foundation::managerarea.pages.datatable-index');
41
    }
42
43
    /**
44
     * List member logs.
45
     *
46
     * @param \Cortex\Auth\Models\Member                  $member
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(Member $member, LogsDataTable $logsDataTable)
52
    {
53
        return $logsDataTable->with([
54
            'resource' => $member,
55
            'tabs' => 'managerarea.members.tabs',
56
            'id' => "managerarea-members-{$member->getRouteKey()}-logs-table",
57
        ])->render('cortex/foundation::managerarea.pages.datatable-tab');
58
    }
59
60
    /**
61
     * Get a listing of the resource activities.
62
     *
63
     * @param \Cortex\Auth\Models\Member                        $member
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(Member $member, ActivitiesDataTable $activitiesDataTable)
69
    {
70
        return $activitiesDataTable->with([
71
            'resource' => $member,
72
            'tabs' => 'managerarea.members.tabs',
73
            'id' => "managerarea-members-{$member->getRouteKey()}-activities-table",
74
        ])->render('cortex/foundation::managerarea.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\Member $member
82
     *
83
     * @return \Illuminate\View\View
84
     */
85
    public function attributes(Request $request, Member $member)
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::managerarea.pages.member-attributes', compact('member'));
88
    }
89
90
    /**
91
     * Process the account update form.
92
     *
93
     * @param \Cortex\Auth\Http\Requests\Managerarea\MemberAttributesFormRequest $request
94
     * @param \Cortex\Auth\Models\Member                                         $member
95
     *
96
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
97
     */
98
    public function updateAttributes(MemberAttributesFormRequest $request, Member $member)
99
    {
100
        $data = $request->validated();
101
102
        // Update profile
103
        $member->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 members.
113
     *
114
     * @param \Cortex\Auth\Models\Member                           $member
115
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
116
     *
117
     * @return \Illuminate\View\View
118
     */
119
    public function import(Member $member, ImportRecordsDataTable $importRecordsDataTable)
120
    {
121
        return $importRecordsDataTable->with([
122
            'resource' => $member,
123
            'tabs' => 'managerarea.attributes.tabs',
124
            'url' => route('managerarea.members.stash'),
125
            'id' => "managerarea-attributes-{$member->getRouteKey()}-import-table",
126
        ])->render('cortex/foundation::managerarea.pages.datatable-dropzone');
127
    }
128
129
    /**
130
     * Stash members.
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 members.
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.member')->getFillable()))->toArray();
159
160
                tap(app('rinvex.auth.member')->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 member 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.member'),
187
            'tabs' => 'managerarea.members.tabs',
188
            'id' => 'managerarea-members-import-logs-table',
189
        ])->render('cortex/foundation::managerarea.pages.datatable-tab');
190
    }
191
192
    /**
193
     * Show member create/edit form.
194
     *
195
     * @param \Illuminate\Http\Request   $request
196
     * @param \Cortex\Auth\Models\Member $member
197
     *
198
     * @return \Illuminate\View\View
199
     */
200
    protected function form(Request $request, Member $member)
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
        return view('cortex/auth::managerarea.pages.member', compact('member', 'abilities', 'roles', 'countries', 'languages', 'genders', 'tags'));
217
    }
218
219
    /**
220
     * Store new member.
221
     *
222
     * @param \Cortex\Auth\Http\Requests\Managerarea\MemberFormRequest $request
223
     * @param \Cortex\Auth\Models\Member                               $member
224
     *
225
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
226
     */
227
    public function store(MemberFormRequest $request, Member $member)
228
    {
229
        return $this->process($request, $member);
230
    }
231
232
    /**
233
     * Update given member.
234
     *
235
     * @param \Cortex\Auth\Http\Requests\Managerarea\MemberFormRequest $request
236
     * @param \Cortex\Auth\Models\Member                               $member
237
     *
238
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
239
     */
240
    public function update(MemberFormRequest $request, Member $member)
241
    {
242
        return $this->process($request, $member);
243
    }
244
245
    /**
246
     * Process stored/updated member.
247
     *
248
     * @param \Illuminate\Foundation\Http\FormRequest $request
249
     * @param \Cortex\Auth\Models\Member              $member
250
     *
251
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
252
     */
253
    protected function process(FormRequest $request, Member $member)
254
    {
255
        // Prepare required input fields
256
        $data = $request->validated();
257
258
        ! $request->hasFile('profile_picture')
259
        || $member->addMediaFromRequest('profile_picture')
260
                ->sanitizingFileName(function ($fileName) {
261
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
262
                })
263
                ->toMediaCollection('profile_picture', config('cortex.foundation.media.disk'));
264
265
        ! $request->hasFile('cover_photo')
266
        || $member->addMediaFromRequest('cover_photo')
267
                ->sanitizingFileName(function ($fileName) {
268
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
269
                })
270
                ->toMediaCollection('cover_photo', config('cortex.foundation.media.disk'));
271
272
        // Save member
273
        $member->fill($data)->save();
274
275
        return intend([
276
            'url' => route('managerarea.members.index'),
277
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.member'), 'identifier' => $member->username])],
278
        ]);
279
    }
280
281
    /**
282
     * Destroy given member.
283
     *
284
     * @param \Cortex\Auth\Models\Member $member
285
     *
286
     * @throws \Exception
287
     *
288
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
289
     */
290
    public function destroy(Member $member)
291
    {
292
        $member->delete();
293
294
        return intend([
295
            'url' => route('managerarea.members.index'),
296
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.member'), 'identifier' => $member->username])],
297
        ]);
298
    }
299
}
300