Completed
Push — develop ( 2232ce...4f1791 )
by Abdelrahman
01:50
created

AttributesController::importLogs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Attributes\Http\Controllers\Adminarea;
6
7
use Cortex\Attributes\Models\Attribute;
8
use Exception;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Foundation\Importers\DefaultImporter;
12
use Cortex\Foundation\DataTables\ImportLogsDataTable;
13
use Cortex\Foundation\Http\Requests\ImportFormRequest;
14
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
15
use Cortex\Foundation\Http\Controllers\AuthorizedController;
16
use Cortex\Attributes\DataTables\Adminarea\AttributesDataTable;
17
use Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest;
18
19
class AttributesController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Attribute::class;
25
26
    /**
27
     * List all attributes.
28
     *
29
     * @param \Cortex\Attributes\DataTables\Adminarea\AttributesDataTable $attributesDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(AttributesDataTable $attributesDataTable)
34
    {
35
        return $attributesDataTable->with([
36
            'id' => 'adminarea-attributes-index-table',
37
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
38
    }
39
40
    /**
41
     * List attribute logs.
42
     *
43
     * @param \Cortex\Attributes\Models\Attribute         $attribute
44
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
45
     *
46
     * @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...
47
     */
48
    public function logs(Attribute $attribute, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $attribute,
52
            'tabs' => 'adminarea.attributes.tabs',
53
            'id' => "adminarea-attributes-{$attribute->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
55
    }
56
57
    /**
58
     * Import attributes.
59
     *
60
     * @param \Cortex\Attributes\Models\Attribute                  $attribute
61
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    public function import(Attribute $attribute, 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...
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $attribute,
69
            'tabs' => 'adminarea.attributes.tabs',
70
            'url' => route('adminarea.attributes.stash'),
71
            'id' => "adminarea-attributes-{$attribute->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
73
    }
74
75
    /**
76
     * Stash attributes.
77
     *
78
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
79
     * @param \Cortex\Foundation\Importers\DefaultImporter       $importer
80
     *
81
     * @return void
82
     */
83
    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...
84
    {
85
        // Handle the import
86
        $importer->config['resource'] = $this->resource;
87
        $importer->handleImport();
88
    }
89
90
    /**
91
     * Hoard attributes.
92
     *
93
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
94
     *
95
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
96
     */
97
    public function hoard(ImportFormRequest $request)
98
    {
99
        foreach ((array) $request->get('selected_ids') as $recordId) {
100
            $record = app('cortex.foundation.import_record')->find($recordId);
101
102
            try {
103
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.attributes.attribute')->getFillable()))->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 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...
104
105
                tap(app('rinvex.attributes.attribute')->firstOrNew($fillable), function ($instance) use ($record) {
106
                    $instance->save() && $record->delete();
107
                });
108
            } catch (Exception $exception) {
109
                $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...
110
                $record->status = 'fail';
111
                $record->save();
112
            }
113
        }
114
115
        return intend([
116
            'back' => true,
117
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
118
        ]);
119
    }
120
121
    /**
122
     * List attribute import logs.
123
     *
124
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
125
     *
126
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
127
     */
128
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
129
    {
130
        return $importLogsDatatable->with([
131
            'resource' => trans('cortex/attributes::common.attribute'),
132
            'tabs' => 'adminarea.attributes.tabs',
133
            'id' => 'adminarea-attributes-import-logs-table',
134
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
135
    }
136
137
    /**
138
     * Create new attribute.
139
     *
140
     * @param \Cortex\Attributes\Models\Attribute $attribute
141
     *
142
     * @return \Illuminate\View\View
143
     */
144
    public function create(Attribute $attribute)
145
    {
146
        return $this->form($attribute);
147
    }
148
149
    /**
150
     * Edit given attribute.
151
     *
152
     * @param \Cortex\Attributes\Models\Attribute $attribute
153
     *
154
     * @return \Illuminate\View\View
155
     */
156
    public function edit(Attribute $attribute)
157
    {
158
        return $this->form($attribute);
159
    }
160
161
    /**
162
     * Show attribute create/edit form.
163
     *
164
     * @param \Cortex\Attributes\Models\Attribute $attribute
165
     *
166
     * @return \Illuminate\View\View
167
     */
168
    protected function form(Attribute $attribute)
169
    {
170
        $groups = app('rinvex.attributes.attribute')->distinct()->get(['group'])->pluck('group', 'group')->toArray();
171
        $entities = array_combine(app('rinvex.attributes.entities')->toArray(), app('rinvex.attributes.entities')->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...
172
        $types = array_combine($typeKeys = array_keys(Attribute::typeMap()), array_map(function ($item) {
173
            return trans('cortex/attributes::common.'.$item);
174
        }, $typeKeys));
175
176
        asort($types);
177
        asort($groups);
178
        asort($entities);
179
180
        return view('cortex/attributes::adminarea.pages.attribute', compact('attribute', 'groups', 'entities', 'types'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 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...
181
    }
182
183
    /**
184
     * Store new attribute.
185
     *
186
     * @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request
187
     * @param \Cortex\Attributes\Models\Attribute                             $attribute
188
     *
189
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
190
     */
191
    public function store(AttributeFormRequest $request, Attribute $attribute)
192
    {
193
        return $this->process($request, $attribute);
194
    }
195
196
    /**
197
     * Update given attribute.
198
     *
199
     * @param \Cortex\Attributes\Http\Requests\Adminarea\AttributeFormRequest $request
200
     * @param \Cortex\Attributes\Models\Attribute                             $attribute
201
     *
202
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
203
     */
204
    public function update(AttributeFormRequest $request, Attribute $attribute)
205
    {
206
        return $this->process($request, $attribute);
207
    }
208
209
    /**
210
     * Process stored/updated attribute.
211
     *
212
     * @param \Illuminate\Foundation\Http\FormRequest $request
213
     * @param \Cortex\Attributes\Models\Attribute     $attribute
214
     *
215
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
216
     */
217
    protected function process(FormRequest $request, Attribute $attribute)
218
    {
219
        // Prepare required input fields
220
        $data = $request->validated();
221
222
        // Save attribute
223
        $attribute->fill($data)->save();
224
225
        return intend([
226
            'url' => route('adminarea.attributes.index'),
227
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 185 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...
228
        ]);
229
    }
230
231
    /**
232
     * Destroy given attribute.
233
     *
234
     * @param \Cortex\Attributes\Models\Attribute $attribute
235
     *
236
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
237
     */
238
    public function destroy(Attribute $attribute)
239
    {
240
        $attribute->delete();
241
242
        return intend([
243
            'url' => route('adminarea.attributes.index'),
244
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/attributes::common.attribute'), 'identifier' => $attribute->name])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 187 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...
245
        ]);
246
    }
247
}
248