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

Http/Controllers/Adminarea/AbilitiesController.php (4 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 Illuminate\Http\Request;
9
use Cortex\Auth\Models\Ability;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Foundation\Importers\DefaultImporter;
13
use Cortex\Foundation\DataTables\ImportLogsDataTable;
14
use Cortex\Foundation\Http\Requests\ImportFormRequest;
15
use Cortex\Auth\DataTables\Adminarea\AbilitiesDataTable;
16
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
17
use Cortex\Auth\Http\Requests\Adminarea\AbilityFormRequest;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
use Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest;
20
21
class AbilitiesController extends AuthorizedController
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected $resource = Ability::class;
27
28
    /**
29
     * List all abilities.
30
     *
31
     * @param \Cortex\Auth\DataTables\Adminarea\AbilitiesDataTable $abilitiesDataTable
32
     *
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
34
     */
35
    public function index(AbilitiesDataTable $abilitiesDataTable)
36
    {
37
        return $abilitiesDataTable->with([
38
            'id' => 'adminarea-abilities-index-table',
39
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
40
    }
41
42
    /**
43
     * List ability logs.
44
     *
45
     * @param \Cortex\Auth\Models\Ability                 $ability
46
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
47
     *
48
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
49
     */
50
    public function logs(Ability $ability, LogsDataTable $logsDataTable)
51
    {
52
        return $logsDataTable->with([
53
            'resource' => $ability,
54
            'tabs' => 'adminarea.abilities.tabs',
55
            'id' => "adminarea-abilities-{$ability->getRouteKey()}-logs-table",
56
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
57
    }
58
59
    /**
60
     * Import abilities.
61
     *
62
     * @param \Cortex\Auth\Models\Ability                          $ability
63
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
64
     *
65
     * @return \Illuminate\View\View
66
     */
67
    public function import(Ability $ability, ImportRecordsDataTable $importRecordsDataTable)
68
    {
69
        return $importRecordsDataTable->with([
70
            'resource' => $ability,
71
            'tabs' => 'adminarea.abilities.tabs',
72
            'url' => route('adminarea.abilities.stash'),
73
            'id' => "adminarea-abilities-{$ability->getRouteKey()}-import-table",
74
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
75
    }
76
77
    /**
78
     * Stash abilities.
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 abilities.
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.ability')->getFillable()))->toArray();
106
107
                tap(app('rinvex.auth.ability')->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 ability 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.ability'),
134
            'tabs' => 'adminarea.abilities.tabs',
135
            'id' => 'adminarea-abilities-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new ability.
141
     *
142
     * @param \Illuminate\Http\Request $request
143
     * @param \Cortex\Auth\Models\Role $ability
0 ignored issues
show
Should the type for parameter $ability not be Ability?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Ability $ability)
148
    {
149
        return $this->form($request, $ability);
150
    }
151
152
    /**
153
     * Edit given ability.
154
     *
155
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormRequest $request
156
     * @param \Cortex\Auth\Models\Role                                $ability
0 ignored issues
show
Should the type for parameter $ability not be Ability?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(AbilityFormRequest $request, Ability $ability)
161
    {
162
        return $this->form($request, $ability);
163
    }
164
165
    /**
166
     * Show ability create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request    $request
169
     * @param \Cortex\Auth\Models\Ability $ability
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Ability $ability)
174
    {
175
        $entityTypes = app('cortex.auth.ability')->distinct()->get(['entity_type'])->pluck('entity_type', 'entity_type')->toArray();
176
        $currentUser = $request->user($this->getGuard());
177
        $roles = get_area_roles($currentUser);
178
179
        return view('cortex/auth::adminarea.pages.ability', compact('ability', 'roles', 'entityTypes'));
180
    }
181
182
    /**
183
     * Store new ability.
184
     *
185
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest $request
186
     * @param \Cortex\Auth\Models\Ability                                    $ability
187
     *
188
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
189
     */
190
    public function store(AbilityFormProcessRequest $request, Ability $ability)
191
    {
192
        return $this->process($request, $ability);
193
    }
194
195
    /**
196
     * Update given ability.
197
     *
198
     * @param \Cortex\Auth\Http\Requests\Adminarea\AbilityFormProcessRequest $request
199
     * @param \Cortex\Auth\Models\Ability                                    $ability
200
     *
201
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
202
     */
203
    public function update(AbilityFormProcessRequest $request, Ability $ability)
204
    {
205
        return $this->process($request, $ability);
206
    }
207
208
    /**
209
     * Process stored/updated ability.
210
     *
211
     * @param \Illuminate\Foundation\Http\FormRequest $request
212
     * @param \Cortex\Auth\Models\Ability             $ability
213
     *
214
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
215
     */
216
    protected function process(FormRequest $request, Ability $ability)
217
    {
218
        // Prepare required input fields
219
        $data = $request->validated();
220
221
        // Save ability
222
        $ability->fill($data)->save();
223
224
        return intend([
225
            'url' => route('adminarea.abilities.index'),
226
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.ability'), 'identifier' => $ability->title])],
0 ignored issues
show
The property title does not exist on object<Cortex\Auth\Models\Ability>. 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...
227
        ]);
228
    }
229
230
    /**
231
     * Destroy given ability.
232
     *
233
     * @param \Cortex\Auth\Models\Ability $ability
234
     *
235
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
236
     */
237
    public function destroy(Ability $ability)
238
    {
239
        $ability->delete();
240
241
        return intend([
242
            'url' => route('adminarea.abilities.index'),
243
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.ability'), 'identifier' => $ability->title])],
0 ignored issues
show
The property title does not exist on object<Cortex\Auth\Models\Ability>. 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...
244
        ]);
245
    }
246
}
247