CustomerContactsController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 48.15%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
eloc 69
c 4
b 0
f 0
dl 0
loc 149
ccs 13
cts 27
cp 0.4815
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanPhoneNumber() 0 3 1
A destroy() 0 8 1
B update() 0 55 6
A restore() 0 8 1
A store() 0 37 5
A forceDelete() 0 8 1
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Http\Controllers\Controller;
6
7
use App\Models\Customer;
8
use App\Models\PhoneNumberType;
9
use App\Models\CustomerContact;
10
use App\Models\CustomerContactPhone;
11
12
use App\Http\Requests\Customers\CustomerContactsRequest;
13
use App\Events\Customers\Contacts\CustomerContactAddedEvent;
14
use App\Events\Customers\Contacts\CustomerContactDeletedEvent;
15
use App\Events\Customers\Contacts\CustomerContactUpdatedEvent;
16
use App\Events\Customers\Contacts\CustomerContactRestoredEvent;
17
use App\Events\Customers\Contacts\CustomerContactForceDeletedEvent;
18 26
19
class CustomerContactsController extends Controller
20 26
{
21 26
    /**
22
     * Store a newly created customer contact
23
     */
24
    public function store(CustomerContactsRequest $request)
25
    {
26
        $cust    = Customer::findOrFail($request->cust_id);
27
        $cust_id = $cust->cust_id;
28
29
        //  If the equipment is shared, it must be assigned to the parent site
30 4
        if($request->shared && $cust->parent_id > 0)
31
        {
32 4
            $cust_id = $cust->parent_id;
33 4
        }
34
35
        //  Create the contact
36
        $newContact = CustomerContact::create([
37 4
            'cust_id' => $cust_id,
38
            'name'    => $request->name,
39 4
            'email'   => $request->email,
40
            'shared'  => $request->shared,
41
            'title'   => $request->title,
42
            'note'    => $request->note,
43
        ]);
44
45
        //  Input the contacts phone numbers
46
        foreach($request->phones as $phone)
47
        {
48
            if(isset($phone['number']))
49
            {
50
                CustomerContactPhone::create([
51
                    'cont_id'       => $newContact->cont_id,
52
                    'phone_type_id' => PhoneNumberType::where('description', $phone['type'])->first()->phone_type_id,
53
                    'phone_number'  => $this->cleanPhoneNumber($phone['number']),
54
                    'extension'     => $phone['extension'],
55
                ]);
56
            }
57
        }
58
59
        event(new CustomerContactAddedEvent($cust, $newContact));
60
        return back()->with(['message' => 'New Contact Created', 'type' => 'success']);
61
    }
62
63
    /**
64
     * Update an existing contact
65 6
     */
66
    public function update(CustomerContactsRequest $request, $id)
67 6
    {
68 6
        $cust    = Customer::findOrFail($request->cust_id);
69
        $cust_id = $cust->cust_id;
70
71
        //  If the equipment is shared, it must be assigned to the parent site
72 2
        if($request->shared && $cust->parent_id > 0)
73
        {
74 2
            $cust_id = $cust->parent_id;
75 2
        }
76
77
        $contact = CustomerContact::find($id);
78
        $contact->update([
79
            'cust_id' => $cust_id,
80
            'name'    => $request->name,
81
            'email'   => $request->email,
82
            'shared'  => $request->shared,
83
            'title'   => $request->title,
84
            'note'    => $request->note,
85
        ]);
86
87
        $updatedNumbers = [];
88
        foreach($request->phones as $phone)
89
        {
90
            //  If the number is an existing number, update it
91
            if(isset($phone['id']))
92
            {
93
                CustomerContactPhone::find($phone['id'])->update([
94
                    'phone_type_id' => PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id,
95
                    'phone_number'  => $this->cleanPhoneNumber($phone['phone_number']),
96
                    'extension'     => $phone['extension'],
97
                ]);
98
                $updatedNumbers[] = $phone['id'];
99
            }
100
            //  Otherwise enter a new number
101
            else
102
            {
103
                $new = CustomerContactPhone::create([
104
                    'cont_id'       => $id,
105
                    'phone_type_id' =>PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id,
106
                    'phone_number'  => $this->cleanPhoneNumber($phone['phone_number']),
107
                    'extension'     => $phone['extension'],
108
                ]);
109
                $updatedNumbers[] = $new->id;
110
            }
111
        }
112
113
        $oldContacts = CustomerContactPhone::where('cont_id', $id)->whereNotIn('id', $updatedNumbers)->get();
114
        foreach($oldContacts as $cont)
115
        {
116
            $cont->delete();
117
        }
118
119
        event(new CustomerContactUpdatedEvent($cust, $contact));
120
        return back()->with(['message' => 'Contact Updated', 'type' => 'success']);
121
    }
122
123
    /**
124
     * Soft Delete a Customer Contact
125
     */
126
    public function destroy($id)
127
    {
128
        $this->authorize('delete', CustomerContact::class);
129
        $cont = CustomerContact::find($id);
130
        $cont->delete();
131
132
        event(new CustomerContactDeletedEvent(Customer::find($cont->cust_id), $cont));
133
        return back()->with(['message' => 'Contact deleted', 'type' => 'danger']);
134
    }
135
136
    /**
137
     * Restore a contact that was soft deleted
138
     */
139
    public function restore($id)
140
    {
141
        $this->authorize('restore', CustomerContact::class);
142
        $cont = CustomerContact::withTrashed()->where('cont_id', $id)->first();
143
        $cont->restore();
144
145
        event(new CustomerContactRestoredEvent(Customer::find($cont->cust_id), $cont));
146
        return back()->with(['message' => 'Contact '.$cont->name.' restored', 'type' => 'success']);
147
    }
148
149
    /**
150
     * Permanently delete a contact
151
     */
152
    public function forceDelete($id)
153
    {
154
        $cont = CustomerContact::withTrashed()->where('cont_id', $id)->first();
155
        $this->authorize('forceDelete', $cont);
156
        $cont->forceDelete();
157
158
        event(new CustomerContactForceDeletedEvent(Customer::find($cont->cust_id), $cont));
159
        return back()->with(['message' => 'Contact permanently deleted', 'type' => 'danger']);
160
    }
161
162
    /*
163
    *   Clean the phone number to be digits only
164
    */
165
    protected function cleanPhoneNumber($number)
166
    {
167
        return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number);
168
    }
169
}
170