Completed
Push — master ( f2b66c...15eb15 )
by Abdelrahman
11:47 queued 10:02
created

src/Http/Controllers/Adminarea/RolesController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Cortex\Auth\Models\Role;
9
use Illuminate\Http\Request;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Foundation\Importers\DefaultImporter;
13
use Cortex\Auth\DataTables\Adminarea\RolesDataTable;
14
use Cortex\Foundation\DataTables\ImportLogsDataTable;
15
use Cortex\Foundation\Http\Requests\ImportFormRequest;
16
use Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest;
17
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
use Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest;
20
21
class RolesController extends AuthorizedController
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $resource = Role::class;
27
28
    /**
29
     * List all roles.
30
     *
31
     * @param \Cortex\Auth\DataTables\Adminarea\RolesDataTable $rolesDataTable
32
     *
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
34
     */
35
    public function index(RolesDataTable $rolesDataTable)
36
    {
37
        return $rolesDataTable->with([
38
            'id' => 'adminarea-roles-index-table',
39
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
40
    }
41
42
    /**
43
     * List role logs.
44
     *
45
     * @param \Cortex\Auth\Models\Role                    $role
46
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
47
     *
48
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
49
     */
50
    public function logs(Role $role, LogsDataTable $logsDataTable)
51
    {
52
        return $logsDataTable->with([
53
            'resource' => $role,
54
            'tabs' => 'adminarea.roles.tabs',
55
            'id' => "adminarea-roles-{$role->getRouteKey()}-logs-table",
56
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
57
    }
58
59
    /**
60
     * Import roles.
61
     *
62
     * @param \Cortex\Auth\Models\Role                             $role
63
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
64
     *
65
     * @return \Illuminate\View\View
66
     */
67
    public function import(Role $role, ImportRecordsDataTable $importRecordsDataTable)
68
    {
69
        return $importRecordsDataTable->with([
70
            'resource' => $role,
71
            'tabs' => 'adminarea.roles.tabs',
72
            'url' => route('adminarea.roles.stash'),
73
            'id' => "adminarea-roles-{$role->getRouteKey()}-import-table",
74
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
75
    }
76
77
    /**
78
     * Stash roles.
79
     *
80
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
81
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
82
     *
83
     * @return void
84
     */
85
    public function stash(ImportFormRequest $request, DefaultImporter $importer)
86
    {
87
        // Handle the import
88
        $importer->config['resource'] = $this->resource;
89
        $importer->handleImport();
90
    }
91
92
    /**
93
     * Hoard roles.
94
     *
95
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
96
     *
97
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
98
     */
99
    public function hoard(ImportFormRequest $request)
100
    {
101
        foreach ((array) $request->get('selected_ids') as $recordId) {
102
            $record = app('cortex.foundation.import_record')->find($recordId);
103
104
            try {
105
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.auth.role')->getFillable()))->toArray();
106
107
                tap(app('rinvex.auth.role')->firstOrNew($fillable), function ($instance) use ($record) {
108
                    $instance->save() && $record->delete();
109
                });
110
            } catch (Exception $exception) {
111
                $record->notes = $exception->getMessage().(method_exists($exception, 'getMessageBag') ? "\n".json_encode($exception->getMessageBag())."\n\n" : '');
112
                $record->status = 'fail';
113
                $record->save();
114
            }
115
        }
116
117
        return intend([
118
            'back' => true,
119
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
120
        ]);
121
    }
122
123
    /**
124
     * List role import logs.
125
     *
126
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
127
     *
128
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
129
     */
130
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
131
    {
132
        return $importLogsDatatable->with([
133
            'resource' => trans('cortex/auth::common.role'),
134
            'tabs' => 'adminarea.roles.tabs',
135
            'id' => 'adminarea-roles-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new role.
141
     *
142
     * @param \Illuminate\Http\Request $request
143
     * @param \Cortex\Auth\Models\Role $role
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Role $role)
148
    {
149
        return $this->form($request, $role);
150
    }
151
152
    /**
153
     * Edit given role.
154
     *
155
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormRequest $request
156
     * @param \Cortex\Auth\Models\Role                             $role
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(RoleFormRequest $request, Role $role)
161
    {
162
        return $this->form($request, $role);
163
    }
164
165
    /**
166
     * Show role create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request $request
169
     * @param \Cortex\Auth\Models\Role $role
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Role $role)
174
    {
175
        $currentUser = $request->user($this->getGuard());
176
        $abilities = get_area_abilities($currentUser);
177
178
        return view('cortex/auth::adminarea.pages.role', compact('role', 'abilities'));
179
    }
180
181
    /**
182
     * Store new role.
183
     *
184
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
185
     * @param \Cortex\Auth\Models\Role                                    $role
186
     *
187
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
188
     */
189
    public function store(RoleFormProcessRequest $request, Role $role)
190
    {
191
        return $this->process($request, $role);
192
    }
193
194
    /**
195
     * Update given role.
196
     *
197
     * @param \Cortex\Auth\Http\Requests\Adminarea\RoleFormProcessRequest $request
198
     * @param \Cortex\Auth\Models\Role                                    $role
199
     *
200
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
201
     */
202
    public function update(RoleFormProcessRequest $request, Role $role)
203
    {
204
        return $this->process($request, $role);
205
    }
206
207
    /**
208
     * Process stored/updated role.
209
     *
210
     * @param \Illuminate\Foundation\Http\FormRequest $request
211
     * @param \Cortex\Auth\Models\Role                $role
212
     *
213
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
214
     */
215
    protected function process(FormRequest $request, Role $role)
216
    {
217
        // Prepare required input fields
218
        $data = $request->validated();
219
220
        // Save role
221
        $role->fill($data)->save();
222
223
        return intend([
224
            'url' => route('adminarea.roles.index'),
225
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.role'), 'identifier' => $role->title])],
0 ignored issues
show
The property title does not exist on object<Cortex\Auth\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
226
        ]);
227
    }
228
229
    /**
230
     * Destroy given role.
231
     *
232
     * @param \Cortex\Auth\Models\Role $role
233
     *
234
     * @throws \Exception
235
     *
236
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
237
     */
238
    public function destroy(Role $role)
239
    {
240
        $role->delete();
241
242
        return intend([
243
            'url' => route('adminarea.roles.index'),
244
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.role'), 'identifier' => $role->title])],
0 ignored issues
show
The property title does not exist on object<Cortex\Auth\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
245
        ]);
246
    }
247
}
248