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 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Contacts\Http\Controllers\Managerarea;
6
7
use Cortex\Contacts\Models\Contact;
8
use Cortex\Foundation\DataTables\ImportRecordsDataTable;
9
use Exception;
10
use Illuminate\Foundation\Http\FormRequest;
11
use Cortex\Foundation\DataTables\LogsDataTable;
12
use Cortex\Foundation\Importers\DefaultImporter;
13
use Cortex\Foundation\DataTables\ImportLogsDataTable;
14
use Cortex\Foundation\Http\Requests\ImportFormRequest;
15
use Cortex\Foundation\Http\Controllers\AuthorizedController;
16
use Cortex\Contacts\DataTables\Managerarea\ContactsDataTable;
17
use Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest;
18
19
class ContactsController extends AuthorizedController
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $resource = Contact::class;
25
26
    /**
27
     * List all contacts.
28
     *
29
     * @param \Cortex\Contacts\DataTables\Managerarea\ContactsDataTable $contactsDataTable
30
     *
31
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
32
     */
33
    public function index(ContactsDataTable $contactsDataTable)
34
    {
35
        return $contactsDataTable->with([
36
            'id' => 'managerarea-contacts-index-table',
37
        ])->render('cortex/foundation::managerarea.pages.datatable-index');
38
    }
39
40
    /**
41
     * List contact logs.
42
     *
43
     * @param \Cortex\Contacts\Models\Contact             $contact
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(Contact $contact, LogsDataTable $logsDataTable)
49
    {
50
        return $logsDataTable->with([
51
            'resource' => $contact,
52
            'tabs' => 'managerarea.contacts.tabs',
53
            'id' => "managerarea-contacts-{$contact->getRouteKey()}-logs-table",
54
        ])->render('cortex/foundation::managerarea.pages.datatable-tab');
55
    }
56
57
    /**
58
     * Import contacts.
59
     *
60
     * @param \Cortex\Contacts\Models\Contact                      $contact
61
     * @param \Cortex\Foundation\DataTables\ImportRecordsDataTable $importRecordsDataTable
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    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...
66
    {
67
        return $importRecordsDataTable->with([
68
            'resource' => $contact,
69
            'tabs' => 'managerarea.contacts.tabs',
70
            'url' => route('managerarea.contacts.stash'),
71
            'id' => "managerarea-contacts-{$contact->getRouteKey()}-import-table",
72
        ])->render('cortex/foundation::managerarea.pages.datatable-dropzone');
73
    }
74
75
    /**
76
     * Stash contacts.
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 contacts.
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.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...
104
105
                tap(app('rinvex.contacts.contact')->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...
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...
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 contact import logs.
123
     *
124
     * @param \Cortex\Foundation\DataTables\ImportLogsDataTable $importLogsDatatable
125
     *
126
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
127
     */
128
    public function importLogs(ImportLogsDataTable $importLogsDatatable)
129
    {
130
        return $importLogsDatatable->with([
131
            'resource' => trans('cortex/contacts::common.contact'),
132
            'tabs' => 'managerarea.contacts.tabs',
133
            'id' => 'managerarea-contacts-import-logs-table',
134
        ])->render('cortex/foundation::managerarea.pages.datatable-tab');
135
    }
136
137
    /**
138
     * Create new contact.
139
     *
140
     * @param \Cortex\Contacts\Models\Contact $contact
141
     *
142
     * @return \Illuminate\View\View
143
     */
144
    public function create(Contact $contact)
145
    {
146
        return $this->form($contact);
147
    }
148
149
    /**
150
     * Edit given contact.
151
     *
152
     * @param \Cortex\Contacts\Models\Contact $contact
153
     *
154
     * @return \Illuminate\View\View
155
     */
156
    public function edit(Contact $contact)
157
    {
158
        return $this->form($contact);
159
    }
160
161
    /**
162
     * Show contact create/edit form.
163
     *
164
     * @param \Cortex\Contacts\Models\Contact $contact
165
     *
166
     * @return \Illuminate\View\View
167
     */
168
    protected function form(Contact $contact)
169
    {
170
        $countries = collect(countries())->map(function ($country, $code) {
171
            return [
172
                'id' => $code,
173
                'text' => $country['name'],
174
                'emoji' => $country['emoji'],
175
            ];
176
        })->values();
177
        $languages = collect(languages())->pluck('name', 'iso_639_1');
178
        $sources = app('rinvex.contacts.contact')->distinct()->get(['source'])->pluck('source', 'source')->toArray();
179
        $methods = app('rinvex.contacts.contact')->distinct()->get(['method'])->pluck('method', 'method')->toArray();
180
        $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...
181
182
        return view('cortex/contacts::managerarea.pages.contact', compact('contact', 'genders', 'countries', 'languages', 'sources', 'methods'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 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...
183
    }
184
185
    /**
186
     * Store new contact.
187
     *
188
     * @param \Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest $request
189
     * @param \Cortex\Contacts\Models\Contact                               $contact
190
     *
191
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
192
     */
193
    public function store(ContactFormRequest $request, Contact $contact)
194
    {
195
        return $this->process($request, $contact);
196
    }
197
198
    /**
199
     * Update given contact.
200
     *
201
     * @param \Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest $request
202
     * @param \Cortex\Contacts\Models\Contact                               $contact
203
     *
204
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
205
     */
206
    public function update(ContactFormRequest $request, Contact $contact)
207
    {
208
        return $this->process($request, $contact);
209
    }
210
211
    /**
212
     * Process stored/updated contact.
213
     *
214
     * @param \Illuminate\Foundation\Http\FormRequest $request
215
     * @param \Cortex\Contacts\Models\Contact         $contact
216
     *
217
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
218
     */
219
    protected function process(FormRequest $request, Contact $contact)
220
    {
221
        // Prepare required input fields
222
        $data = $request->validated();
223
224
        // Save contact
225
        $contact->fill($data)->save();
226
227
        return intend([
228
            'url' => route('managerarea.contacts.index'),
229
            '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...
230
        ]);
231
    }
232
233
    /**
234
     * Destroy given contact.
235
     *
236
     * @param \Cortex\Contacts\Models\Contact $contact
237
     *
238
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
239
     */
240
    public function destroy(Contact $contact)
241
    {
242
        $contact->delete();
243
244
        return intend([
245
            'url' => route('managerarea.contacts.index'),
246
            '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...
247
        ]);
248
    }
249
}
250