TagsController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A import() 0 9 1
A stash() 0 6 1
A hoard() 0 23 5
A importLogs() 0 8 1
A logs() 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\Tags\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Cortex\Tags\Models\Tag;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Cortex\Foundation\DataTables\LogsDataTable;
11
use Cortex\Foundation\Importers\DefaultImporter;
12
use Cortex\Tags\DataTables\Adminarea\TagsDataTable;
13
use Cortex\Foundation\DataTables\ImportLogsDataTable;
14
use Cortex\Foundation\Http\Requests\ImportFormRequest;
15
use Cortex\Tags\Http\Requests\Adminarea\TagFormRequest;
16
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
17
use Cortex\Foundation\Http\Controllers\AuthorizedController;
18
19
class TagsController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Tag::class;
25
26
    /**
27
     * List all tags.
28
     *
29
     * @param \Cortex\Tags\DataTables\Adminarea\TagsDataTable $tagsDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(TagsDataTable $tagsDataTable)
34
    {
35
        return $tagsDataTable->with([
36
            'id' => 'adminarea-tags-index-table',
37
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
38
    }
39
40
    /**
41
     * List tag logs.
42
     *
43
     * @param \Cortex\Tags\Models\Tag                     $tag
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(Tag $tag, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $tag,
52
            'tabs' => 'adminarea.tags.tabs',
53
            'id' => "adminarea-tags-{$tag->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
55
    }
56
57
    /**
58
     * Import tags.
59
     *
60
     * @param \Cortex\Tags\Models\Tag                              $tag
61
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    public function import(Tag $tag, ImportRecordsDataTable $importRecordsDataTable)
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $tag,
69
            'tabs' => 'adminarea.tags.tabs',
70
            'url' => route('adminarea.tags.stash'),
71
            'id' => "adminarea-tags-{$tag->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
73
    }
74
75
    /**
76
     * Stash tags.
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 tags.
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.tags.tag')->getFillable()))->toArray();
104
105
                tap(app('rinvex.tags.tag')->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...
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 tag import logs.
123
     *
124
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
125
     *
126
     * @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...
127
     */
128
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
129
    {
130
        return $importLogsDatatable->with([
131
            'resource' => trans('cortex/tags::common.tag'),
132
            'tabs' => 'adminarea.tags.tabs',
133
            'id' => 'adminarea-tags-import-logs-table',
134
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
135
    }
136
137
    /**
138
     * Show tag create/edit form.
139
     *
140
     * @param \Cortex\Tags\Models\Tag $tag
141
     *
142
     * @return \Illuminate\View\View
143
     */
144
    protected function form(Tag $tag)
145
    {
146
        $groups = app('rinvex.tags.tag')->distinct()->get(['group'])->pluck('group', 'group')->toArray();
147
148
        return view('cortex/tags::adminarea.pages.tag', compact('tag', 'groups'));
149
    }
150
151
    /**
152
     * Store new tag.
153
     *
154
     * @param \Cortex\Tags\Http\Requests\Adminarea\TagFormRequest $request
155
     * @param \Cortex\Tags\Models\Tag                             $tag
156
     *
157
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
158
     */
159
    public function store(TagFormRequest $request, Tag $tag)
160
    {
161
        return $this->process($request, $tag);
162
    }
163
164
    /**
165
     * Update given tag.
166
     *
167
     * @param \Cortex\Tags\Http\Requests\Adminarea\TagFormRequest $request
168
     * @param \Cortex\Tags\Models\Tag                             $tag
169
     *
170
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
171
     */
172
    public function update(TagFormRequest $request, Tag $tag)
173
    {
174
        return $this->process($request, $tag);
175
    }
176
177
    /**
178
     * Process stored/updated tag.
179
     *
180
     * @param \Illuminate\Foundation\Http\FormRequest $request
181
     * @param \Cortex\Tags\Models\Tag                 $tag
182
     *
183
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
184
     */
185
    protected function process(FormRequest $request, Tag $tag)
186
    {
187
        // Prepare required input fields
188
        $data = $request->validated();
189
190
        // Save tag
191
        $tag->fill($data)->save();
192
193
        return intend([
194
            'url' => route('adminarea.tags.index'),
195
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/tags::common.tag'), 'identifier' => $tag->name])],
196
        ]);
197
    }
198
199
    /**
200
     * Destroy given tag.
201
     *
202
     * @param \Cortex\Tags\Models\Tag $tag
203
     *
204
     * @throws \Exception
205
     *
206
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
207
     */
208
    public function destroy(Tag $tag)
209
    {
210
        $tag->delete();
211
212
        return intend([
213
            'url' => route('adminarea.tags.index'),
214
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/tags::common.tag'), 'identifier' => $tag->name])],
215
        ]);
216
    }
217
}
218