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

Http/Controllers/Adminarea/GuardiansController.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 Illuminate\Http\Request;
9
use Cortex\Auth\Models\Guardian;
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\GuardiansDataTable;
16
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
17
use Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest;
18
use Cortex\Foundation\Http\Controllers\AuthorizedController;
19
20
class GuardiansController extends AuthorizedController
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $resource = Guardian::class;
26
27
    /**
28
     * List all guardians.
29
     *
30
     * @param \Cortex\Auth\DataTables\Adminarea\GuardiansDataTable $guardiansDataTable
31
     *
32
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
33
     */
34
    public function index(GuardiansDataTable $guardiansDataTable)
35
    {
36
        return $guardiansDataTable->with([
37
            'id' => 'adminarea-guardians-index-table',
38
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
39
    }
40
41
    /**
42
     * List guardian logs.
43
     *
44
     * @param \Cortex\Auth\Models\Guardian                $guardian
45
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
46
     *
47
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
48
     */
49
    public function logs(Guardian $guardian, LogsDataTable $logsDataTable)
50
    {
51
        return $logsDataTable->with([
52
            'resource' => $guardian,
53
            'tabs' => 'adminarea.guardians.tabs',
54
            'id' => "adminarea-guardians-{$guardian->getRouteKey()}-logs-table",
55
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
56
    }
57
58
    /**
59
     * Import guardians.
60
     *
61
     * @param \Cortex\Auth\Models\Guardian                         $guardian
62
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
63
     *
64
     * @return \Illuminate\View\View
65
     */
66
    public function import(Guardian $guardian, ImportRecordsDataTable $importRecordsDataTable)
67
    {
68
        return $importRecordsDataTable->with([
69
            'resource' => $guardian,
70
            'tabs' => 'adminarea.guardians.tabs',
71
            'url' => route('adminarea.guardians.stash'),
72
            'id' => "adminarea-attributes-{$guardian->getRouteKey()}-import-table",
73
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
74
    }
75
76
    /**
77
     * Stash guardians.
78
     *
79
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
80
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
81
     *
82
     * @return void
83
     */
84
    public function stash(ImportFormRequest $request, DefaultImporter $importer)
85
    {
86
        // Handle the import
87
        $importer->config['resource'] = $this->resource;
88
        $importer->config['name'] = 'username';
89
        $importer->handleImport();
90
    }
91
92
    /**
93
     * Hoard guardians.
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.guardian')->getFillable()))->toArray();
106
107
                tap(app('rinvex.auth.guardian')->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 guardian 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.guardian'),
134
            'tabs' => 'adminarea.guardians.tabs',
135
            'id' => 'adminarea-guardians-import-logs-table',
136
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
137
    }
138
139
    /**
140
     * Create new guardian.
141
     *
142
     * @param \Illuminate\Http\Request     $request
143
     * @param \Cortex\Auth\Models\Guardian $guardian
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    public function create(Request $request, Guardian $guardian)
148
    {
149
        return $this->form($request, $guardian);
150
    }
151
152
    /**
153
     * Edit given guardian.
154
     *
155
     * @param \Illuminate\Http\Request     $request
156
     * @param \Cortex\Auth\Models\Guardian $guardian
157
     *
158
     * @return \Illuminate\View\View
159
     */
160
    public function edit(Request $request, Guardian $guardian)
161
    {
162
        return $this->form($request, $guardian);
163
    }
164
165
    /**
166
     * Show guardian create/edit form.
167
     *
168
     * @param \Illuminate\Http\Request     $request
169
     * @param \Cortex\Auth\Models\Guardian $guardian
170
     *
171
     * @return \Illuminate\View\View
172
     */
173
    protected function form(Request $request, Guardian $guardian)
174
    {
175
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
176
177
        return view('cortex/auth::adminarea.pages.guardian', compact('guardian', 'tags'));
178
    }
179
180
    /**
181
     * Store new guardian.
182
     *
183
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
184
     * @param \Cortex\Auth\Models\Guardian                             $guardian
185
     *
186
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
187
     */
188
    public function store(GuardianFormRequest $request, Guardian $guardian)
189
    {
190
        return $this->process($request, $guardian);
191
    }
192
193
    /**
194
     * Update given guardian.
195
     *
196
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
197
     * @param \Cortex\Auth\Models\Guardian                             $guardian
198
     *
199
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
200
     */
201
    public function update(GuardianFormRequest $request, Guardian $guardian)
202
    {
203
        return $this->process($request, $guardian);
204
    }
205
206
    /**
207
     * Process stored/updated guardian.
208
     *
209
     * @param \Illuminate\Foundation\Http\FormRequest $request
210
     * @param \Cortex\Auth\Models\Guardian            $guardian
211
     *
212
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
213
     */
214
    protected function process(FormRequest $request, Guardian $guardian)
215
    {
216
        // Prepare required input fields
217
        $data = $request->validated();
218
219
        // Save guardian
220
        $guardian->fill($data)->save();
221
222
        return intend([
223
            'url' => route('adminarea.guardians.index'),
224
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
The property username does not exist on object<Cortex\Auth\Models\Guardian>. 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...
225
        ]);
226
    }
227
228
    /**
229
     * Destroy given guardian.
230
     *
231
     * @param \Cortex\Auth\Models\Guardian $guardian
232
     *
233
     * @throws \Exception
234
     *
235
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
236
     */
237
    public function destroy(Guardian $guardian)
238
    {
239
        $guardian->delete();
240
241
        return intend([
242
            'url' => route('adminarea.guardians.index'),
243
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
The property username does not exist on object<Cortex\Auth\Models\Guardian>. 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