Completed
Push — master ( 3e8f18...c69213 )
by Abdelrahman
02:02
created

GuardiansController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 0
loc 201
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A logs() 0 8 1
A import() 0 9 1
A stash() 0 7 1
A hoard() 0 23 5
A importLogs() 0 8 1
A form() 0 6 1
A store() 0 4 1
A update() 0 4 1
A process() 0 13 1
A destroy() 0 9 1
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
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(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)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $importRecordsDataTable exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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)
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...
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();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
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" : '');
0 ignored issues
show
Bug introduced by
The method getMessageBag() does not exist on Exception. Did you maybe mean getMessage()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 163 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...
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
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...
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
     * Show guardian create/edit form.
141
     *
142
     * @param \Illuminate\Http\Request     $request
143
     * @param \Cortex\Auth\Models\Guardian $guardian
144
     *
145
     * @return \Illuminate\View\View
146
     */
147
    protected function form(Request $request, Guardian $guardian)
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...
148
    {
149
        $tags = app('rinvex.tags.tag')->pluck('name', 'id');
150
151
        return view('cortex/auth::adminarea.pages.guardian', compact('guardian', 'tags'));
152
    }
153
154
    /**
155
     * Store new guardian.
156
     *
157
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
158
     * @param \Cortex\Auth\Models\Guardian                             $guardian
159
     *
160
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
161
     */
162
    public function store(GuardianFormRequest $request, Guardian $guardian)
163
    {
164
        return $this->process($request, $guardian);
165
    }
166
167
    /**
168
     * Update given guardian.
169
     *
170
     * @param \Cortex\Auth\Http\Requests\Adminarea\GuardianFormRequest $request
171
     * @param \Cortex\Auth\Models\Guardian                             $guardian
172
     *
173
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
174
     */
175
    public function update(GuardianFormRequest $request, Guardian $guardian)
176
    {
177
        return $this->process($request, $guardian);
178
    }
179
180
    /**
181
     * Process stored/updated guardian.
182
     *
183
     * @param \Illuminate\Foundation\Http\FormRequest $request
184
     * @param \Cortex\Auth\Models\Guardian            $guardian
185
     *
186
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
187
     */
188
    protected function process(FormRequest $request, Guardian $guardian)
189
    {
190
        // Prepare required input fields
191
        $data = $request->validated();
192
193
        // Save guardian
194
        $guardian->fill($data)->save();
195
196
        return intend([
197
            'url' => route('adminarea.guardians.index'),
198
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
Documentation introduced by
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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 181 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...
199
        ]);
200
    }
201
202
    /**
203
     * Destroy given guardian.
204
     *
205
     * @param \Cortex\Auth\Models\Guardian $guardian
206
     *
207
     * @throws \Exception
208
     *
209
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
210
     */
211
    public function destroy(Guardian $guardian)
212
    {
213
        $guardian->delete();
214
215
        return intend([
216
            'url' => route('adminarea.guardians.index'),
217
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/auth::common.guardian'), 'identifier' => $guardian->username])],
0 ignored issues
show
Documentation introduced by
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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 183 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...
218
        ]);
219
    }
220
}
221