Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

MembersController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 10
dl 0
loc 229
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 7 1
A logs() 0 9 1
A activities() 0 9 1
A attributes() 0 4 1
A updateAttributes() 0 12 1
A create() 0 4 1
A edit() 0 4 1
A form() 0 23 3
A store() 0 4 1
A update() 0 4 1
B process() 0 27 3
A destroy() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Illuminate\Http\Request;
8
use Cortex\Auth\Models\Member;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Foundation\DataTables\ActivitiesDataTable;
12
use Cortex\Auth\DataTables\Adminarea\MembersDataTable;
13
use Cortex\Auth\Http\Requests\Adminarea\MemberFormRequest;
14
use Cortex\Foundation\Http\Controllers\AuthorizedController;
15
use Cortex\Auth\Http\Requests\Adminarea\MemberAttributesFormRequest;
16
17
class MembersController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = 'member';
23
24
    /**
25
     * List all members.
26
     *
27
     * @param \Cortex\Auth\DataTables\Adminarea\MembersDataTable $membersDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(MembersDataTable $membersDataTable)
32
    {
33
        return $membersDataTable->with([
34
            'id' => 'adminarea-members-index-table',
35
            'phrase' => trans('cortex/auth::common.members'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * List member logs.
41
     *
42
     * @param \Cortex\Auth\Models\Member                  $member
43
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
44
     *
45
     * @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...
46
     */
47
    public function logs(Member $member, LogsDataTable $logsDataTable)
