Completed
Push — master ( b64705...d28813 )
by Abdelrahman
08:12
created

TestimonialsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Testimonials\Http\Controllers\Managerarea;
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\Managerarea\TestimonialsDataTable;
17
use Cortex\Testimonials\Http\Requests\Managerarea\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\Managerarea\TestimonialsDataTable $testimonialsDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(TestimonialsDataTable $testimonialsDataTable)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $testimonialsDataTable 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...
34
    {
35
        return $testimonialsDataTable->with([
36
            'id' => 'managerarea-testimonials-index-table',
37
        ])->render('cortex/foundation::managerarea.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' => 'managerarea.testimonials.tabs',
53
            'id' => "managerarea-testimonials-{$testimonial->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::managerarea.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)
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' => $testimonial,
69
            'tabs' => 'managerarea.testimonials.tabs',
70
            'url' => route('managerarea.testimonials.stash'),
71
            'id' => "managerarea-testimonials-{$testimonial->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::managerarea.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();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 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...
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...
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...
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' => 'managerarea.testimonials.tabs',
134
            'id' => 'managerarea-testimonials-import-logs-table',
135
        ])->render('cortex/foundation::managerarea.pages.datatable-tab');
136
    }
137
138
    /**
139
     * Create new testimonial.
140
     *
141
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
142
     *
143
     * @return \Illuminate\View\View
144
     */
145
    public function create(Testimonial $testimonial)
146
    {
147
        return $this->form($testimonial);
148
    }
149
150
    /**
151
     * Edit given testimonial.
152
     *
153
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
154
     *
155
     * @return \Illuminate\View\View
156
     */
157
    public function edit(Testimonial $testimonial)
158
    {
159
        return $this->form($testimonial);
160
    }
161
162
    /**
163
     * Show testimonial create/edit form.
164
     *
165
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
166
     *
167
     * @return \Illuminate\View\View
168
     */
169
    protected function form(Testimonial $testimonial)
170
    {
171
        return view('cortex/testimonials::managerarea.pages.testimonial', compact('testimonial'));
172
    }
173
174
    /**
175
     * Store new testimonial.
176
     *
177
     * @param \Cortex\Testimonials\Http\Requests\Managerarea\TestimonialFormRequest $request
178
     * @param \Cortex\Testimonials\Models\Testimonial                               $testimonial
179
     *
180
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
181
     */
182
    public function store(TestimonialFormRequest $request, Testimonial $testimonial)
183
    {
184
        return $this->process($request, $testimonial);
185
    }
186
187
    /**
188
     * Update given testimonial.
189
     *
190
     * @param \Cortex\Testimonials\Http\Requests\Managerarea\TestimonialFormRequest $request
191
     * @param \Cortex\Testimonials\Models\Testimonial                               $testimonial
192
     *
193
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
194
     */
195
    public function update(TestimonialFormRequest $request, Testimonial $testimonial)
196
    {
197
        return $this->process($request, $testimonial);
198
    }
199
200
    /**
201
     * Process stored/updated testimonial.
202
     *
203
     * @param \Illuminate\Foundation\Http\FormRequest $request
204
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
205
     *
206
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
207
     */
208
    protected function process(FormRequest $request, Testimonial $testimonial)
209
    {
210
        // Prepare required input fields
211
        $data = $request->validated();
212
213
        // Save testimonial
214
        $testimonial->fill($data)->save();
215
216
        return intend([
217
            'url' => route('managerarea.testimonials.index'),
218
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => trans('cortex/testimonials::common.testimonial'), 'identifier' => $testimonial->getRouteKey()])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 200 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...
219
        ]);
220
    }
221
222
    /**
223
     * Destroy given testimonial.
224
     *
225
     * @param \Cortex\Testimonials\Models\Testimonial $testimonial
226
     *
227
     * @throws \Exception
228
     *
229
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
230
     */
231
    public function destroy(Testimonial $testimonial)
232
    {
233
        $testimonial->delete();
234
235
        return intend([
236
            'url' => route('managerarea.testimonials.index'),
237
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => trans('cortex/testimonials::common.testimonial'), 'identifier' => $testimonial->getRouteKey()])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 202 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...
238
        ]);
239
    }
240
}
241