Test Failed
Push — dev6 ( d1c8a2...d5abaa )
by Ron
20:46
created

CustomerContactsController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
eloc 65
c 3
b 0
f 0
dl 0
loc 149
rs 10

7 Methods

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