48
    {
49
        return $logsDataTable->with([
50
            'resource' => $member,
51
            'tabs' => 'adminarea.members.tabs',
52
            'phrase' => trans('cortex/auth::common.members'),
53
            'id' => "adminarea-members-{$member->getKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
55
    }
56
57
    /**
58
     * Get a listing of the resource activities.
59
     *
60
     * @param \Cortex\Auth\Models\Member                        $member
61
     * @param \Cortex\Foundation\DataTables\ActivitiesDataTable $activitiesDataTable
62
     *
63
     * @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...
64
     */
65
    public function activities(Member $member, ActivitiesDataTable $activitiesDataTable)
66
    {
67
        return $activitiesDataTable->with([
68
            'resource' => $member,
69
            'tabs' => 'adminarea.members.tabs',
70
            'phrase' => trans('cortex/auth::common.members'),
71
            'id' => "adminarea-members-{$member->getKey()}-activities-table",
72
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
73
    }
74
75
    /**
76
     * Show the form for create/update of the given resource attributes.
77
     *
78
     * @param \Illuminate\Http\Request   $request
79
     * @param \Cortex\Auth\Models\Member $member
80
     *
81
     * @return \Illuminate\View\View
82
     */
83
    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...
84
    {
85
        return view('cortex/auth::adminarea.pages.member-attributes', compact('member'));
86
    }
87
88
    /**
89
     * Process the account update form.
90
     *
91
     * @param \Cortex\Auth\Http\Requests\Adminarea\MemberAttributesFormRequest $request
92
     * @param \Cortex\Auth\Models\Member                                       $member
93
     *
94
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
95
     */
96
    public function updateAttributes(MemberAttributesFormRequest $request, Member $member)
97
    {
98
        $data = $request->validated();
99
100
        // Update profile
101
        $member->fill($data)->save();
102
103
        return intend([
104
            'back' => true,
105
            'with' => ['success' => trans('cortex/auth::messages.account.updated_attributes')],
106
        ]);
107
    }
108
109
    /**
110
     * Create new member.
111
     *
112
     * @param \Illuminate\Http\Request   $request
113
     * @param \Cortex\Auth\Models\Member $member
114
     *
115
     * @return \Illuminate\View\View
116
     */
117
    public function create(Request $request, Member $member)
118
    {
119
        return $this->form($request, $member);
120
    }
121
122
    /**
123
     * Edit given member.
124
     *
125
     * @param \Illuminate\Http\Request   $request
126
     * @param \Cortex\Auth\Models\Member $member
127
     *
128
     * @return \Illuminate\View\View
129
     */
130
    public function edit(Request $request, Member $member)
131
    {
132
        return $this->form($request, $member);
133
    }
134
135
    /**
136
     * Show member create/edit form.
137
     *
138
     * @param \Illuminate\Http\Request   $request
139
     * @param \Cortex\Auth\Models\Member $member
140
     *
141
     * @return \Illuminate\View\View
142
     */
143
    protected function form(Request $request, Member $member)
144
    {
145
        $countries = collect(countries())->map(function ($country, $code) {
146
            return [
147
                'id' => $code,
148
                'text' => $country['name'],
149
                'emoji' => $country['emoji'],
150
            ];
151
        })->values();
152
        $currentUser = $request->user($this->getGuard());
153
        $languages = collect(languages())->pluck('name', 'iso_639_1');
154
        $genders = ['male' => trans('cortex/auth::common.male'), 'female' => trans('cortex/auth::common.female')];
155
156
        $roles = $currentUser->can('superadmin')
157
            ? app('cortex.auth.role')->all()->pluck('name', 'id')->toArray()
158
            : $currentUser->roles->pluck('name', 'id')->toArray();
159
160
        $abilities = $currentUser->can('superadmin')
161
            ? app('cortex.auth.ability')->all()->pluck('title', 'id')->toArray()
162
            : $currentUser->abilities->pluck('title', 'id')->toArray();
163
164
        return view('cortex/auth::adminarea.pages.member', compact('member', 'abilities', 'roles', 'countries', 'languages', 'genders'));
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...
165
    }
166
167
    /**
168
     * Store new member.
169
     *
170
     * @param \Cortex\Auth\Http\Requests\Adminarea\MemberFormRequest $request
171
     * @param \Cortex\Auth\Models\Member                             $member
172
     *
173
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
174
     */
175
    public function store(MemberFormRequest $request, Member $member)
176
    {
177
        return $this->process($request, $member);
178
    }
179
180
    /**
181
     * Update given member.
182
     *
183
     * @param \Cortex\Auth\Http\Requests\Adminarea\MemberFormRequest $request
184
     * @param \Cortex\Auth\Models\Member                             $member
185
     *
186
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
187
     */
188
    public function update(MemberFormRequest $request, Member $member)
189
    {
190
        return $this->process($request, $member);
191
    }
192
193
    /**
194
     * Process stored/updated member.
195
     *
196
     * @param \Illuminate\Foundation\Http\FormRequest $request
197
     * @param \Cortex\Auth\Models\Member              $member
198
     *
199
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
200
     */
201
    protected function process(FormRequest $request, Member $member)
202
    {
203
        // Prepare required input fields
204
        $data = $request->validated();
205
206
        ! $request->hasFile('profile_picture')
207
        || $member->addMediaFromRequest('profile_picture')
208
                ->sanitizingFileName(function ($fileName) {
209
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
210
                })
211
                ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
212
213
        ! $request->hasFile('cover_photo')
214
        || $member->addMediaFromRequest('cover_photo')
215
                ->sanitizingFileName(function ($fileName) {
216
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
217
                })
218
                ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
219
220
        // Save member
221
        $member->fill($data)->save();
222
223
        return intend([
224
            'url' => route('adminarea.members.index'),
225
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'member', 'id' => $member->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
226
        ]);
227
    }
228
229
    /**
230
     * Destroy given member.
231
     *
232
     * @param \Cortex\Auth\Models\Member $member
233
     *
234
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
235
     */
236
    public function destroy(Member $member)
237
    {
238
        $member->delete();
239
240
        return intend([
241
            'url' => route('adminarea.members.index'),
242
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'member', 'id' => $member->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 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...
243
        ]);
244
    }
245
}
246