Completed
Push — develop ( 8b4f1c...827f15 )
by Abdelrahman
01:35
created

ContactsController::stash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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