Completed
Push — develop ( 242664...eb47a8 )
by Abdelrahman
16:40
created

RolesController::logs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

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 7
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Cortex\Auth\Models\Role;
8
use Cortex\Foundation\DataTables\ImportLogsDataTable;
9
use Cortex\Foundation\Http\Requests\ImportFormRequest;
10
use Cortex\Foundation\Importers\DefaultImporter;
11
use Illuminate\Http\Request;
12
use Illuminate\Foundation\Http\FormRequest;
13
use Cortex\Foundation\DataTables\LogsDataTable;
14
use Cortex\Auth\DataTables\Adminarea\RolesDataTable;
15
use Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest;
16
use Cortex\Foundation\Http\Controllers\AuthorizedController;
17
use Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest;
18
19
class RolesController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Role::class;
25
26
    /**
27
     * List all roles.
28
     *
29
     * @param \Cortex\Auth\DataTables\Adminarea\RolesDataTable $rolesDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(RolesDataTable $rolesDataTable)
34
    {
35
        return $rolesDataTable->with([
36
            'id' => 'adminarea-roles-index-table',
37
            'phrase' => trans('cortex/auth::common.roles'),
38
        ])->render('cortex/foundation::adminarea.pages.datatable');
39
    }
40
41
    /**
42
     * List role logs.
43
     *
44
     * @param \Cortex\Auth\Models\Role                    $role
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(Role $role, LogsDataTable $logsDataTable)
50
    {
51
        return $logsDataTable->with([
52
            'resource' => $role,
53
            'tabs' => 'adminarea.roles.tabs',
54
            'phrase' => trans('cortex/auth::common.roles'),
55
            'id' => "adminarea-roles-{$role->getKey()}-logs-table",
56
        ])->render('cortex/foundation::adminarea.pages.datatable-logs');
57
    }
58
59
    /**
60
     * Import roles.
61
     *
62
     * @return \Illuminate\View\View
63
     */
64
    public function import()
65
    {
66
        return view('cortex/foundation::adminarea.pages.import', [
67
            'id' => 'adminarea-roles-import',
68
            'tabs' => 'adminarea.roles.tabs',
69
            'url' => route('adminarea.roles.hoard'),
70
            'phrase' => trans('cortex/auth::common.roles'),
71
        ]);
72
    }
73
74
    /**
75
     * Hoard roles.
76
     *
77
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
78
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
79
     *
80
     * @return void
81
     */
82
    public function hoard(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...
83
    {
84
        // Handle the import
85
        $importer->config['resource'] = $this->resource;
86
        $importer->handleImport();
87
    }
88
89
    /**
90
     * List role import logs.
91
     *
92
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
93
     *
94
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
95
     */
96
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
97
    {
98
        return $importLogsDatatable->with([
99
            'resource' => 'role',
100
            'tabs' => 'adminarea.roles.tabs',
101
            'id' => 'adminarea-roles-import-logs-table',
102
            'phrase' => trans('cortex/roles::common.roles'),
103
        ])->render('cortex/foundation::adminarea.pages.datatable-import-logs');
104
    }
105
106
    /**
107
     * Create new role.
108
     *
109
     * @param \Illuminate\Http\Request $request
110
     * @param \Cortex\Auth\Models\Role $role
111
     *
112
     * @return \Illuminate\View\View
113
     */
114
    public function create(Request $request, Role $role)
115
    {
116
        return $this->form($request, $role);
117
    }
118
119
    /**
120
     * Edit given role.
121
     *
122
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest $request
123
     * @param \Cortex\Auth\Models\Role                             $role
124
     *
125
     * @return \Illuminate\View\View
126
     */
127
    public function edit(RoleFormRequest $request, Role $role)
128
    {
129
        return $this->form($request, $role);
130
    }
131
132
    /**
133
     * Show role create/edit form.
134
     *
135
     * @param \Illuminate\Http\Request $request
136
     * @param \Cortex\Auth\Models\Role $role
137
     *
138
     * @return \Illuminate\View\View
139
     */
140
    protected function form(Request $request, Role $role)
141
    {
142
        $abilities = $request->user($this->getGuard())->can('superadmin')
143
            ? app('cortex.auth.ability')->all()->groupBy('entity_type')->map->pluck('title', 'id')->toArray()
144
            : $request->user($this->getGuard())->getAbilities()->groupBy('entity_type')->map->pluck('title', 'id')->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
145
146
        ksort($abilities);
147
148
        return view('cortex/auth::adminarea.pages.role', compact('role', 'abilities'));
149
    }
150
151
    /**
152
     * Store new role.
153
     *
154
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
155
     * @param \Cortex\Auth\Models\Role                                    $role
156
     *
157
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
158
     */
159
    public function store(RoleFormProcessRequest $request, Role $role)
160
    {
161
        return $this->process($request, $role);
162
    }
163
164
    /**
165
     * Update given role.
166
     *
167
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
168
     * @param \Cortex\Auth\Models\Role                                    $role
169
     *
170
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
171
     */
172
    public function update(RoleFormProcessRequest $request, Role $role)
173
    {
174
        return $this->process($request, $role);
175
    }
176
177
    /**
178
     * Process stored/updated role.
179
     *
180
     * @param \Illuminate\Foundation\Http\FormRequest $request
181
     * @param \Cortex\Auth\Models\Role                $role
182
     *
183
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
184
     */
185
    protected function process(FormRequest $request, Role $role)
186
    {
187
        // Prepare required input fields
188
        $data = $request->validated();
189
190
        // Save role
191
        $role->fill($data)->save();
192
193
        return intend([
194
            'url' => route('adminarea.roles.index'),
195
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'role', 'id' => $role->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
196
        ]);
197
    }
198
199
    /**
200
     * Destroy given role.
201
     *
202
     * @param \Cortex\Auth\Models\Role $role
203
     *
204
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
205
     */
206
    public function destroy(Role $role)
207
    {
208
        $role->delete();
209
210
        return intend([
211
            'url' => route('adminarea.roles.index'),
212
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'role', 'id' => $role->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
213
        ]);
214
    }
215
}
216