Completed
Push — develop ( 508664...7b00fe )
by Abdelrahman
01:58
created

AdminsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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\Admin;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Auth\DataTables\Adminarea\AdminsDataTable;
12
use Cortex\Foundation\DataTables\ActivitiesDataTable;
13
use Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest;
14
use Cortex\Foundation\Http\Controllers\AuthorizedController;
15
use Cortex\Auth\Http\Requests\Adminarea\AdminAttributesFormRequest;
16
17
class AdminsController extends AuthorizedController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resource = Admin::class;
23
24
    /**
25
     * List all admins.
26
     *
27
     * @param \Cortex\Auth\DataTables\Adminarea\AdminsDataTable $adminsDataTable
28
     *
29
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
30
     */
31
    public function index(AdminsDataTable $adminsDataTable)
32
    {
33
        return $adminsDataTable->with([
34
            'id' => 'adminarea-admins-index-table',
35
            'phrase' => trans('cortex/auth::common.admins'),
36
        ])->render('cortex/foundation::adminarea.pages.datatable');
37
    }
38
39
    /**
40
     * List admin logs.
41
     *
42
     * @param \Cortex\Auth\Models\Admin                   $admin
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(Admin $admin, LogsDataTable $logsDataTable)
48
    {
49
        return $logsDataTable->with([
50
            'resource' => $admin,
51
            'tabs' => 'adminarea.admins.tabs',
52
            'phrase' => trans('cortex/auth::common.admins'),
53
            'id' => "adminarea-admins-{$admin->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\Admin                         $admin
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(Admin $admin, ActivitiesDataTable $activitiesDataTable)
66
    {
67
        return $activitiesDataTable->with([
68
            'resource' => $admin,
69
            'tabs' => 'adminarea.admins.tabs',
70
            'phrase' => trans('cortex/auth::common.admins'),
71
            'id' => "adminarea-admins-{$admin->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\Admin $admin
80
     *
81
     * @return \Illuminate\View\View
82
     */
83
    public function attributes(Request $request, Admin $admin)
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.admin-attributes', compact('admin'));
86
    }
87
88
    /**
89
     * Process the account update form.
90
     *
91
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminAttributesFormRequest $request
92
     * @param \Cortex\Auth\Models\Admin                                       $admin
93
     *
94
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
95
     */
96
    public function updateAttributes(AdminAttributesFormRequest $request, Admin $admin)
97
    {
98
        $data = $request->validated();
99
100
        // Update profile
101
        $admin->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 admin.
111
     *
112
     * @param \Illuminate\Http\Request  $request
113
     * @param \Cortex\Auth\Models\Admin $admin
114
     *
115
     * @return \Illuminate\View\View
116
     */
117
    public function create(Request $request, Admin $admin)
118
    {
119
        return $this->form($request, $admin);
120
    }
121
122
    /**
123
     * Edit given admin.
124
     *
125
     * @param \Illuminate\Http\Request  $request
126
     * @param \Cortex\Auth\Models\Admin $admin
127
     *
128
     * @return \Illuminate\View\View
129
     */
130
    public function edit(Request $request, Admin $admin)
131
    {
132
        return $this->form($request, $admin);
133
    }
134
135
    /**
136
     * Show admin create/edit form.
137
     *
138
     * @param \Illuminate\Http\Request  $request
139
     * @param \Cortex\Auth\Models\Admin $admin
140
     *
141
     * @return \Illuminate\View\View
142
     */
143
    protected function form(Request $request, Admin $admin)
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()->groupBy('entity_type')->map->pluck('title', 'id')->toArray()
162
            : $currentUser->getAbilities()->groupBy('entity_type')->map->pluck('title', 'id')->toArray();
163
164
        asort($roles);
165
        ksort($abilities);
166
167
        return view('cortex/auth::adminarea.pages.admin', compact('admin', 'abilities', 'roles', 'countries', 'languages', 'genders'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 135 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...
168
    }
169
170
    /**
171
     * Store new admin.
172
     *
173
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest $request
174
     * @param \Cortex\Auth\Models\Admin                             $admin
175
     *
176
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
177
     */
178
    public function store(AdminFormRequest $request, Admin $admin)
179
    {
180
        return $this->process($request, $admin);
181
    }
182
183
    /**
184
     * Update given admin.
185
     *
186
     * @param \Cortex\Auth\Http\Requests\Adminarea\AdminFormRequest $request
187
     * @param \Cortex\Auth\Models\Admin                             $admin
188
     *
189
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
190
     */
191
    public function update(AdminFormRequest $request, Admin $admin)
192
    {
193
        return $this->process($request, $admin);
194
    }
195
196
    /**
197
     * Process stored/updated admin.
198
     *
199
     * @param \Illuminate\Foundation\Http\FormRequest $request
200
     * @param \Cortex\Auth\Models\Admin               $admin
201
     *
202
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
203
     */
204
    protected function process(FormRequest $request, Admin $admin)
205
    {
206
        // Prepare required input fields
207
        $data = $request->validated();
208
209
        ! $request->hasFile('profile_picture')
210
        || $admin->addMediaFromRequest('profile_picture')
211
                ->sanitizingFileName(function ($fileName) {
212
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
213
                })
214
                ->toMediaCollection('profile_picture', config('cortex.auth.media.disk'));
215
216
        ! $request->hasFile('cover_photo')
217
        || $admin->addMediaFromRequest('cover_photo')
218
                ->sanitizingFileName(function ($fileName) {
219
                    return md5($fileName).'.'.pathinfo($fileName, PATHINFO_EXTENSION);
220
                })
221
                ->toMediaCollection('cover_photo', config('cortex.auth.media.disk'));
222
223
        // Save admin
224
        $admin->fill($data)->save();
225
226
        return intend([
227
            'url' => route('adminarea.admins.index'),
228
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'admin', 'id' => $admin->username])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 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...
229
        ]);
230
    }
231
232
    /**
233
     * Destroy given admin.
234
     *
235
     * @param \Cortex\Auth\Models\Admin $admin
236
     *
237
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
238
     */
239
    public function destroy(Admin $admin)
240
    {
241
        $admin->delete();
242
243
        return intend([
244
            'url' => route('adminarea.admins.index'),
245
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'admin', 'id' => $admin->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...
246
        ]);
247
    }
248
}
249