Completed
Push — master ( 7c292a...33abbb )
by Abdelrahman
10:08 queued 09:01
created

TestimonialsController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
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\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
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)
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
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
     * 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::managerarea.pages.testimonial', compact('testimonial'));
148
    }
149
150
    /**
151
     * Store new testimonial.
152
     *
153
     * @param \Cortex\Testimonials\Http\Requests\Managerarea\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\Managerarea\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('managerarea.testimonials.index'),
194
            '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...
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('managerarea.testimonials.index'),
213
            '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...
214
        ]);
215
    }
216
}
217