TestimonialsController::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Testimonials\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Cortex\Testimonials\Models\Testimonial;
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\Testimonials\DataTables\Adminarea\TestimonialsDataTable;
17
use Cortex\Testimonials\Http\Requests\Adminarea\TestimonialFormRequest;
18
19
class TestimonialsController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Testimonial::class;
25
26
    /**
27
     * List all testimonials.
28
     *
29
     * @param \Cortex\Testimonials\DataTables\Adminarea\TestimonialsDataTable $testimonialsDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(TestimonialsDataTable $testimonialsDataTable)
34
    {
35
        return $testimonialsDataTable->with([
36
            'id' => 'adminarea-testimonials-index-table',
37
        ])->render('cortex/foundation::adminarea.pages.datatable-index');
38
    }
39
40
    /**
41
     * List testimonial logs.
42
     *
43
     * @param \Cortex\Testimonials\Models\Testimonial     $testimonial
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(Testimonial $testimonial, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $testimonial,
52
            'tabs' => 'adminarea.testimonials.tabs',
53
            'id' => "adminarea-testimonials-{$testimonial->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
55
    }
56
57
    /**
58
     * Import testimonials.
59
     *
60
     * @param \Cortex\Testimonials\Models\Testimonial              $testimonial
61
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    public function import(Testimonial $testimonial, ImportRecordsDataTable $importRecordsDataTable)
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $testimonial,
69
            'tabs' => 'adminarea.testimonials.tabs',
70
            'url' => route('adminarea.testimonials.stash'),
71
            'id' => "adminarea-testimonials-{$testimonial->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::adminarea.pages.datatable-dropzone');
73
    }
74
75
    /**
76
     * Stash testimonials.
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->config['name'] = 'id';
88
        $importer->handleImport();
89
    }
90
91
    /**
92
     * Hoard testimonials.
93
     *
94
     * @param \Cortex\Foundation\Http\Requests\ImportFormRequest $request
95
     *
96
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
97
     */
98
    public function hoard(ImportFormRequest $request)
99
    {
100
        foreach ((array) $request->get('selected_ids') as $recordId) {
101
            $record = app('cortex.foundation.import_record')->find($recordId);
102
103
            try {
104
                $fillable = collect($record['data'])->intersectByKeys(array_flip(app('rinvex.testimonials.testimonial')->getFillable()))->toArray();
105
106
                tap(app('rinvex.testimonials.testimonial')->firstOrNew($fillable), function ($instance) use ($record) {
107
                    $instance->save() && $record->delete();
108
                });
109
            } catch (Exception $exception) {
110
                $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...
111
                $record->status = 'fail';
112
                $record->save();
113
            }
114
        }
115
116
        return intend([
117
            'back' => true,
118
            'with' => ['success' => trans('cortex/foundation::messages.import_complete')],
119
        ]);
120
    }
121
122
    /**
123
     * List testimonial import logs.
124
     *
125
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
126
     *
127
     * @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...
128
     */
129
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
130
    {
131
        return $importLogsDatatable->with([
132
            'resource' => trans('cortex/testimonials::common.testimonial'),
133
            'tabs' => 'adminarea.testimonials.tabs',
134
            'id' => 'adminarea-testimonials-import-logs-table',
135
        ])->render('cortex/foundation::adminarea.pages.datatable-tab');
136
    }
137
138
    /**
139
     * Show testimonial create/edit form.
140
     *
141
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
142
     *
143
     * @return \Illuminate\View\View
144
     */
145
    protected function form(Testimonial $testimonial)
146
    {
147
        return view('cortex/testimonials::adminarea.pages.testimonial', compact('testimonial'));
148
    }
149
150
    /**
151
     * Store new testimonial.
152
     *
153
     * @param \Cortex\Testimonials\Http\Requests\Adminarea\TestimonialFormRequest $request
154
     * @param \Cortex\Testimonials\Models\Testimonial                             $testimonial
155
     *
156
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
157
     */
158
    public function store(TestimonialFormRequest $request, Testimonial $testimonial)
159
    {
160
        return $this->process($request, $testimonial);
161
    }
162
163
    /**
164
     * Update given testimonial.
165
     *
166
     * @param \Cortex\Testimonials\Http\Requests\Adminarea\TestimonialFormRequest $request
167
     * @param \Cortex\Testimonials\Models\Testimonial                             $testimonial
168
     *
169
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
170
     */
171
    public function update(TestimonialFormRequest $request, Testimonial $testimonial)
172
    {
173
        return $this->process($request, $testimonial);
174
    }
175
176
    /**
177
     * Process stored/updated testimonial.
178
     *
179
     * @param \Illuminate\Foundation\Http\FormRequest $request
180
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
181
     *
182
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
183
     */
184
    protected function process(FormRequest $request, Testimonial $testimonial)
185
    {
186
        // Prepare required input fields
187
        $data = $request->validated();
188
189
        // Save testimonial
190
        $testimonial->fill($data)->save();
191
192
        return intend([
193
            'url' => route('adminarea.testimonials.index'),
194
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/testimonials::common.testimonial'), 'identifier' => $testimonial->getRouteKey()])],
195
        ]);
196
    }
197
198
    /**
199
     * Destroy given testimonial.
200
     *
201
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
202
     *
203
     * @throws \Exception
204
     *
205
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
206
     */
207
    public function destroy(Testimonial $testimonial)
208
    {
209
        $testimonial->delete();
210
211
        return intend([
212
            'url' => route('adminarea.testimonials.index'),
213
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/testimonials::common.testimonial'), 'identifier' => $testimonial->getRouteKey()])],
214
        ]);
215
    }
216
}
217