Completed
Push — develop ( 94516c...5292b7 )
by Abdelrahman
06:14
created

ContactsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Contacts\Http\Controllers\Managerarea;
6
7
use Rinvex\Contacts\Models\Contact;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Cortex\Foundation\DataTables\LogsDataTable;
10
use Cortex\Foundation\Http\Controllers\AuthorizedController;
11
use Cortex\Contacts\DataTables\Managerarea\ContactsDataTable;
12
use Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest;
13
14
class ContactsController extends AuthorizedController
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected $resource = 'contact';
20
21
    /**
22
     * List all contacts.
23
     *
24
     * @param \Cortex\Contacts\DataTables\Managerarea\ContactsDataTable $contactsDataTable
25
     *
26
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
27
     */
28
    public function index(ContactsDataTable $contactsDataTable)
29
    {
30
        return $contactsDataTable->with([
31
            'id' => 'managerarea-contacts-index-table',
32
            'phrase' => trans('cortex/contacts::common.contacts'),
33
        ])->render('cortex/tenants::managerarea.pages.datatable');
34
    }
35
36
    /**
37
     * List contact logs.
38
     *
39
     * @param \Cortex\Contacts\Models\Contact             $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
40
     * @param \Cortex\Foundation\DataTables\LogsDataTable $logsDataTable
41
     *
42
     * @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...
43
     */
44
    public function logs(Contact $contact, LogsDataTable $logsDataTable)
45
    {
46
        return $logsDataTable->with([
47
            'resource' => $contact,
48
            'tabs' => 'managerarea.contacts.tabs',
49
            'phrase' => trans('cortex/contacts::common.contacts'),
50
            'id' => "managerarea-contacts-{$contact->getKey()}-logs-table",
51
        ])->render('cortex/tenants::managerarea.pages.datatable-logs');
52
    }
53
54
    /**
55
     * Create new contact.
56
     *
57
     * @param \Cortex\Contacts\Models\Contact $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
58
     *
59
     * @return \Illuminate\View\View
60
     */
61
    public function create(Contact $contact)
62
    {
63
        return $this->form($contact);
64
    }
65
66
    /**
67
     * Edit given contact.
68
     *
69
     * @param \Cortex\Contacts\Models\Contact $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
70
     *
71
     * @return \Illuminate\View\View
72
     */
73
    public function edit(Contact $contact)
74
    {
75
        return $this->form($contact);
76
    }
77
78
    /**
79
     * Show contact create/edit form.
80
     *
81
     * @param \Cortex\Contacts\Models\Contact $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
82
     *
83
     * @return \Illuminate\View\View
84
     */
85
    protected function form(Contact $contact)
86
    {
87
        $countries = collect(countries())->map(function ($country, $code) {
88
            return [
89
                'id' => $code,
90
                'text' => $country['name'],
91
                'emoji' => $country['emoji'],
92
            ];
93
        })->values();
94
        $languages = collect(languages())->pluck('name', 'iso_639_1');
95
        $sources = app('rinvex.contacts.contact')->distinct()->get(['source'])->pluck('source', 'source')->toArray();
96
        $methods = app('rinvex.contacts.contact')->distinct()->get(['method'])->pluck('method', 'method')->toArray();
97
        $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...
98
99
        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...
100
    }
101
102
    /**
103
     * Store new contact.
104
     *
105
     * @param \Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest $request
106
     * @param \Cortex\Contacts\Models\Contact                               $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
107
     *
108
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
109
     */
110
    public function store(ContactFormRequest $request, Contact $contact)
111
    {
112
        return $this->process($request, $contact);
113
    }
114
115
    /**
116
     * Update given contact.
117
     *
118
     * @param \Cortex\Contacts\Http\Requests\Managerarea\ContactFormRequest $request
119
     * @param \Cortex\Contacts\Models\Contact                               $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
120
     *
121
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
122
     */
123
    public function update(ContactFormRequest $request, Contact $contact)
124
    {
125
        return $this->process($request, $contact);
126
    }
127
128
    /**
129
     * Process stored/updated contact.
130
     *
131
     * @param \Illuminate\Foundation\Http\FormRequest $request
132
     * @param \Cortex\Contacts\Models\Contact         $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
133
     *
134
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
135
     */
136
    protected function process(FormRequest $request, Contact $contact)
137
    {
138
        // Prepare required input fields
139
        $data = $request->validated();
140
141
        // Save contact
142
        $contact->fill($data)->save();
143
144
        return intend([
145
            'url' => route('managerarea.contacts.index'),
146
            'with' => ['success' => trans('cortex/foundation::messages.resource_saved', ['resource' => 'contact', 'id' => $contact->slug])],
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...
147
        ]);
148
    }
149
150
    /**
151
     * Destroy given contact.
152
     *
153
     * @param \Cortex\Contacts\Models\Contact $contact
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $contact a bit more specific; maybe use Contact.
Loading history...
154
     *
155
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
156
     */
157
    public function destroy(Contact $contact)
158
    {
159
        $contact->delete();
160
161
        return intend([
162
            'url' => route('managerarea.contacts.index'),
163
            'with' => ['warning' => trans('cortex/foundation::messages.resource_deleted', ['resource' => 'contact', 'id' => $contact->slug])],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 142 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...
164
        ]);
165
    }
166
}
167