Passed
Push — dev5 ( eeca7a...5844c4 )
by Ron
10:35
created

SetCustomerContacts::checkBelongToParent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Domains\Customers;
4
5
use App\CustomerContactPhones;
6
use App\CustomerContacts;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Auth;
9
10
use App\Customers;
11
use App\Http\Requests\CustomerEditContactRequest;
12
use App\Http\Requests\CustomerNewContactRequest;
13
14
class SetCustomerContacts
15
{
16
    protected $custID;
17
18 12
    public function __construct($custID)
19
    {
20 12
        $this->custID = $custID;
21 12
    }
22
23 4
    public function createContact(CustomerNewContactRequest $request)
24
    {
25
        //
26 4
        if($request->shared)
27
        {
28
            $this->checkBelongToParent($request->cust_id);
29
        }
30
31
        //  Update the primary information
32 4
        $contData = CustomerContacts::create([
33 4
            'cust_id' => $this->custID,
34 4
            'shared'  => $request->shared,
35 4
            'name'    => $request->name,
36 4
            'email'   => $request->email,
37
        ]);
38
39 4
        $this->processPhoneNumbers($contData->cont_id, $request->customer_contact_phones);
40
41 4
        Log::info('Contact created for customer ID '.$this->custID.' by '.Auth::user()->full_name.'.  New Data - ', array($contData));
42 4
        return true;
43
    }
44
45 6
    public function updateContact(CustomerEditContactRequest $request)
46
    {
47
        //  verify that the contact id is valid
48 6
        $contData = CustomerContacts::where('cont_id', $request->cont_id)->where('cust_id', $this->custID)->first();
49 6
        if(!$contData)
50
        {
51 6
            return false;
52
        }
53
54
        if($request->shared)
55
        {
56
            $this->checkBelongToParent($request->cust_id);
57
        }
58
59
        //  Update the primary information
60
        $contData->update([
61
            'cust_id' => $this->custID,
62
            'shared'  => $request->shared,
63
            'name'    => $request->name,
64
            'email'   => $request->email,
65
        ]);
66
67
        $this->processPhoneNumbers($request->cont_id, $request->customer_contact_phones);
68
69
        Log::info('Contact information updated for customer ID '.$this->custID.' by '.Auth::user()->full_name.'.  New Data - ', array($contData));
70
        return true;
71
    }
72
73
    //  Remove a specific contact
74 2
    public function deleteContact($contID)
75
    {
76 2
        CustomerContacts::find($contID)->delete();
77 2
        Log::notice('Customer Contact ID '.$contID.' deleted by '.Auth::user()->full_name);
78 2
        return true;
79
    }
80
81
    //  Determine if the contact should be attached to the parent site
82
    protected function checkBelongToParent($custID)
83
    {
84
        $custData = Customers::find($custID);
85
        if($custData->parent_id)
86
        {
87
            $this->custID = $custData->parentID;
88
        }
89
    }
90
91
    //  Cycle through phone numbers and enter or edit them in the database
92 4
    protected function processPhoneNumbers($contID, $phoneData)
93
    {
94 4
        $curData = CustomerContactPhones::where('cont_id', $contID)->get();
95
96 4
        if($phoneData)
97
        {
98 4
            foreach($phoneData as $phone)
99
            {
100 4
                if(isset($phone['id']) && !empty($phone['readable']))
101
                {
102
                    CustomerContactPhones::find($phone['id'])->update([
103
                        'phone_type_id' => $phone['phone_type_id'],
104
                        'phone_number'  => $this->cleanPhoneNumber($phone['readable']),
105
                        'extension'     => $phone['extension'],
106
                    ]);
107
                    $curData = $curData->filter(function($item) use ($phone) {
108
                        return $item->id != $phone['id'];
109
                    });
110
                }
111 4
                else if(!empty($phone['readable']))
112
                {
113
                    CustomerContactPhones::create([
114
                        'cont_id'       => $contID,
115
                        'phone_type_id' => $phone['phone_type_id'],
116
                        'phone_number'  => $this->cleanPhoneNumber($phone['readable']),
117
                        'extension'     => $phone['extension'],
118
                    ]);
119
                }
120
            }
121
        }
122
123
        //  Remove any numbers that were deleted
124 4
        foreach($curData as $phone)
125
        {
126
            CustomerContactPhones::find($phone->id)->delete();
127
        }
128 4
    }
129
130
    //  Re-format the number to be strictly 10 digits
131
    protected function cleanPhoneNumber($number)
132
    {
133
        return preg_replace('~.*(\d{3})[^\d]*(\d{3})[^\d]*(\d{4}).*~', '$1$2$3', $number);
134
    }
135
136
}
137